BigQuery
Squids can store their data to BigQuery datasets using the@subsquid/bigquery-store package.
Basic Usage
Define and use theDatabase object as follows:
src/main.ts
Configuration
bqis aBigQueryinstance. When created without arguments it’ll look at theGOOGLE_APPLICATION_CREDENTIALSenvironment variable for a path to a JSON with authentication details.datasetis the path to the target dataset.
tableslists the tables that will be created and populated within the dataset. For every field of thetablesobject an eponymous field of thectx.storeobject will be created; callinginsert()orinsertMany()on such a field will result in data being queued for writing to the corresponding dataset table. The actual writing will be done at the end of the batch in a single transaction, ensuring dataset integrity.
Deploying to SQD Cloud
We discourage uploading any sensitive data with squid code when deploying to SQD Cloud. To pass your credentials JSON to your squid, create a Cloud secret variable populated with its contents:src/main.ts write the contents to a file:
src/main.ts
GOOGLE_APPLICATION_CREDENTIALS variable and request the secret in the deployment manifest:
squid.yaml
BigQuery Examples
An end-to-end example geared towards local runs can be found in this repo. Look at this branch for an example of a squid made for deployment to SQD Cloud.BigQuery Troubleshooting
Transaction is aborted due to concurrent update This means that your project has an open session that is updating some of the tables used by the squid. Most commonly, the session is left by a squid itself after an unclean termination. You have two options:- If you are not sure if your squid is the only app that uses sessions to access your BigQuery project, find the faulty session manually and terminate it. See Get a list of your active sessions and Terminate a session by ID.
-
DANGEROUS If you are absolutely certain that the squid is the only app that uses sessions to access your BigQuery project, you can terminate all the dangling sessions at once.
Terminating all dangling sessions
Run the following, replacingregion-uswith your dataset’s region:You can also enableabortAllProjectSessionsOnStartupand supplydatasetRegionin your database config to perform this operation at startup:This method will cause data loss if, at the moment when the squid starts, some other app happens to be writing data anywhere in the project using the sessions mechanism.
insert() calls), then enable pagination for that table by passing a page size as the third argument of the Table constructor:
Filesystem Datasets
Overview
Squid SDK provides append-onlyStore interface implementations for saving data to files. The store is designed primarily for offline analytics and supports:
- CSV files - Using
@subsquid/file-store-csv - Parquet files - Using
@subsquid/file-store-parquet - JSON files - Using
@subsquid/file-store-json
Basic Example
Save decoded transfer records totransfers.csv files:
./data folder may look like this:
status.txt contains the height of the last indexed block and its hash.
Data Partitioning
File-based stores always partition datasets along the “block height” dimension, even when it is not in the schema. A new partition is written when either:- The internal buffer (size governed by
chunkSizeMb) fills up, or - There’s a call to
setForceFlush()during batch processing
Storage Destinations
- Local filesystem - Using
LocalDestclass - S3-compatible storage - Using
S3Destclass for AWS S3, Google Cloud Storage, or other S3-compatible services