Spark BigQuery 连接器:写入 ARRAY 类型导致异常:“"Invalid value for: ARRAY is not a valid value"”
Spark BigQuery Connector: Writing ARRAY type causes exception: ""Invalid value for: ARRAY is not a valid value""
运行 Google Cloud Dataproc 中的 Spark 作业。使用 BigQuery Connector 将作业的 json 数据输出加载到 BigQuery table。
BigQuery Standard-SQL data types documentation 声明支持 ARRAY 类型。
我的 Scala 代码是:
val outputDatasetId = "mydataset"
val tableSchema = "["+
"{'name': '_id', 'type': 'STRING'},"+
"{'name': 'array1', 'type': 'ARRAY'},"+
"{'name': 'array2', 'type': 'ARRAY'},"+
"{'name': 'number1', 'type': 'FLOAT'}"+
"]"
// Output configuration
BigQueryConfiguration.configureBigQueryOutput(
conf, projectId, outputDatasetId, "outputTable",
tableSchema)
//Write visits to BigQuery
jsonData.saveAsNewAPIHadoopDataset(conf)
但是作业抛出这个异常:
{
"code" : 400,
"errors" : [ {
"domain" : "global",
"message" : "Invalid value for: ARRAY is not a valid value",
"reason" : "invalid"
} ],
"message" : "Invalid value for: ARRAY is not a valid value"
}
at
com.google.cloud.hadoop.util.AbstractGoogleAsyncWriteChannel.waitForCompletionAnThrowIfUploadFailed(AbstractGoogleAsyncWriteChannel.java:432)
at com.google.cloud.hadoop.util.AbstractGoogleAsyncWriteChannel.close(AbstractGoogleAsyncWriteChannel.java:287)
at com.google.cloud.hadoop.io.bigquery.BigQueryRecordWriter.close(BigQueryRecordWriter.java:358)
at org.apache.spark.rdd.PairRDDFunctions$$anonfun$saveAsNewAPIHadoopDataset$$anonfun$$anonfun$apply.apply$mcV$sp(PairRDDFunctions.scala:1124)
at org.apache.spark.util.Utils$.tryWithSafeFinallyAndFailureCallbacks(Utils.scala:1366)
... 8 more
Caused by: com.google.api.client.googleapis.json.GoogleJsonResponseException:
400 Bad Request
这是旧版与标准版 SQL 的问题吗?或者 ARRAY 类型是否不受 BigQuery Connector for Spark 支持?
不要使用 type=ARRAY
,而是像往常一样尝试设置 type
,但还要设置密钥 mode=REPEATED
。
例如,字符串数组将定义为:
{'name': 'field1', 'type': 'STRING', 'mode': 'REPEATED'}
这些是字符串数组吗?整数?我相信使用此 API,您需要将 type
设置为元素类型,例如STRING
或 INT64
,但使用 REPEATED
的 mode
。 BigQuery API 尚未完全更新以在所有地方使用标准 SQL 类型,因此您需要改为使用类型 + 模式的旧约定。
运行 Google Cloud Dataproc 中的 Spark 作业。使用 BigQuery Connector 将作业的 json 数据输出加载到 BigQuery table。
BigQuery Standard-SQL data types documentation 声明支持 ARRAY 类型。
我的 Scala 代码是:
val outputDatasetId = "mydataset"
val tableSchema = "["+
"{'name': '_id', 'type': 'STRING'},"+
"{'name': 'array1', 'type': 'ARRAY'},"+
"{'name': 'array2', 'type': 'ARRAY'},"+
"{'name': 'number1', 'type': 'FLOAT'}"+
"]"
// Output configuration
BigQueryConfiguration.configureBigQueryOutput(
conf, projectId, outputDatasetId, "outputTable",
tableSchema)
//Write visits to BigQuery
jsonData.saveAsNewAPIHadoopDataset(conf)
但是作业抛出这个异常:
{
"code" : 400,
"errors" : [ {
"domain" : "global",
"message" : "Invalid value for: ARRAY is not a valid value",
"reason" : "invalid"
} ],
"message" : "Invalid value for: ARRAY is not a valid value"
}
at
com.google.cloud.hadoop.util.AbstractGoogleAsyncWriteChannel.waitForCompletionAnThrowIfUploadFailed(AbstractGoogleAsyncWriteChannel.java:432)
at com.google.cloud.hadoop.util.AbstractGoogleAsyncWriteChannel.close(AbstractGoogleAsyncWriteChannel.java:287)
at com.google.cloud.hadoop.io.bigquery.BigQueryRecordWriter.close(BigQueryRecordWriter.java:358)
at org.apache.spark.rdd.PairRDDFunctions$$anonfun$saveAsNewAPIHadoopDataset$$anonfun$$anonfun$apply.apply$mcV$sp(PairRDDFunctions.scala:1124)
at org.apache.spark.util.Utils$.tryWithSafeFinallyAndFailureCallbacks(Utils.scala:1366)
... 8 more
Caused by: com.google.api.client.googleapis.json.GoogleJsonResponseException:
400 Bad Request
这是旧版与标准版 SQL 的问题吗?或者 ARRAY 类型是否不受 BigQuery Connector for Spark 支持?
不要使用 type=ARRAY
,而是像往常一样尝试设置 type
,但还要设置密钥 mode=REPEATED
。
例如,字符串数组将定义为:
{'name': 'field1', 'type': 'STRING', 'mode': 'REPEATED'}
这些是字符串数组吗?整数?我相信使用此 API,您需要将 type
设置为元素类型,例如STRING
或 INT64
,但使用 REPEATED
的 mode
。 BigQuery API 尚未完全更新以在所有地方使用标准 SQL 类型,因此您需要改为使用类型 + 模式的旧约定。