首页 > 解决方案 > C# 如何将 JSON 插入 Google BigQuery 表架构

问题描述

我有一个复杂的 JSON 对象数组,其中包含 JSON 对象的嵌入式数组。举个例子,一个 JSON 对象可能看起来像……

{
    "shopID": "12194",
    "name": "Shop Name",
    "timeZone": "US\/Eastern",
    "timeStamp": "2020-11-30T13:25:13+00:00",
    "Contact": {
        "contactID": "90",
        "Addresses": [
            "ContactAddress": {
                "address1": "123 Some Street",
                "address2": "",
                "city": "A city",
                "state": "A state",
                "zip": "12345",
                "country": "United States",
                "countryCode": "US"
            }
        ],
        "Emails": "",
        "Websites": {
            "ContactWebsite": {
                "url": "www.companyname.com"
            }
        }
    },
    "TaxCategory": {
        "taxCategoryID": "2",
        "isTaxInclusive": "false",
        "tax1Name": "Tax region name",
        "tax1Rate": "0.07"
    }
}

我无法修改上述格式...我正在调用一个将其作为响应返回的公共 API。

假设已在 Google BigQuery 中创建了相应的表架构,我如何简单地将这个 JSON 作为一行插入到表中?

我的代码看起来像这样......

bqc = BigQueryClient.Create("a-project-id");
var trShops = new TableReference()
{
    ProjectId = "a-project-id",
    DatasetId = "a-dataset-name",
    TableId = "Shops"
};

# Call to my API data source that returns a JSON array of Shops...
content = await _lsrApiHelper.GetRawDataAsync(uri, currentOffset, _apiLimit);

# After call to API I deserialize the JSON response...
response = JsonSerializer.Deserialize<ShopResponseRoot>(content); 

# Then I want loop through Shops in the List<ShopResponse> and insert them into BigQuery...
foreach (ShopResponse shop in response.Shop)
{
    var newRow = new BigQueryInsertRow();
    newRow.InsertId = "shopID";
    newRow.Add(shop);              # This is where I am struggling
    bqc.InsertRow(trShops, newRow);
}

我正在努力研究如何使用 JSON 或 C# 对象来填充我想要插入 BigQuery 的数据。

非常感谢任何和所有指导。

标签: c#jsongoogle-bigqueryinsert

解决方案


推荐阅读