【问题标题】:How to load JSON file to google Bigquery using node.js如何使用 node.js 将 JSON 文件加载到谷歌 Bigquery
【发布时间】:2021-03-05 16:03:24
【问题描述】:

我正在使用 fetch 函数获取附加的 JSON 对象,并使用我的 node.js 后端使用以下代码将此 JSON 数据加载到 Bigquery:

await bigquery
      .dataset(datasetId)
      .table(tableId).insert(JSON_obj)

但不幸的是得到以下错误:

Unhandled rejection PartialFailureError: A failure occurred during this request

JSON 对象

{
  "totalResults": 418,
  "profileInfo": {
    "profileId": "104881487",
    "profileName": "All Mobile App Data",
    "accountId": "64812694",
    "tableId": "ga:105536427",
    "internalWebPropertyId": "100521715",
    "webPropertyId": "UA-648333494-1"
  },
  "totalsForAllResults": {
    "ga:users": "427",
    "ga:totalEvents": "682",
    "ga:eventValue": "0"
  },
  "query": {
    "max-results": 1000,
    "start-index": 1,
    "start-date": "today",
    "end-date": "today",
    "dimensions": "ga:eventCategory,ga:eventAction,ga:eventLabel,ga:dateHourMinute",
    "metrics": [
      "ga:users",
      "ga:totalEvents",
      "ga:eventValue"
    ],
    "ids": "ga:104831427",
    "sort": [
      "-ga:totalEvents"
    ]
  },
  "selfLink": "https://www.googleapis.com/analytics/v3/data/ga?ids=ga:10483467&dimensions=ga:eventCategory,ga:eventAction,ga:eventLabel,ga:dateHourMinute&metrics=ga:users,ga:totalEvents,ga:eventValue&sort=-ga:totalEvents&start-date=today&end-date=today",
  "columnHeaders": [
    {
      "name": "ga:eventCategory",
      "columnType": "DIMENSION",
      "dataType": "STRING"
    },
    {
      "name": "ga:eventAction",
      "columnType": "DIMENSION",
      "dataType": "STRING"
    },
    {
      "name": "ga:eventLabel",
      "columnType": "DIMENSION",
      "dataType": "STRING"
    },
    {
      "name": "ga:dateHourMinute",
      "columnType": "DIMENSION",
      "dataType": "STRING"
    },
    {
      "name": "ga:users",
      "columnType": "METRIC",
      "dataType": "INTEGER"
    },
    {
      "name": "ga:totalEvents",
      "columnType": "METRIC",
      "dataType": "INTEGER"
    },
    {
      "name": "ga:eventValue",
      "columnType": "METRIC",
      "dataType": "INTEGER"
    }
  ],
  "containsSampledData": false,
  "id": "https://www.googleapis.com/analytics/v3/data/ga?ids=ga:104831427&dimensions=ga:eventCategory,ga:eventAction,ga:eventLabel,ga:dateHourMinute&metrics=ga:users,ga:totalEvents,ga:eventValue&sort=-ga:totalEvents&start-date=today&end-date=today",
  "itemsPerPage": 1000,
  "kind": "analytics#gaData",
  "rows": [
    [
      "video_screen",
      "click_on_screen",
      "false",
      "202011190517",
      "1",
      "32",
      "0"
    ],
    [
      "video_screen",
      "click_on_screen",
      "false",
      "202011190730",
      "1",
      "17",
      "0"
    ],
    ...

【问题讨论】:

  • 我们的表的架构是什么?您真的要存储整个 JSON 吗?
  • 没错,这是架构: eventCategory STRING NULLABLE eventAction STRING NULLABLE eventLabel STRING NULLABLE dateHourMinute STRING NULLABLE users INTEGER NULLABLE totalEvents INTEGER NULLABLE eventValue FLOAT NULLABLE

标签: node.js google-cloud-platform google-bigquery google-cloud-storage


【解决方案1】:

当您将 JSON 提交到 BigQuery 插入表函数时,您只需提供所需的数据。在这里,您提供了一个很大的 JSON,而库需要猜测数据才能进入其中。

我个人猜测在rows 数组中,但我不确定。而且我也不确定字段顺序!!

因此,从您的 JSON 中提取有用的数据,根据需要对其进行格式化(CSV、JSON、...)并将其提交给 BigQuery。效果会更好!

【讨论】:

  • 我尝试更改它并像这样插入它:JSON_obj.rows 但它仍然无法正常工作,await bigquery .dataset(datasetId) .table(tableId).insert(JSON_obj.rows)
  • 对于作为 JSON 文件上传,我需要存储对象、获取路径并使用加载而不是插入(流),但由于服务器问题,我无法上传 JSON,所以我试图让它与众不同。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-07
  • 1970-01-01
  • 2017-02-10
  • 2020-03-29
  • 1970-01-01
  • 1970-01-01
  • 2014-12-10
相关资源
最近更新 更多