BQ插入到记录类型成功,但没有插入数据。

huangapple go评论79阅读模式
英文:

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

这些字段应该被导出。这类问题已经被报告了很多次:

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:

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) {

huangapple
  • 本文由 发表于 2023年5月23日 20:49:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76314940.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定