简体   繁体   English

BigQuery - 从 Cloud Run 中的 Go 服务器获取表数据

[英]BigQuery - Fetch table data from Go Server in Cloud Run

I am able to fetch the Big Query table output as json through Golang server.我能够通过 Golang 服务器获取大查询表 output 作为 json。 But is there a way to fetch the schema directly instead of defining it as ColStatsRow ?但是有没有办法直接获取模式而不是将其定义为ColStatsRow Also, any way to make this better.此外,任何使这更好的方法。

type ColStatsRow struct {
        COLUMN_NAME       string `bigquery:"column_name"`
        COLUMN_VALUE      string `bigquery:"column_value"`
        FREQUENCY int64  `bigquery:"frequency"`
}

// getOutput prints results from a query 
func getOutput(w http.ResponseWriter, iter *bigquery.RowIterator) error {
        var rows []ColStatsRow
        for {
                var row ColStatsRow
                err := iter.Next(&row)
                if err == iterator.Done {
                        out, err := json.Marshal(rows)
                        if err != nil {
                            return fmt.Errorf("error marshalling results: %v", err)
                        }

                        w.Write([]byte(out))
                        return nil
                }
                if err != nil {
                        return fmt.Errorf("error iterating through results: %v", err)
                }

                rows = append(rows, row)
        }
}

Thank you.谢谢你。

If you're after the schema for the result, it's available on the RowIterator .如果您追求结果的架构,则可以在RowIterator上使用它。

If you mean you want to more dynamically process the rows without a specific struct, usually some combination of checking the schema and/or leveraging a type switch is the way to go about this.如果您的意思是您想要更动态地处理没有特定结构的行,通常检查架构和/或利用类型开关的某种组合是 go 关于此的方法。

According to the documentation you can specify a JSON schema file like this:根据文档,您可以像这样指定JSON schema file

[
 {
   "description": "[DESCRIPTION]",
   "name": "[NAME]",
   "type": "[TYPE]",
   "mode": "[MODE]"
 },
 {
   "description": "[DESCRIPTION]",
   "name": "[NAME]",
   "type": "[TYPE]",
   "mode": "[MODE]"
 }
]

and then you can write this schema using the following command:然后您可以使用以下命令编写此架构:

bq show --schema --format=prettyjson project_id:dataset.table > path_to_file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM