【问题标题】:Node JS - Big Query update table with a request objectNode JS - 带有请求对象的大查询更新表
【发布时间】:2022-01-04 11:55:59
【问题描述】:

根据下面的请求对象我需要更新表格

var reqBody = {
   "Name":"testing11",
   "columns":[
      {
         "fieldExistsIn":"BOTH",
         "columnWidth":5,
         "hide":false
      },
      {
         "fieldExistsIn":"BOTH",
         "columnWidth":10,
         "hide":false
      }
   ],
   "Range":{
      "startDate":"20-Oct-2022",
      "endDate":"26-Oct-2022"
   }
}


UPDATE table_name
SET requestData = reqBody
WHERE requestData.Name = reqBody.oldName;

我正在使用以下查询进行插入

await bigquery
    .dataset(datasetId)
    .table(tableId)
    .insert(reqBody);

对于表架构,您可以参考以下问题

Node JS - Big Query insert to a request object fully into a record data type

【问题讨论】:

  • 您要对哪一列执行更新? where条件是什么?
  • 在链接的问题中,您将“名称”列设置为“重复”类型,这意味着它是一个数组。你真的需要它是一个数组,就像你想要检查名称相等的条件一样吗?否则,每一行都会有多个名字。
  • 哦,没有名字不是重复字段
  • 嗨@user1187,如果我的回答解决了您的问题,请考虑接受并投票。如果没有,请告诉我,以便我改进答案。

标签: node.js google-bigquery


【解决方案1】:

根据您提供的表架构和示例数据,我尝试将其复制。

表架构:

根据您的要求,我已参考 Google BigQuery Node.js 客户端 API 中的 queryParamsStructsquery 示例代码修改了代码。要通过 BigQuery 客户端 API 使用 JSON 对象更新表中的多个列,我们必须编写 UPDATE 查询并将其传递到代码中。 JSON 对象应该在 params 中传递,并且您应该必须在 UPDATE 查询中访问该 JSON 对象,如下所示:

const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
async function query() {
   // Queries the U.S. given names dataset for the state of Texas.

   const query = `UPDATE
     \`ProjectID.DatasetID.TableID\`
       SET reqData.columns = ARRAY(
       SELECT AS STRUCT * FROM UNNEST(@reqData.columns)
       ),
       reqData.Range.startDate = CAST(@reqData.Range.startDate AS DATE),
       reqData.Range.endDate = CAST(@reqData.Range.endDate AS DATE)
       WHERE reqData.Name = @reqData.Name`;
   // For all options, see https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/query
   const options = {
     query: query,
     // Location must match that of the dataset(s) referenced in the query.
     location: 'US',
     params: {
           "reqData": {
             "Name": "testing",
             "columns": [
               {
                 "fieldExistsIn": "One",
                 "columnWidth": 1,
                 "hide": true
               },
               {
                 "fieldExistsIn": "four",
                 "columnWidth": 4,
                 "hide": true
               }
             ],
             "Range":{
               "startDate": "2021-1-11",
               "endDate": "2022-2-22"
            }
           }
       },
   };
   // Run the query as a job
   const [job] = await bigquery.createQueryJob(options);
   console.log(`Job ${job.id} started.`);

   // Wait for the query to finish
   const [rows] = await job.getQueryResults();

   // Print the results
   console.log('Rows:');
   rows.forEach(row => console.log(row));
 }
 // [END bigquery_query]
 query();

初始数据:

更新结果:

注意:如果您最近使用流式插入将行插入到表中,则在最近 30 分钟内无法使用 UPDATE、DELETE 或 MERGE 修改行。有关详细信息,请参阅此 limitations 文档。如果您尝试在过去 30 分钟内更新表,您将收到以下错误:

UnhandledPromiseRejectionWarning: Error: UPDATE or DELETE statement over table projectID.datasetID.tableID would affect rows in the streaming buffer, which is not supported

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-10
    • 1970-01-01
    • 1970-01-01
    • 2020-08-13
    • 2016-10-05
    • 2014-03-14
    • 1970-01-01
    相关资源
    最近更新 更多