英文:
BQ Insert to record type successful not data not inserted
问题
我正在尝试按照给定示例以预先创建的表格的方式将数据插入到BigQuery中。我的代码如下:
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
}
代码成功运行,但是我没有看到任何记录被插入。表格结构如下:
字段名 类型 修饰符 键 排序规则 默认值 策略 Tags
proj_name STRING NULLABLE
tags RECORD NULLABLE
key STRING NULLABLE
values STRING NULLABLE
不确定出了什么问题,如果有人能提供指导,将不胜感激。
期望成功将记录插入表格中。
英文:
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.
答案1
得分: 0
这些字段应该被导出。这类问题已经被报告了很多次:
- JSON: https://stackoverflow.com/questions/26327391/json-marshalstruct-returns
- BSON: https://stackoverflow.com/questions/52006453/inserting-struct-in-mongodb-in-golang
- YAML: https://stackoverflow.com/questions/29544146/go-unmarshaling-yaml-into-struct
- TOML: https://stackoverflow.com/questions/43218484/not-able-to-read-toml-file-using-go-with-burntsushi-library
type tagInfo struct {
ProjName string `bigquery:"proj_name"`
Tags Tags `bigquery:"tags"`
}
type Tags struct {
Key string `bigquery:"key"`
Values string `bigquery:"values"`
}
请参阅 (*Inserter).Put 的文档:
> ...
>
> 如果 src 是 ValueSaver,则调用其 Save 方法生成要上传的行。
>
> 如果 src 是结构体或指向结构体的指针,则从中推断出模式并用于创建 StructSaver。StructSaver 的 InsertID 将为空。
>
> ...
这是 inferFields 函数的注释:
// inferFields 从结构体类型中提取所有导出的字段类型。
func inferFields(rt reflect.Type) (Schema, error) {
英文:
The fields should be exported. These kind of issues are reported too many times:
- JSON: https://stackoverflow.com/questions/26327391/json-marshalstruct-returns
- BSON: https://stackoverflow.com/questions/52006453/inserting-struct-in-mongodb-in-golang
- YAML: https://stackoverflow.com/questions/29544146/go-unmarshaling-yaml-into-struct
- TOML: https://stackoverflow.com/questions/43218484/not-able-to-read-toml-file-using-go-with-burntsushi-library
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) {
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论