Dan Dinu
Dan Dinu

Reputation: 33438

Golang BigQuery: Insert array into table

I'm trying to insert an array into a table using the bigquery golang library.

I have a table schema that looks like this. The array is the quality column, has the Repeated.

    tableSchema := bigquery.Schema{
        {Name: "issue_id", Type: bigquery.StringFieldType},
        {Name: "quality", Type: bigquery.StringFieldType, Repeated: true},
        {Name: "creation_date_ts", Type: bigquery.TimestampFieldType},
    }

This is how I defined the record:

type escalationsRecord struct {
    IssueID      bigquery.NullString    `bigquery:"issue_id"`
    Quality      []string               `bigquery:"quality"`
    CreationTime bigquery.NullTimestamp `bigquery:"creation_date_ts"`
}

This is how I create a record:

    record := escalationsRecord{
            IssueID:      bigquery.NullString{StringVal: fmt.Sprint(int(issue.Number)), Valid: true},
            Quality:      qualityArray,
            CreationTime: bigquery.NullTimestamp{Timestamp: issue.CreatedAt.Time, Valid: true},
        }
records = append(records, &record) 

This is how I put the records to BigQuery

inserter := table.Inserter()
err2 := inserter.Put(b.ctx, records)

The quality column is NULL when I look at it in bigqyery. There are no errors. The array contains elements. Any idea how to properly insert arrays? I can't find anything in the docs.

Upvotes: 1

Views: 840

Answers (1)

Dan Dinu
Dan Dinu

Reputation: 33438

What @BrianWagner suggested in his comment works. The only change I did was to use the InferSchema() instead of defining it myself.

So instead of

  tableSchema := bigquery.Schema{
        {Name: "issue_id", Type: bigquery.StringFieldType},
        {Name: "quality", Type: bigquery.StringFieldType, Repeated: true},
        {Name: "creation_date_ts", Type: bigquery.TimestampFieldType},
    }

I now have

    tableSchema, err := bigquery.InferSchema(escalationsRecord{})
    if err != nil {
        log.Fatal(err)
    }

Upvotes: 2

Related Questions