Home  >  Article  >  Backend Development  >  BQ inserts record type successfully, but no data is inserted

BQ inserts record type successfully, but no data is inserted

PHPz
PHPzforward
2024-02-09 15:40:181108browse

BQ 插入记录类型成功,未插入数据

php Editor Banana will introduce to you a common database operation problem, that is, the record type is successfully inserted into the BQ database but the actual data is not inserted. This situation may cause data inconsistency, so we need to find out the cause and solve the problem. In the following, we will detail the possible causes and solutions to help readers better deal with this problem.

Question content

I am trying to insert data into bigquery as per the following example Text with pre-created table compared to 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 don't see any records inserted. The table structure is as follows

Field name type mod key sorting rule default value policy label

proj_name string nullable

Label. Records can be empty

key. String can be empty values. String can be empty

Not sure what's going wrong, if anyone can provide guidance, thank you very much.

tia Srikanth

Expect records to be successfully inserted into the table.

Solution

Fields should be exported. This issue has been 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 documentation (*inserter).put:

...

If src is valuesaver, call its save method to generate a row for uploading.

If src is a structure or a pointer to a structure, the schema is inferred from it and used to create the structsaver. The insertid of structsaver will be empty.

...

This is a comment on

The above is the detailed content of BQ inserts record type successfully, but no data is inserted. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete