如何通过HTTP API

时间:2017-04-05 21:21:15

标签: google-bigquery

我正在为F#的类型提供程序工作并实现它我们需要获取查询编译时的模式,这意味着它必须很快。截至目前我们运行的是这样的:

bq query --format=json --dry_run=true --use_legacy_sql=false 'SELECT @a IS TRUE AS x, @b + 1 AS y, "foo" = @c AS z, ["tomas", "jansson"] as w, STRUCT("wat" as t, 69 as u) as v, [STRUCT(3, "allo" as g), STRUCT(5 as a, "yolo")] as u, STRUCT(["a"] as h) as t;'

它完全符合我们的要求,但它使用bq工具。我想知道底层的http调用是什么,我在代码库中找不到。

我想知道底层代码库的原因是因为我想删除尽可能多的第三方依赖项,这些依赖项可能在构建服务器上不可用或需要很长时间才能设置。

2 个答案:

答案 0 :(得分:1)

编辑:米哈伊尔是对的;只有jobs.insert API提供架构信息。我将使用这个答案来记录当前的行为。

使用jobs.insert API,这是一个示例请求和响应:

PROJECT="YOUR_PROJECT_NAME"
QUERY="\"SELECT 1 AS x, 'foo' AS y, @b IS FALSE AS z;\""
REQUEST="{\"configuration\":{\"dryRun\":true,\"query\":{\"useLegacySql\":false,\"query\":${QUERY}}}}"
echo $REQUEST | \
curl -X POST -d @- -H "Content-Type: application/json" \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    https://www.googleapis.com/bigquery/v2/projects/$PROJECT/jobs

(some parts omitted)
"statistics": {
 "query": {
  "schema": {
   "fields": [
    {
     "name": "x",
     "type": "INTEGER",
     "mode": "NULLABLE"
    },
    {
     "name": "y",
     "type": "STRING",
     "mode": "NULLABLE"
    },
    {
     "name": "z",
     "type": "BOOLEAN",
     "mode": "NULLABLE"
    }
   ]
  },
  "undeclaredQueryParameters": [
   {
    "name": "b",
    "parameterType": {
     "type": "BOOL"
    }
   }
  ],
  "statementType": "SELECT"
 }

以下是使用jobs.query的示例请求和响应,以便进行比较:

PROJECT="YOUR_PROJECT_NAME"
QUERY="\"SELECT 1 AS x, 'foo' AS y, @b IS FALSE AS z;\""
REQUEST="{\"kind\":\"bigquery#queryRequest\",\"dryRun\":true,\"useLegacySql\":false,\"query\":$QUERY}"
echo $REQUEST | \
  curl -X POST -d @- -H "Content-Type: application/json" \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    https://www.googleapis.com/bigquery/v2/projects/$PROJECT/queries

{
 "kind": "bigquery#queryResponse",
 "jobReference": {
  "projectId": "<omitted>"
 },
 "totalBytesProcessed": "0",
 "jobComplete": true,
 "cacheHit": false
}

答案 1 :(得分:1)

或者您可以使用

Jobs: insert query配置属性,dryRun设置为true