简体   繁体   English

如何使用 node.js 将 JSON 文件加载到 google Bigquery

[英]How to load JSON file to google Bigquery using node.js

I'm using fetch function for getting the attached JSON object, and using my node.js backend to load this JSON data to Bigquery using the following code:我正在使用 fetch 函数获取附加的 JSON 对象,并使用我的 node.js 后端使用以下代码将此 JSON 数据加载到 Bigquery:

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

But unfortunately getting the following error :但不幸的是收到以下错误:

Unhandled rejection PartialFailureError: A failure occurred during this request

JSON OBJ 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"
    ],
    ...

When you submit a JSON to BigQuery Insert to table function, you need to provide only the required data.当您将 JSON 提交给 BigQuery 插入到表函数时,您只需提供所需的数据。 Here you provide a big big JSON and the library need to guess the data to get in it.在这里,您提供了一个很大的 JSON,库需要猜测数据才能进入其中。

Personally I guess in the rows array, but I'm not sure.我个人猜测rows数组中,但我不确定。 And I'm also not sure about the field order!!而且我也不确定现场顺序!!

So, extract the useful data from your JSON, format them as you want (CSV, JSON,...) and submit them to BigQuery.因此,从您的 JSON 中提取有用的数据,根据需要对其进行格式化(CSV、JSON 等)并将它们提交给 BigQuery。 It will work better!它会更好地工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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