简体   繁体   中英

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. But is there a way to fetch the schema directly instead of defining it as 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 .

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.

According to the documentation you can specify a JSON schema file like this:

[
 {
   "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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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