Created
August 21, 2022 20:14
-
-
Save iht/2a952c9a39934db0688a5e46aef9f43f to your computer and use it in GitHub Desktop.
BigQuery JSON file to Beam Schema
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dev.herraiz.beam.schemas; | |
import com.fasterxml.jackson.databind.JsonNode; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.google.api.client.googleapis.util.Utils; | |
import com.google.api.client.json.JsonFactory; | |
import com.google.api.services.bigquery.model.TableSchema; | |
import org.apache.beam.sdk.io.gcp.bigquery.BigQueryUtils; | |
import org.apache.beam.sdk.schemas.Schema; | |
public class JsonSchemaParser { | |
private static final String ROOT_NODE_PATH = "schema"; | |
public static Schema bqJson2BeamSchema(String schemaAsString) throws Exception { | |
JsonFactory defaultJsonFactory = Utils.getDefaultJsonFactory(); | |
ObjectMapper mapper = new ObjectMapper(); | |
JsonNode topNode = mapper.readTree(schemaAsString); | |
JsonNode schemaRootNode = topNode.path(ROOT_NODE_PATH); | |
if (schemaRootNode.isMissingNode()) { | |
throw new Exception( | |
"Is this a BQ schema? The given schema must have a top node of name " + ROOT_NODE_PATH); | |
} | |
TableSchema tableSchema = | |
defaultJsonFactory.fromString(schemaRootNode.toString(), TableSchema.class); | |
return BigQueryUtils.fromTableSchema(tableSchema); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For more details, see https://medium.com/google-cloud/how-to-get-a-beam-schema-from-a-bigquery-schema-json-file-e848a4939f41