正在将 JSON 个数组加载到 BigQuery 中
Loading JSON Array Into BigQuery
我正在尝试将 json 数组加载到 bigquery table 中。数据结构如下:
[{"image":"testimage1","component":"component1"},{"image":"testimage2","component":"component2"}]
每条 json 记录对应 BigQuery 中的 1 行。 BigQuery 中的列是:图像和组件。
当我尝试摄取数据时,它因解析错误而失败。
如果我尝试将结构更改为此,它会起作用
{"image":"testimage1","component":"component1"}{"image":"testimage2","component":"component2"}
我正在尝试摄取 NEWLINE_DELIMITED_JSON
有什么方法可以让第一个 json 结构被吸收到 Bigquery 中吗?
不,BigQuery 只能提取有效的 JSON,并且有效的 JSON 不以数组开头。
你要稍微改造一下:
- 要么将其转换为有效的 JSON(在开头添加一个
{"object":
并以 }
结束该行)。在临时 table 中摄取 JSON 并执行查询以扫描新的 table 并将正确的值插入目标 tables
- 或者删除数组定义
[]
并将 },{
替换为 }\n{
以获得 JSON 行。
或者,您可以将 JSON 提取为 CSV 文件(其中只有 1 列 JSON 原始文本),然后使用 BigQuery String 函数转换数据并将它们插入到目标数据库中。
您可以按照这种循环遍历列表并将其写入 json 文件的方法;然后将 json 文件加载到 BigQuery 中。
from google.cloud import bigquery
from google.oauth2 import service_account
import json
client = bigquery.Client(project="project-id")
dataset_id = "dataset-id"
table_id = "bqjson"
list_dict =[{"image":"testimage1","component":"component1"},{"image":"testimage2","component":"component2"}]
with open ("sample-json-data.json", "w") as jsonwrite:
for item in list_dict:
jsonwrite.write(json.dumps(item) + '\n') #newline delimited json file
dataset_ref = client.dataset(dataset_id)
table_ref = dataset_ref.table(table_id)
job_config = bigquery.LoadJobConfig()
job_config.source_format = bigquery.SourceFormat.NEWLINE_DELIMITED_JSON
job_config.autodetect = True
with open("sample-json-data.json", "rb") as source_file:
job = client.load_table_from_file(
source_file,
table_ref,
location="us", # Must match the destination dataset location.
job_config=job_config,
) # API request
job.result() # Waits for table load to complete.
print("Loaded {} rows into {}:{}.".format(job.output_rows, dataset_id, table_id))
输出:
我正在尝试将 json 数组加载到 bigquery table 中。数据结构如下:
[{"image":"testimage1","component":"component1"},{"image":"testimage2","component":"component2"}]
每条 json 记录对应 BigQuery 中的 1 行。 BigQuery 中的列是:图像和组件。 当我尝试摄取数据时,它因解析错误而失败。 如果我尝试将结构更改为此,它会起作用
{"image":"testimage1","component":"component1"}{"image":"testimage2","component":"component2"}
我正在尝试摄取 NEWLINE_DELIMITED_JSON
有什么方法可以让第一个 json 结构被吸收到 Bigquery 中吗?
不,BigQuery 只能提取有效的 JSON,并且有效的 JSON 不以数组开头。
你要稍微改造一下:
- 要么将其转换为有效的 JSON(在开头添加一个
{"object":
并以}
结束该行)。在临时 table 中摄取 JSON 并执行查询以扫描新的 table 并将正确的值插入目标 tables - 或者删除数组定义
[]
并将},{
替换为}\n{
以获得 JSON 行。
或者,您可以将 JSON 提取为 CSV 文件(其中只有 1 列 JSON 原始文本),然后使用 BigQuery String 函数转换数据并将它们插入到目标数据库中。
您可以按照这种循环遍历列表并将其写入 json 文件的方法;然后将 json 文件加载到 BigQuery 中。
from google.cloud import bigquery
from google.oauth2 import service_account
import json
client = bigquery.Client(project="project-id")
dataset_id = "dataset-id"
table_id = "bqjson"
list_dict =[{"image":"testimage1","component":"component1"},{"image":"testimage2","component":"component2"}]
with open ("sample-json-data.json", "w") as jsonwrite:
for item in list_dict:
jsonwrite.write(json.dumps(item) + '\n') #newline delimited json file
dataset_ref = client.dataset(dataset_id)
table_ref = dataset_ref.table(table_id)
job_config = bigquery.LoadJobConfig()
job_config.source_format = bigquery.SourceFormat.NEWLINE_DELIMITED_JSON
job_config.autodetect = True
with open("sample-json-data.json", "rb") as source_file:
job = client.load_table_from_file(
source_file,
table_ref,
location="us", # Must match the destination dataset location.
job_config=job_config,
) # API request
job.result() # Waits for table load to complete.
print("Loaded {} rows into {}:{}.".format(job.output_rows, dataset_id, table_id))
输出: