【发布时间】:2020-06-05 00:31:57
【问题描述】:
尝试将 csv 文件读入数据帧并使用该数据帧加载到具有范围分区的 Bigquery 表中。但是为 Long: 获得 400 POST Invalid value 不是正确的类型错误。
重现步骤: 使用 google-cloud-bigquery v1.24.0
Test.csv
Name, Age, DOB
"rona", 10, 01-01-2010
"king", 20, 05-01-2000
这是要复制的代码
import pandas as pd
from google.cloud import bigquery
def Range_Partitioning(field, dict_range):
cRangePartition = bigquery.RangePartitioning(range_=bigquery.PartitionRange(start=dict_range.get("Start"), interval=dict_range.get("Interval"), end=dict_range.get("End")),
field=field)
return cRangePartition
df = pd.read_csv("Test.txt", dtype={ "Name": "str", "Age": "int64", "DOB": "str"}, parse_dates=["DOB"])
BQClient = bigquery.Client()
Dataset = "Test"
TableName = "Load_Range_Test"
schema = [
{
"name": "Name",
"type": "STRING",
"mode": "REQUIRED"
},
{
"name": "Age",
"type": "INTEGER",
"mode": "REQUIRED"
},
{
"name": "DOB",
"type": "DATE",
"mode": "REQUIRED"
}
]
TableRef = sProjectId + "." + Dataset + "." + TableName
RangePartition = Range_Partitioning("Age", {"start":0, "interval":1, "end":100})
WriteOption = "WRITE_TRUNCATE"
JobConfig = bigquery.LoadJobConfig(
schema=schema,
write_disposition=WriteOption,
range_partitioning=RangePartition)
Job = BQClient.load_table_from_dataframe(df, TableRef, job_config=JobConfig)
Job.result()
错误: 400 POST Invalid value for Long: 类型不正确
当我不进行范围分区时工作,只有使用范围分区我会收到此错误。
【问题讨论】:
-
你确定你可以这样打电话给
bigquery.RangePartitioning吗?函数签名说它接受两个参数:(range_:bigquery.PartitionRange,field_: str) -
@MichaelDelgado 你是对的,我忘了添加我创建的方法,更新了代码。根据 BQ 文档,我确实使用了 range_ 和字段。
-
你的函数中有什么
sPartitionColumn? -
@MichaelDelgado,是字段,它是“年龄”,它是一个整数
-
@MichaelDelgado 是的,您只需要更改 ProjectId 并拥有适当的凭据即可运行 BQ 作业。它应该运行并给出相同的错误。
标签: python pandas dataframe google-bigquery