2016-10-22 2 views
0

Ошибка:BigQuery Повторная запись добавляется за пределами массива

Exception in thread "main" java.lang.RuntimeException 
{"errors":[{"debugInfo":"generic::failed_precondition: Repeated record added outside of an array.","reason":"invalid"}],"index":0} 

Язык: Scala

Gradle BigQuery зависимость:

compile "com.google.apis:google-api-services-bigquery:v2-rev326-1.22.0" 

Код для создания схемы таблицы:

import scala.collection.JavaConversions._ 
val orderTableSchema = new TableSchema() 
orderTableSchema.setFields(Seq(
    new TableFieldSchema().setName("revisionId").setType("STRING").setMode("REQUIRED"), 
    new TableFieldSchema().setName("executions").setType("RECORD").setMode("REPEATED").setFields(Seq(
    new TableFieldSchema().setName("ts").setType("INTEGER"), 
    new TableFieldSchema().setName("price").setType("FLOAT"), 
    new TableFieldSchema().setName("qty").setType("INTEGER") 
)) 
)) 

таблица успешно создан с правильной схемой для столбца казням, как показано в BigQuery веб-интерфейс:

revisionId   STRING REQUIRED  
executions   RECORD REPEATED 
executions   RECORD REPEATED 
executions.ts  INTEGER NULLABLE 
executions.price FLOAT NULLABLE 
executions.qty  INTEGER NULLABLE 

Код для вставки данных, которые не удается:

import scala.collection.JavaConversions._ 
val row = new TableRow() 
    .set("revisionId", "revision1") 
    .set("executions", Seq(
    new TableRow().set("ts", 1L).set("price", 100.01).set("qty", 1000) 
)) 
val content = new TableDataInsertAllRequest().setRows(Seq(
    new TableDataInsertAllRequest.Rows().setJson(row) 
)) 
val insertAll = bigQuery.tabledata().insertAll(projectId, datasetId, "order", content) 
val insertResponse = insertAll.execute() 

if (insertResponse.getInsertErrors != null) { 
    insertResponse.getInsertErrors.foreach(println) 
    // this prints: 
    // {"errors":[{"debugInfo":"generic::failed_precondition: Repeated record added outside of an array.","reason":"invalid"}],"index":0} 

    // throw to just terminate early for demo 
    throw new RuntimeException() 
} 

ответ

1

Нашли проблему.

Это что-то вроде конвертов с коллекцией scala-to-java. Я должен был явно добавить преобразование, используя JavaConversions.seqAsJavaList, и он волшебным образом начал работать

val row = new TableRow() 
    .set("revisionId", s"revisionId$v") 
    .set("executions", JavaConversions.seqAsJavaList(Seq(
    new TableRow().set("ts", 1L).set("price", 100.01).set("qty", 1000), 
    new TableRow().set("ts", 2L).set("price", 100.02).set("qty", 2000), 
    new TableRow().set("ts", 3L).set("price", 100.03).set("qty", 3000) 
))) 
Смежные вопросы