Skip to content
Notifications
Clear all

Check out what I made: A script to sync Consensus data to BigQuery.

4 Posts
4 Users
0 Reactions
21 Views
(@integration_tinkerer)
Estimable Member
Joined: 6 months ago
Posts: 141
Topic starter   [#8863]

Hey everyone, I've been digging into Consensus for a project and really needed a way to get the aggregated research data out for deeper analysis alongside our other metrics. Their API is pretty solid, but I wanted a persistent data warehouse setup.

So I built a script that pulls from the Consensus API and syncs it directly to BigQuery on a schedule. This lets me run complex queries, join research findings with our internal product usage data, and build Looker Studio dashboards. Here's the core of it:

```python
import requests
import pandas as pd
from google.cloud import bigquery

CONSENSUS_API_KEY = 'your_key_here'
PROJECT_ID = 'your_project_id'
DATASET_ID = 'consensus'

def fetch_consensus_papers(query):
url = "https://consensus.app/api/v1/search/"
headers = {"Authorization": f"Bearer {CONSENSUS_API_KEY}"}
params = {"query": query, "size": 50} # adjust size as needed
response = requests.get(url, headers=headers, params=params)
return response.json().get('results', [])

def transform_to_dataframe(results):
# Flatten the nested JSON structure
rows = []
for paper in results:
rows.append({
'paper_id': paper.get('id'),
'title': paper.get('title'),
'year': paper.get('year'),
'citation_count': paper.get('citationCount'),
'consensus_strength': paper.get('consensusStrength'),
'abstract': paper.get('abstract')
})
return pd.DataFrame(rows)

# Fetch and transform
papers_data = fetch_consensus_papers("effects of intermittent fasting")
df = transform_to_dataframe(papers_data)

# Load to BigQuery
client = bigquery.Client(project=PROJECT_ID)
table_ref = f"{PROJECT_ID}.{DATASET_ID}.papers"
job = client.load_table_from_dataframe(df, table_ref)
```

I run this as a Cloud Function triggered by Cloud Scheduler every week. The main things I had to handle:

* **Pagination:** The script above fetches the first page; you'll need a loop to get all results if you have many.
* **Schema management:** I defined the BigQuery table schema upfront to control data types.
* **Incremental updates:** I modified it to use `paper_id` as a key to avoid full reloads.

Now I can easily track things like:
- Which health topics have the strongest consensus over time?
- Correlation between citation count and consensus strength.
- Merging this with our user research data in the same warehouse.

Has anyone else tried piping Consensus data elsewhere? I'm curious about other destinations like Snowflake or Airtable. If you want the full Cloud Function code with error handling, just let me know!



   
Quote
(@cost_cutter_99)
Honorable Member
Joined: 6 months ago
Posts: 404
 

Nice approach. I'm curious about the cost side of this setup. Have you run into any unexpected BigQuery charges from running this on a schedule, especially with large result sets? The API pricing page mentions rate limits, but I've found that consistent, automated pulls can add up fast.

Also, did you consider using something like Cloud Scheduler plus Cloud Functions instead of a persistent VM? Might be cheaper if you're only syncing a few times a day. The trade-off is a bit more setup complexity.

What's your rough data volume per run?



   
ReplyQuote
(@amyc)
Reputable Member
Joined: 3 months ago
Posts: 397
 

Great questions. The cost angle is crucial, especially with vendor APIs where you pay per call on both ends.

I've seen folks get bitten by not factoring in the cost of storing and querying the raw JSON in BigQuery, which can be pricier than expected if you're pulling full paper texts. Partitioning your tables by sync date is a must to manage query scans.

Cloud Scheduler + Functions is a solid suggestion for a lightweight cron. The main trade-off is execution time limits, but for most sync jobs, it's perfect and way more cost-effective than a sleeping VM.



   
ReplyQuote
(@data_pipeline_newbie)
Reputable Member
Joined: 5 months ago
Posts: 292
 

Oh that's so cool! I love seeing how people connect different data sources like this. I'm trying to build something similar but for a different academic database.

Your script looks straightforward, which is encouraging for a beginner like me. I'm curious, how are you handling incremental updates? Like, when your script runs again tomorrow, how does it know not to pull the same 50 papers over again? Does the API have a `date` parameter you filter on, or do you check what's already in BigQuery? I'd probably mess that part up and duplicate all my data.



   
ReplyQuote