Sreekanth
Sreekanth

Reputation: 13

BQ Insert to record type successful not data not inserted

I'm trying to insert data into Bigquery following the example at text with pre-created table as against the given example. My code is as follows

type tagInfo struct {
    proj_name string `bigquery:"proj_name"`
    Tags      Tags   `bigquery:"tags"`
}
type Tags struct {
    key    string `bigquery:"key"`
    values string `bigquery:"values"`
}
func insertData() error {
    prjName := "prjName"
    datasetID := "DSName"
    tableID := "TName"

    ctx := context.Background()
    client, err := bigquery.NewClient(ctx, prjName)
    if err != nil {
        return fmt.Errorf("Bigquery.NewClient: %w", err)
    }
    defer client.Close()

    item := &tagInfo{
        proj_name: "Devil",
        Tags: Tags{
            key:    "engcontact",
            values: "Meryl Streep",
        },
    }
    fmt.Printf("Item is: %s", item)
    items := []*tagInfo{item}

    table := client.Dataset(datasetID).Table(tableID)
    inserter := table.Inserter()
    err = inserter.Put(ctx, items)

    if err != nil {
        if multiErr, ok := err.(bigquery.PutMultiError); ok {
            for _, putErr := range multiErr {
                fmt.Printf("failed to insert row %d with err: %v \n", putErr.RowIndex, putErr.Error())
                fmt.Println(putErr.Errors)
            }
        }
        return err
    }
    return nil
}

The code runs successfully, but I do not see any records inserted. Table structure is as follows

Field name Type Mod Key Collation Default Value Policy Tags
proj_name STRING NULLABLE
tags. RECORD NULLABLE
key. STRING NULLABLE values. STRING NULLABLE

Not sure what/where is it going wrong, if someone can provide a pointer, hugely appreciated.

TIA Sreekanth

Expecting the records to be inserted successfully in table.

Upvotes: 1

Views: 335

Answers (1)

Zeke Lu
Zeke Lu

Reputation: 7485

The fields should be exported. These kind of issues are reported too many times:

type tagInfo struct {
    ProjName string `bigquery:"proj_name"`
    Tags     Tags   `bigquery:"tags"`
}
type Tags struct {
    Key    string `bigquery:"key"`
    Values string `bigquery:"values"`
}

See the doc for (*Inserter).Put:

...

If src is ValueSaver, then its Save method is called to produce a row for uploading.

If src is a struct or pointer to a struct, then a schema is inferred from it and used to create a StructSaver. The InsertID of the StructSaver will be empty.

...

And here is the comment for the inferFields func:

// inferFields extracts all exported field types from struct type.
func inferFields(rt reflect.Type) (Schema, error) {

Upvotes: 0

Related Questions