0

在Google Cloud Dataproc中运行Spark作业。使用BigQuery Connector将从作业输出的json数据加载到BigQuery表中。Spark BigQuery连接器:写入ARRAY类型会导致异常:“”ARRAY的值无效“

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$1$$anonfun$12$$anonfun$apply$5.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的问题?或者,Spark的BigQuery Connector不支持ARRAY类型?

回答

3

而不是使用type=ARRAY,尝试设置type像往常一样,但也设置密钥mode=REPEATED

例如字符串数组将被定义为:

{'name': 'field1', 'type': 'STRING', 'mode': 'REPEATED'} 
+0

完美。解决了。感谢复制/粘贴代码块。 –

2

是字符串的这些阵列?整型?我相信使用这个API,您需要将type设置为元素类型,例如STRINGINT64,但是使用modeREPEATED。 BigQuery API尚未完全更新,无处不在使用标准SQL类型,因此您需要使用类型+模式的传统约定。

相关问题