我如何使用BigQuery补丁? [英] How do I use BigQuery patch?

查看:121
本文介绍了我如何使用BigQuery补丁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在BigQuery API文档中有一个名为patch的方法。我希望我可以使用它来改变现有表的模式。不幸的是它不被bq支持。但根据他们的网站,您可以在 https://开发人员处进行试用。 google.com/bigquery/docs/reference/v2/tables/patch 。然而,当我尝试它发送以下请求:

 修补程序https://www.googleapis.com/bigquery/v2/projects /(我的项目ID)/ datasets / tmp_bt / tables / change_cols?key = {YOUR_API_KEY} 

Content-Type:application / json
Authorization:Bearer(removed)
X -JavaScript-User-Agent:Google APIs Explorer

{
schema:{
fields:[
{
},
$ b {
mode:nullable,
name:gotchahere,
type:string
$ b b




不知道空元素是从哪里来的,编辑器太难使用,只能粘贴到我现有的表格定义中。我注意到它缺少必需的元素,比如我的项目ID,我期望将它包含在内,因为它们是必需的然后我得到响应:

  cache-control:private,max-age = 0 
内容编码:gzip
content-len gth:122
content-type:application / json; charset = UTF-8
日期:2013年6月13日星期四22:22:09 GMT
过期时间:2013年6月13日星期四22:22:09 GMT
服务器:GSE

{
error:{
errors:[
{
domain:global,
reason:backendError ,
message:后端错误
}
],
code:503,
message:Backend Error
}
}

完全没用。我做了一个网络搜索,并没有找到任何使用它的例子。



任何人都可以给我一个使用BigQuery补丁修改表的例子,描述它实际上可以做什么?

解决方案

TLDR :您需要提供完整的模式在补丁请求的主体中,而不仅仅是您要添加的字段。后端错误可能是由该数组中的空字段造成的。




BigQuery的API允许表格(和其他资源)有两种更新方式:更新和补丁。



update 方法用您提供的新资源替换表资源。如果您想要获取现有表资源并对其进行修改,然后将修改后的表资源完整发回BigQuery,则此方法非常有用。 (但是,请注意,对象的某些字段(如creationTime)被认为是不可变的,因此为这些字段提供的新值将被忽略。)



<一个href =https://developers.google.com/bigquery/docs/reference/v2/tables/patch>补丁方法只会取代您在请求中包含的字段,并将其余的资源不变。如果您想对一个字段进行单独更改而不用担心其他字段,则此方法非常有用。此算法递归应用于任何嵌套对象,但嵌套数组。换句话说,通过请求发送的修补程序资源将与现有资源递归合并,直到遇到数组或标量值,此时修补程序对象中的数组或值将替换现有资源中的数组或值。由于您尝试更新的模式包含一组字段,并且由于修补程序方法会批量更新数组,所以您的修补程序对象需要包含完整的字段数组想要在最终的模式。 (您不能通过在修补程序对象中指定一个字段数组来添加字段。)

请注意,表ID在请求URL中给出,所以它不需要包含在对象本身中。



最后,后端错误对我们来说是一个问题,可能是由于您的两个空的模式字段请求。我们会进一步挖掘并希望改进未来的错误消息。


In the BigQuery API documentation there is a method called patch. I am hoping that I can use it to alter the schema of an existing table. Unfortunately it is not supported by bq. But according to their website, you can try it at https://developers.google.com/bigquery/docs/reference/v2/tables/patch. However when I try it sends the following request:

PATCH https://www.googleapis.com/bigquery/v2/projects/(my project id)/datasets/tmp_bt/tables/change_cols?key={YOUR_API_KEY}

Content-Type:  application/json
Authorization:  Bearer (removed)
X-JavaScript-User-Agent:  Google APIs Explorer

{
 "schema": {
  "fields": [
   {
   },
   {
   },
   {
    "mode": "nullable",
    "name": "gotchahere",
    "type": "string"
   }
  ]
 }
}

(I have no idea where the empty elements came from, and the editor is too painful to use to just paste in my existing table definition. I note that it is missing required elements like my project ID, which I expected to be included because they were required in the form) and then I get the response:

cache-control:  private, max-age=0
content-encoding:  gzip
content-length:  122
content-type:  application/json; charset=UTF-8
date:  Thu, 13 Jun 2013 22:22:09 GMT
expires:  Thu, 13 Jun 2013 22:22:09 GMT
server:  GSE

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "backendError",
    "message": "Backend Error"
   }
  ],
  "code": 503,
  "message": "Backend Error"
 }
}

which is utterly useless. I've done a web search, and failed to find any examples of it in use.

Can anyone give me an example of using BigQuery patch to alter a table, and a description of what it can actually do?

解决方案

TLDR: You need to supply the complete schema in the body of the patch request, not just the fields you're trying to add. The backend error is likely caused by the empty fields in that array.


BigQuery's API allows tables (and other resources) to be updated in two ways: update and patch.

The update method replaces the table resource with the new resource that you supply. This method is useful in cases where you want to take an existing table resource, modify it, and then post that modified table resource back to BigQuery in its entirety. (Note, however, that some fields of the object, such as creationTime, are considered immutable, so the new values supplied for these fields will be ignored.)

The patch method only replaces the fields that you include in the request, and leaves the rest of the resource unchanged. This method is useful if you want to make an isolated change to one field without worrying about the rest. This algorithm is applied recursively to any nested objects, but not to nested arrays. In other words, the patch resource that you send with the request is merged recursively with the existing resource until an array or scalar value is encountered, at which point the array or value in the patch object replaces the one in the existing resource.

Since the schema you're trying to update contains an array of fields, and since the patch method updates arrays wholesale, your patch object needs to contain the complete array of fields that you want in the resulting schema. (You can't add a field by specifying a one-field array in the patch object.)

Note that the table ID is given in the request URL, so it doesn't need to be included in the object itself.

Finally, the backend error is a problem on our end, likely a result of the two empty schema fields in your request. We'll dig further and hopefully improve the error message for the future.

这篇关于我如何使用BigQuery补丁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆