Quickstart: BigQuery
In about fifteen minutes you will load public sample JSON from S3 into a BigQuery table and query it.
Mfd infers the schema, creates the BigQuery dataset and table for you, and inserts the rows with BigQuery jobs. You provide a Google Cloud project and a service account.
Before you begin
You need:
- Mfd installed. Check with
mfd --version, orpython -c "import mfd"if you use Python. See Install. - A Google Cloud project with the BigQuery API enabled, and permission to create service accounts and grant roles in it.
- The
gcloudandbqcommand-line tools, signed in to that project. You can do the same steps in the Google Cloud console instead. - AWS credentials that can read
s3://mfd-public-sample-data/bike-hire/. Any AWS account works.
Export your credentials and project ID, then create an empty working directory. Mfd reads mfd.yml from the current directory, so run every command from there.
export AWS_ACCESS_KEY_ID="your-access-key-id"
export AWS_SECRET_ACCESS_KEY="your-secret-access-key"
export AWS_DEFAULT_REGION="us-east-1"
export GCP_PROJECT="my-gcp-project"
mkdir mfd-bigquery && cd mfd-bigquery1. Create a service account
Mfd authenticates to BigQuery with a service account key file. The service account needs BigQuery Data Editor (create datasets and tables, write rows) and BigQuery Job User (run the jobs that insert the rows).
gcloud iam service-accounts create mfd-loader --project "$GCP_PROJECT"
gcloud projects add-iam-policy-binding "$GCP_PROJECT" \
--member "serviceAccount:mfd-loader@$GCP_PROJECT.iam.gserviceaccount.com" \
--role roles/bigquery.dataEditor
gcloud projects add-iam-policy-binding "$GCP_PROJECT" \
--member "serviceAccount:mfd-loader@$GCP_PROJECT.iam.gserviceaccount.com" \
--role roles/bigquery.jobUser
gcloud iam service-accounts keys create mfd-loader.json \
--iam-account "mfd-loader@$GCP_PROJECT.iam.gserviceaccount.com"
export GOOGLE_APPLICATION_CREDENTIALS="$PWD/mfd-loader.json"Keep mfd-loader.json out of version control. It grants write access to BigQuery in your project.
2. Describe the pipeline
Each tab writes the same mfd.yml. Replace my-gcp-project with your project ID.
from mfd import Config, DataSinkBigquery, DataSourceS3, LocalStorage, Pipeline
cfg = Config().workspace("quickstart").storage(LocalStorage())
sample = cfg.data_source(
"sample",
DataSourceS3(s3_bucket="mfd-public-sample-data", s3_prefix="bike-hire"),
)
warehouse = cfg.data_sink(
"warehouse",
DataSinkBigquery(
project="my-gcp-project",
dataset="mfd_quickstart",
location="US",
credentials_path="${GOOGLE_APPLICATION_CREDENTIALS}",
),
)
cfg.pipeline("bikehire", Pipeline(data_source=sample, data_sink=warehouse))
cfg.save("mfd.yml")mfd --workspace quickstart --storage-mode local connect data-source s3 \
--pipeline bikehire \
--name sample \
--s3-bucket mfd-public-sample-data \
--s3-prefix bike-hire
mfd connect data-sink bigquery \
--pipeline bikehire \
--name warehouse \
--project my-gcp-project \
--dataset mfd_quickstart \
--location US \
--credentials-path '${GOOGLE_APPLICATION_CREDENTIALS}'mfd:
workspace: quickstart
mfd_el_storage_mode: local
pipelines:
bikehire:
data_source: data_sources.sample
data_sink: data_sinks.warehouse
data_sources:
sample:
S3:
s3_bucket: mfd-public-sample-data
s3_prefix: bike-hire
data_sinks:
warehouse:
Bigquery:
project: my-gcp-project
dataset: mfd_quickstart
location: US
credentials_path: ${GOOGLE_APPLICATION_CREDENTIALS}What the settings mean:
credentials_pathis required. Mfd reads the key only from this setting; it does not pick upGOOGLE_APPLICATION_CREDENTIALSon its own. Writing${GOOGLE_APPLICATION_CREDENTIALS}reads the path from the environment when the pipeline starts. In the CLI, single quotes stop your shell from expanding it early.locationis where Mfd creates the dataset if it does not exist. If the dataset already exists, use its location.LocalStorage()(mfd_el_storage_mode: local) keeps Mfd's own state, such as the discovered schema and how far it has read, in./data. That is fine for a trial; for production, see Manifold Store.
After you save the file, check it. Doctor validates mfd.yml; it does not connect, so it does not replace discover.
import mfd
s = mfd.Session(mfd.Config.discover().get_pipeline("bikehire"))
s.doctor()mfd doctor3. Discover the schema
import mfd
s = mfd.Session(mfd.Config.discover().get_pipeline("bikehire"))
s.discover()mfd discover --pipeline bikehire
mfd schema --pipeline bikehirediscover samples the JSON and records each field's name and type. It does not touch BigQuery. mfd schema prints the fields, which become your table's columns.
4. Sync
import mfd
s = mfd.Session(mfd.Config.discover().get_pipeline("bikehire"))
s.sync(once=True)mfd sync --pipeline bikehire --onceSync reads the source and writes each batch to Mfd's write-ahead log (WAL) first. A crash retries that committed batch; BigQuery applies it once. It then creates the mfd_quickstart dataset and the bikehire table if they do not exist, and inserts the rows with BigQuery jobs. When new fields appear later, Mfd adds the columns before loading.
--once (once=True) runs a single pass and exits. Without it, sync keeps running and checks the source for new data on a schedule.
Check it worked
Count the rows and look at a sample:
bq query --use_legacy_sql=false \
"SELECT COUNT(*) AS row_count FROM \`$GCP_PROJECT.mfd_quickstart.bikehire\`"
bq query --use_legacy_sql=false \
"SELECT * FROM \`$GCP_PROJECT.mfd_quickstart.bikehire\` LIMIT 10"You should see a non-zero row_count, and columns that match the fields mfd schema printed in step 3. The table is named after the pipeline, in lower case. You can run the same SQL in the BigQuery console.
Troubleshooting
BigQuery requires a non-empty credentials_path in plugin config—credentials_pathis missing, orGOOGLE_APPLICATION_CREDENTIALSwas empty when the pipeline started. Export it in the shell running Mfd, or add it to a.envfile next tomfd.yml.mfd.yml references ${GOOGLE_APPLICATION_CREDENTIALS} ... but that environment variable is not set— same cause. Export the variable and rerun.- Mfd cannot read the key file — the path is wrong or the file is not readable by the user running Mfd. Check with
ls -l "$GOOGLE_APPLICATION_CREDENTIALS". Access Deniedon the project, dataset, or a job — the service account is missing a role. Grant BigQuery Data Editor and BigQuery Job User as in step 1.- A location error, or
Not found: Dataset— the dataset exists in a different location fromlocation. Setlocationto the dataset's location, or use a new dataset name. AccessDeniedreading the sample bucket — your AWS credentials are missing or lacks3:GetObjectands3:ListBucket. Runaws sts get-caller-identityto see which principal Mfd uses.
For more detail, rerun the failing command with --log debug, or see Troubleshooting.
Next steps
- BigQuery destination — every option and the type mapping.
- S3 source — point the pipeline at your own bucket.
- Schema discovery and evolution — how types are inferred and how new fields are added.
- How Mfd works — what happens between source and destination.
