2014-05-17 69 views
3

我想使用JAVA API流重複記錄字段。它插入記錄但是爲空值。數據和架構我用的是谷歌的例子爲「嵌套和重複的數據」重複記錄 - 嵌套和重複數據 - Java API

以下是代碼片段:

 TableRow row = new TableRow(); 
     row.set("kind", "person"); 
     row.set("fullName", "P"); 
     row.set("age", "22"); 
     row.set("gender", "M"); 


     TableRow prow = new TableRow(); 
     prow.set("areaCode", 206); 
     prow.set("number", 1234567);  
     row.set("phoneNumber", prow); 

     JsonArray children = new JsonArray(); 

     JsonObject child1 = new JsonObject(); 
     child1.addProperty("name", "Jane"); 
     child1.addProperty("gender", "f"); 
     child1.addProperty("age", 6);  
     children.add(child1); 

     JsonObject child2 = new JsonObject(); 
     child2.addProperty("name", "John"); 
     child2.addProperty("gender", "m"); 
     child2.addProperty("age", 15);   
     children.add(child2); 

     row.set("children", children); 


     TableDataInsertAllRequest.Rows rows = new TableDataInsertAllRequest.Rows();   
     rows.setJson(row); 

     List rowList = new ArrayList(); 
     rowList.add(rows); 
     TableDataInsertAllRequest content = new TableDataInsertAllRequest().setRows(rowList); 
     TableDataInsertAllResponse response = bigquery.tabledata().insertAll(projectId, "tmp", "person5", content).execute(); 
+0

您是否從響應打印錯誤?我認爲問題在於JsonObject,試着在'prow'或'row'中使用'TableRow'。 – Pentium10

+0

如果我這樣做,那麼它會拋出一個錯誤,「JsonArray是必需的」 – user3513161

回答

2

而不是JsonArray創建列表工作如下:

List<TableRow> children = new ArrayList<TableRow>(); 

TableRow child1 = new TableRow(); 
child1.set("name", "Jane"); 
child1.set("gender", "f"); 
child1.set("age", 6);  

TableRow child2 = new TableRow(); 
child2.set("name", "John"); 
child2.set("gender", "m"); 
child2.set("age", 15); 

children.add(child1); 
children.add(child2);