Search in sources :

Example 1 with TableDataInsertAllRequest

use of com.google.api.services.bigquery.model.TableDataInsertAllRequest in project google-cloud-java by GoogleCloudPlatform.

the class BigQueryImplTest method testInsertAll.

@Test
public void testInsertAll() {
    Map<String, Object> row1 = ImmutableMap.<String, Object>of("field", "value1");
    Map<String, Object> row2 = ImmutableMap.<String, Object>of("field", "value2");
    List<RowToInsert> rows = ImmutableList.of(new RowToInsert("row1", row1), new RowToInsert("row2", row2));
    InsertAllRequest request = InsertAllRequest.newBuilder(TABLE_ID).setRows(rows).setSkipInvalidRows(false).setIgnoreUnknownValues(true).setTemplateSuffix("suffix").build();
    TableDataInsertAllRequest requestPb = new TableDataInsertAllRequest().setRows(Lists.transform(rows, new Function<RowToInsert, TableDataInsertAllRequest.Rows>() {

        @Override
        public TableDataInsertAllRequest.Rows apply(RowToInsert rowToInsert) {
            return new TableDataInsertAllRequest.Rows().setInsertId(rowToInsert.getId()).setJson(rowToInsert.getContent());
        }
    })).setSkipInvalidRows(false).setIgnoreUnknownValues(true).setTemplateSuffix("suffix");
    TableDataInsertAllResponse responsePb = new TableDataInsertAllResponse().setInsertErrors(ImmutableList.of(new TableDataInsertAllResponse.InsertErrors().setIndex(0L).setErrors(ImmutableList.of(new ErrorProto().setMessage("ErrorMessage")))));
    EasyMock.expect(bigqueryRpcMock.insertAll(PROJECT, DATASET, TABLE, requestPb)).andReturn(responsePb);
    EasyMock.replay(bigqueryRpcMock);
    bigquery = options.getService();
    InsertAllResponse response = bigquery.insertAll(request);
    assertNotNull(response.getErrorsFor(0L));
    assertNull(response.getErrorsFor(1L));
    assertEquals(1, response.getErrorsFor(0L).size());
    assertEquals("ErrorMessage", response.getErrorsFor(0L).get(0).getMessage());
}
Also used : ErrorProto(com.google.api.services.bigquery.model.ErrorProto) TableDataInsertAllResponse(com.google.api.services.bigquery.model.TableDataInsertAllResponse) TableDataInsertAllResponse(com.google.api.services.bigquery.model.TableDataInsertAllResponse) TableDataInsertAllRequest(com.google.api.services.bigquery.model.TableDataInsertAllRequest) RowToInsert(com.google.cloud.bigquery.InsertAllRequest.RowToInsert) TableDataInsertAllRequest(com.google.api.services.bigquery.model.TableDataInsertAllRequest) Test(org.junit.Test)

Example 2 with TableDataInsertAllRequest

use of com.google.api.services.bigquery.model.TableDataInsertAllRequest in project google-cloud-java by GoogleCloudPlatform.

the class BigQueryImplTest method testInsertAllWithProject.

@Test
public void testInsertAllWithProject() {
    Map<String, Object> row1 = ImmutableMap.<String, Object>of("field", "value1");
    Map<String, Object> row2 = ImmutableMap.<String, Object>of("field", "value2");
    List<RowToInsert> rows = ImmutableList.of(new RowToInsert("row1", row1), new RowToInsert("row2", row2));
    TableId tableId = TableId.of(OTHER_PROJECT, DATASET, TABLE);
    InsertAllRequest request = InsertAllRequest.newBuilder(tableId).setRows(rows).setSkipInvalidRows(false).setIgnoreUnknownValues(true).setTemplateSuffix("suffix").build();
    TableDataInsertAllRequest requestPb = new TableDataInsertAllRequest().setRows(Lists.transform(rows, new Function<RowToInsert, TableDataInsertAllRequest.Rows>() {

        @Override
        public TableDataInsertAllRequest.Rows apply(RowToInsert rowToInsert) {
            return new TableDataInsertAllRequest.Rows().setInsertId(rowToInsert.getId()).setJson(rowToInsert.getContent());
        }
    })).setSkipInvalidRows(false).setIgnoreUnknownValues(true).setTemplateSuffix("suffix");
    TableDataInsertAllResponse responsePb = new TableDataInsertAllResponse().setInsertErrors(ImmutableList.of(new TableDataInsertAllResponse.InsertErrors().setIndex(0L).setErrors(ImmutableList.of(new ErrorProto().setMessage("ErrorMessage")))));
    EasyMock.expect(bigqueryRpcMock.insertAll(OTHER_PROJECT, DATASET, TABLE, requestPb)).andReturn(responsePb);
    EasyMock.replay(bigqueryRpcMock);
    bigquery = options.getService();
    InsertAllResponse response = bigquery.insertAll(request);
    assertNotNull(response.getErrorsFor(0L));
    assertNull(response.getErrorsFor(1L));
    assertEquals(1, response.getErrorsFor(0L).size());
    assertEquals("ErrorMessage", response.getErrorsFor(0L).get(0).getMessage());
}
Also used : ErrorProto(com.google.api.services.bigquery.model.ErrorProto) TableDataInsertAllResponse(com.google.api.services.bigquery.model.TableDataInsertAllResponse) TableDataInsertAllResponse(com.google.api.services.bigquery.model.TableDataInsertAllResponse) TableDataInsertAllRequest(com.google.api.services.bigquery.model.TableDataInsertAllRequest) RowToInsert(com.google.cloud.bigquery.InsertAllRequest.RowToInsert) TableDataInsertAllRequest(com.google.api.services.bigquery.model.TableDataInsertAllRequest) Test(org.junit.Test)

Example 3 with TableDataInsertAllRequest

use of com.google.api.services.bigquery.model.TableDataInsertAllRequest in project google-cloud-java by GoogleCloudPlatform.

the class BigQueryImpl method insertAll.

@Override
public InsertAllResponse insertAll(InsertAllRequest request) {
    final TableId tableId = request.getTable().setProjectId(getOptions().getProjectId());
    final TableDataInsertAllRequest requestPb = new TableDataInsertAllRequest();
    requestPb.setIgnoreUnknownValues(request.ignoreUnknownValues());
    requestPb.setSkipInvalidRows(request.skipInvalidRows());
    requestPb.setTemplateSuffix(request.getTemplateSuffix());
    List<Rows> rowsPb = Lists.transform(request.getRows(), new Function<RowToInsert, Rows>() {

        @Override
        public Rows apply(RowToInsert rowToInsert) {
            return new Rows().setInsertId(rowToInsert.getId()).setJson(rowToInsert.getContent());
        }
    });
    requestPb.setRows(rowsPb);
    return InsertAllResponse.fromPb(bigQueryRpc.insertAll(tableId.getProject(), tableId.getDataset(), tableId.getTable(), requestPb));
}
Also used : TableDataInsertAllRequest(com.google.api.services.bigquery.model.TableDataInsertAllRequest) RowToInsert(com.google.cloud.bigquery.InsertAllRequest.RowToInsert) Rows(com.google.api.services.bigquery.model.TableDataInsertAllRequest.Rows)

Aggregations

TableDataInsertAllRequest (com.google.api.services.bigquery.model.TableDataInsertAllRequest)3 RowToInsert (com.google.cloud.bigquery.InsertAllRequest.RowToInsert)3 ErrorProto (com.google.api.services.bigquery.model.ErrorProto)2 TableDataInsertAllResponse (com.google.api.services.bigquery.model.TableDataInsertAllResponse)2 Test (org.junit.Test)2 Rows (com.google.api.services.bigquery.model.TableDataInsertAllRequest.Rows)1