Skip to content

Converts avro schema (supports schema with array/record types) to bq schema which can be used to create bq table

License

Notifications You must be signed in to change notification settings

go-syar/avro-schema-bq

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Reference

avro-schema-bq

Convert Apache Avro schema (it supports schemas with array/record types) to BigQuery Table Schema.

go install github.com/go-syar/avro-schema-bq@latest

Usage

avro-bq-schema schema.avsc > bq.json

Create BQ Table with Avro Schema (avsc)

Create BQ Table with Avro schema by providing variables projectID, datasetID, tableID, serviceAccount, schemaFilePath

table.CreateBQTableWithSA(projectID string, datasetID string, tableID string, serviceAccount string, schemaFilePath string) error
// service account := "service-account.json"

Avro Schema (avsc) to BQ Schema (json)

schema.ConvertAvroToBigQuery(avroSchema map[string]interface{}) ([]*bigquery.FieldSchema, error)

Convert .avsc file to map[string]interface{}

	schemaFilePath := $ your-(.avsc)file-path

	avroSchemaContent, err := ioutil.ReadFile(schemaFilePath)
	if err != nil {
		fmt.Println("Error reading Avro schema file:", err)
		return
	}

	var avroSchema map[string]interface{}
	err = json.Unmarshal(avroSchemaContent, &avroSchema)
	if err != nil {
		fmt.Println("Error parsing Avro schema:", err)
		return
	}

Convert the BigQuery schema to JSON

	jsonData, err := json.MarshalIndent(bqFields, "", "    ")
	if err != nil {
		fmt.Println("Error marshaling BigQuery schema to JSON:", err)
		return
	}

Write the JSON BigQuery schema output to a file

	err = ioutil.WriteFile("schema/test_data/bq_schema.json", jsonData, 0644)
	if err != nil {
		fmt.Println("Error writing JSON data to file:", err)
		return
	}