]> code.communitydata.science - cdsc_reddit.git/blob - examples/pyarrow_reading.py
update examples with working streaming
[cdsc_reddit.git] / examples / pyarrow_reading.py
1 import pyarrow.dataset as ds
2
3 # A pyarrow dataset abstracts reading, writing, or filtering a parquet file. It does not read dataa into memory. 
4 #dataset = ds.dataset(pathlib.Path('/gscratch/comdata/output/reddit_submissions_by_subreddit.parquet/'), format='parquet', partitioning='hive')
5 dataset = ds.dataset('/gscratch/comdata/output/reddit_comments_by_subreddit.parquet/', format='parquet')
6
7 # let's get all the comments to two subreddits:
8 subreddits_to_pull = ['seattle','seattlewa']
9
10 # a table is a low-level structured data format.  This line pulls data into memory. Setting metadata_n_threads > 1 gives a little speed boost.
11 table = dataset.to_table(filter = ds.field('subreddit').isin(subreddits_to_pull), columns=['id','subreddit','CreatedAt','author','ups','downs','score','subreddit_id','stickied','title','url','is_self','selftext'])
12
13 # Since data from just these 2 subreddits fits in memory we can just turn our table into a pandas dataframe.
14 df = table.to_pandas()
15
16 # We should save this smaller dataset so we don't have to wait 15 min to pull from parquet next time.
17 df.to_csv("mydataset.csv")

Community Data Science Collective || Want to submit a patch?