dqr3883 2019-05-02 11:48
浏览 53
已采纳

在没有明确定义的结构的情况下插入BigQuery

I'd like to insert any type of JSON directly into BigQuery, but haven't found any good way of doing this? All ways assume I have a well defined struct that I insert. It seems it was possible with the old deprecated api but not with the new "cloud.google.com/go/bigquery" package.

I'd like to have /api/table_name/insert be able to take any type of json and insert it into BigQuery as both the client and server knows the schema the endpoint should just forward it.

Thanks

  • 写回答

1条回答 默认 最新

  • duanshang9426 2019-06-15 20:48
    关注

    seems like this was replaced with ValueSaver which you can implement like:

    type genericRecord map[string]bigquery.Value
    
    func (rec genericRecord) Save() (map[string]bigquery.Value, string, error) {
        insertID := uuid.New().String()
        return rec, insertID, nil
    }
    
    var data []*genericRecord
    json.Unmarshal(<YOUR JSON BYTE>, &data)
    
    ctx := context.Background()
    client, err := bigquery.NewClient(ctx, <YOUR PROJECT ID>)
    ins := client.Dataset(<DATASET>).Table(<TABLE>).Inserter()
    ins.Put(ctx, data)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据