Kboyz
Kboyz

Reputation: 189

BigQuery change schema will affect the data?

I've exported the data from Datastore to Cloud Storage and loaded it to BigQuery. If I changed the order of the schema, would it affect the data?

Here's the table:

enter image description here

Code for updating schema:

func updateSchema(projectID, datasetID, tableID string) error {
    projectID := "my-project-id"
    datasetID := "mydataset"
    tableID := "mytable"
    ctx := context.Background()
    client, err := bigquery.NewClient(ctx, projectID)
    if err != nil {
        return fmt.Errorf("bigquery.NewClient: %v", err)
    }
    defer client.Close()

    tableRef := client.Dataset(datasetID).Table(tableID)
    meta, err := tableRef.Metadata(ctx)
    if err != nil {
        return err
    }
    newSchema := bigquery.Schema{
        {Name: "settlement", Type: bigquery.StringFieldType },
        {Name: "dismissal_id", Type: bigquery.IntegerFieldType },
        {Name: "brand_safe_flag", Type: bigquery.BooleanFieldType },
        {Name: "net_cv_cost", Type: bigquery.StringFieldType },
        {Name: "gross_cv_cost", Type: bigquery.StringFieldType },
        {Name: "non_achievement_message", Type: bigquery.StringFieldType },
        {Name: "partner_id", Type: bigquery.IntegerFieldType },
        {Name: "click_url", Type: bigquery.RecordFieldType, Schema: bigquery.Schema{
            {Name: "string", Type: bigquery.StringFieldType},
            {Name: "text", Type: bigquery.StringFieldType},
            {Name: "provided", Type: bigquery.StringFieldType},
        }},
    }
    update := bigquery.TableMetadataToUpdate{
        Schema: newSchema,
    }
    if _, err := tableRef.Update(ctx, update, meta.ETag); err != nil {
        return err
    }
    return nil
}

Upvotes: 0

Views: 271

Answers (1)

Illia
Illia

Reputation: 374

The only modifications supported for existing BigQuery Tables are:

  • Adding columns to a schema definition
  • Relaxing a column's mode from REQUIRED to NULLABLE

Please refer to https://cloud.google.com/bigquery/docs/managing-table-schemas. If you would like to perform a schema change out of the scope of these two operations you probably have to take a different approach. Consider migrating your data into a new table with a new Schema.

Upvotes: 1

Related Questions