/** Example of inserting rows into the table. */ // [TARGET insert(Iterable)] // [VARIABLE "rowId1"] // [VARIABLE "rowId2"] public InsertAllResponse insert(String rowId1, String rowId2) { // [START ] List<RowToInsert> rows = new ArrayList<>(); Map<String, Object> row1 = new HashMap<>(); row1.put("stringField", "value1"); row1.put("booleanField", true); Map<String, Object> row2 = new HashMap<>(); row2.put("stringField", "value2"); row2.put("booleanField", false); rows.add(RowToInsert.of(rowId1, row1)); rows.add(RowToInsert.of(rowId2, row2)); InsertAllResponse response = table.insert(rows); // do something with response // [END ] return response; }
/** Example of inserting rows into the table, ignoring invalid rows. */ // [TARGET insert(Iterable, boolean, boolean)] // [VARIABLE "rowId1"] // [VARIABLE "rowId2"] public InsertAllResponse insertWithParams(String rowId1, String rowId2) { // [START ] List<RowToInsert> rows = new ArrayList<>(); Map<String, Object> row1 = new HashMap<>(); row1.put("stringField", 1); row1.put("booleanField", true); Map<String, Object> row2 = new HashMap<>(); row2.put("stringField", "value2"); row2.put("booleanField", false); rows.add(RowToInsert.of(rowId1, row1)); rows.add(RowToInsert.of(rowId2, row2)); InsertAllResponse response = table.insert(rows, true, true); // do something with response // [END ] return response; }
@Test public void testInsert() throws Exception { initializeExpectedTable(1); expect(bigquery.getOptions()).andReturn(mockOptions); expect(bigquery.insertAll(INSERT_ALL_REQUEST)).andReturn(EMPTY_INSERT_ALL_RESPONSE); replay(bigquery); initializeTable(); InsertAllResponse response = table.insert(ROWS_TO_INSERT); assertSame(EMPTY_INSERT_ALL_RESPONSE, response); }
@Test public void testInsertComplete() throws Exception { initializeExpectedTable(1); expect(bigquery.getOptions()).andReturn(mockOptions); expect(bigquery.insertAll(INSERT_ALL_REQUEST_COMPLETE)).andReturn(EMPTY_INSERT_ALL_RESPONSE); replay(bigquery); initializeTable(); InsertAllResponse response = table.insert(ROWS_TO_INSERT, true, true); assertSame(EMPTY_INSERT_ALL_RESPONSE, response); }
public InsertAllResponse insertJson(Table table, Reader jsonData) throws IOException { BigQueryDataFile dataFile = BigQueryDataFile.jsonFile(jsonData); return table.insert(dataFile.iterable()); }