Cleans a CSV inside GCS, using a Cloud Run job.
878
Cleans and normalizes CSV data from a mounted filesystem, writes the cleaned CSV to Google Cloud Storage, and emits a matching BigQuery schema JSON. Supports optional header transformations, SQL‑friendly header normalization, value removal, type validation or inference, head preview export, and a “nil transform” template.
cmd/cloudRunCsvCleaner/main.go (helpers in cmd/cloudRunCsvCleaner/helpers.go)Required:
clean/my.csv).clean/my.schema.json).Optional:
nil-transform.json next to SCHEMA_FILE, describing a no‑op transform mapping original headers to final headers with inferred/validated types.OUT_FILE + ".head.csv".expected-column-name (string): The original header token to look for.rename-to (string, optional): The desired final column name; defaults to expected-column-name if omitted.type (string): Expected data type for the column; accepted values are case‑insensitive and normalized to BigQuery primitives. Common values: INTEGER, FLOAT, BOOLEAN, STRING (aliases like NUMBER, DECIMAL, DOUBLE, BOOL are also handled).Example SCHEMA_TRANSFORM JSON:
[
{ "expected-column-name": "User Id", "rename-to": "user_id", "type": "INTEGER" },
{ "expected-column-name": "Active", "type": "BOOLEAN" },
{ "expected-column-name": "Score", "type": "FLOAT" },
{ "expected-column-name": "Notes", "type": "STRING" }
]
SCHEMA_TRANSFORM substring replacements, then parses the resulting header as CSV.SCHEMA_TRANSFORM is provided:
SCHEMA_TRANSFORM is not provided:
MAKE_COLUMN_NAMES_SQL_FRIENDLY).REMOVE_VALUES by replacing exact matches with empty strings before validation/inference.gs://BUCKET/OUT_FILE.{ "name", "type", "mode": "NULLABLE" }) and writes it to gs://BUCKET/SCHEMA_FILE.EXPORT_HEAD is true, writes a header + 5‑row preview to gs://BUCKET/<OUT_FILE>.head.csv.EXPORT_NIL_TRANSFORM is true, writes nil-transform.json next to the schema file, mapping original headers to final names and mapping BigQuery types back to transform‑style types (string, number, boolean).DELETE_IN_FILE is true, removes the local input file.task-id and dag-id and progress lines every 100,000 rows.docker run --rm \
-e TASK_ID=task-123 \
-e DAG_ID=dag-abc \
-e MOUNT_PATH=/data \
-e IN_FILE=landing/input.csv \
-e OUT_FILE=clean/output.csv \
-e SCHEMA_FILE=clean/output.schema.json \
-e BUCKET=my-bucket \
-e MAKE_COLUMN_NAMES_SQL_FRIENDLY=true \
-e EXPORT_HEAD=true \
-e REMOVE_VALUES="N/A,NULL" \
-v "$(pwd)/data:/data" \
-v "$GOOGLE_APPLICATION_CREDENTIALS:/var/secrets/google/key.json:ro" \
-e GOOGLE_APPLICATION_CREDENTIALS=/var/secrets/google/key.json \
sqlpipe/cloud-run-csv-cleaner:dev-<tag>
With an explicit transform:
docker run --rm \
-e TASK_ID=task-123 \
-e DAG_ID=dag-abc \
-e MOUNT_PATH=/data \
-e IN_FILE=landing/input.csv \
-e OUT_FILE=clean/output.csv \
-e SCHEMA_FILE=clean/output.schema.json \
-e BUCKET=my-bucket \
-e SCHEMA_TRANSFORM='[{"expected-column-name":"User Id","rename-to":"user_id","type":"INTEGER"},{"expected-column-name":"Active","type":"BOOLEAN"}]' \
-v "$(pwd)/data:/data" \
-v "$GOOGLE_APPLICATION_CREDENTIALS:/var/secrets/google/key.json:ro" \
-e GOOGLE_APPLICATION_CREDENTIALS=/var/secrets/google/key.json \
sqlpipe/cloud-run-csv-cleaner:dev-<tag>
gs://BUCKET/OUT_FILEgs://BUCKET/SCHEMA_FILEgs://BUCKET/<OUT_FILE>.head.csvgs://BUCKET/<dir-of-SCHEMA_FILE>/nil-transform.jsonSCHEMA_TRANSFORM is set, type validation is strict; any mismatch results in an immediate non‑zero exit.SCHEMA_TRANSFORM, type inference only considers primitive numeric and boolean categories; all others default to STRING.Content type
Image
Digest
sha256:3d22e0400…
Size
9.9 MB
Last updated
11 months ago
docker pull sqlpipe/cloud-run-csv-cleaner:3