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類型,因此您需要使用類型+模式的傳統約定。

相關問題