Search in sources :

Example 1 with FieldValue

use of com.google.cloud.bigquery.FieldValue in project google-cloud-java by GoogleCloudPlatform.

the class ITTableSnippets method testInsertParams.

@Test
public void testInsertParams() throws InterruptedException {
    InsertAllResponse response = tableSnippets.insertWithParams("row1", "row2");
    assertTrue(response.hasErrors());
    List<List<FieldValue>> rows = ImmutableList.copyOf(tableSnippets.list().getValues());
    while (rows.isEmpty()) {
        Thread.sleep(500);
        rows = ImmutableList.copyOf(tableSnippets.list().getValues());
    }
    Set<List<?>> values = FluentIterable.from(rows).transform(new Function<List<FieldValue>, List<?>>() {

        @Override
        public List<?> apply(List<FieldValue> row) {
            return ImmutableList.of(row.get(0).getStringValue(), row.get(1).getBooleanValue());
        }
    }).toSet();
    assertEquals(ImmutableSet.of(ROW2), values);
}
Also used : Function(com.google.common.base.Function) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) FieldValue(com.google.cloud.bigquery.FieldValue) InsertAllResponse(com.google.cloud.bigquery.InsertAllResponse) Test(org.junit.Test)

Example 2 with FieldValue

use of com.google.cloud.bigquery.FieldValue in project google-cloud-java by GoogleCloudPlatform.

the class ITBigQueryTest method testCreateViewTable.

@Test
public void testCreateViewTable() throws InterruptedException {
    String tableName = "test_create_view_table";
    TableId tableId = TableId.of(DATASET, tableName);
    ViewDefinition viewDefinition = ViewDefinition.of("SELECT TimestampField, StringField, BooleanField FROM " + DATASET + "." + TABLE_ID.getTable());
    TableInfo tableInfo = TableInfo.of(tableId, viewDefinition);
    Table createdTable = bigquery.create(tableInfo);
    assertNotNull(createdTable);
    assertEquals(DATASET, createdTable.getTableId().getDataset());
    assertEquals(tableName, createdTable.getTableId().getTable());
    Table remoteTable = bigquery.getTable(DATASET, tableName);
    assertNotNull(remoteTable);
    assertEquals(createdTable.getTableId(), remoteTable.getTableId());
    assertTrue(remoteTable.getDefinition() instanceof ViewDefinition);
    Schema expectedSchema = Schema.newBuilder().addField(Field.newBuilder("TimestampField", Field.Type.timestamp()).setMode(Field.Mode.NULLABLE).build()).addField(Field.newBuilder("StringField", Field.Type.string()).setMode(Field.Mode.NULLABLE).build()).addField(Field.newBuilder("BooleanField", Field.Type.bool()).setMode(Field.Mode.NULLABLE).build()).build();
    assertEquals(expectedSchema, remoteTable.getDefinition().getSchema());
    QueryRequest request = QueryRequest.newBuilder("SELECT * FROM " + tableName).setDefaultDataset(DatasetId.of(DATASET)).setMaxWaitTime(60000L).setPageSize(1000L).build();
    QueryResponse response = bigquery.query(request);
    while (!response.jobCompleted()) {
        response = bigquery.getQueryResults(response.getJobId());
        Thread.sleep(1000);
    }
    int rowCount = 0;
    for (List<FieldValue> row : response.getResult().getValues()) {
        FieldValue timestampCell = row.get(0);
        FieldValue stringCell = row.get(1);
        FieldValue booleanCell = row.get(2);
        assertEquals(FieldValue.Attribute.PRIMITIVE, timestampCell.getAttribute());
        assertEquals(FieldValue.Attribute.PRIMITIVE, stringCell.getAttribute());
        assertEquals(FieldValue.Attribute.PRIMITIVE, booleanCell.getAttribute());
        assertEquals(1408452095220000L, timestampCell.getTimestampValue());
        assertEquals("stringValue", stringCell.getStringValue());
        assertEquals(false, booleanCell.getBooleanValue());
        rowCount++;
    }
    assertEquals(2, rowCount);
    assertTrue(remoteTable.delete());
}
Also used : TableId(com.google.cloud.bigquery.TableId) Table(com.google.cloud.bigquery.Table) QueryRequest(com.google.cloud.bigquery.QueryRequest) ViewDefinition(com.google.cloud.bigquery.ViewDefinition) Schema(com.google.cloud.bigquery.Schema) QueryResponse(com.google.cloud.bigquery.QueryResponse) TableInfo(com.google.cloud.bigquery.TableInfo) FieldValue(com.google.cloud.bigquery.FieldValue) Test(org.junit.Test)

Example 3 with FieldValue

use of com.google.cloud.bigquery.FieldValue in project google-cloud-java by GoogleCloudPlatform.

the class ITBigQueryTest method testListAllTableData.

@Test
public void testListAllTableData() {
    Page<List<FieldValue>> rows = bigquery.listTableData(TABLE_ID);
    int rowCount = 0;
    for (List<FieldValue> row : rows.getValues()) {
        FieldValue timestampCell = row.get(0);
        FieldValue stringCell = row.get(1);
        FieldValue integerArrayCell = row.get(2);
        FieldValue booleanCell = row.get(3);
        FieldValue bytesCell = row.get(4);
        FieldValue recordCell = row.get(5);
        FieldValue integerCell = row.get(6);
        FieldValue floatCell = row.get(7);
        assertEquals(FieldValue.Attribute.PRIMITIVE, timestampCell.getAttribute());
        assertEquals(FieldValue.Attribute.PRIMITIVE, stringCell.getAttribute());
        assertEquals(FieldValue.Attribute.REPEATED, integerArrayCell.getAttribute());
        assertEquals(FieldValue.Attribute.PRIMITIVE, booleanCell.getAttribute());
        assertEquals(FieldValue.Attribute.PRIMITIVE, bytesCell.getAttribute());
        assertEquals(FieldValue.Attribute.RECORD, recordCell.getAttribute());
        assertEquals(FieldValue.Attribute.PRIMITIVE, integerCell.getAttribute());
        assertEquals(FieldValue.Attribute.PRIMITIVE, floatCell.getAttribute());
        assertEquals(1408452095220000L, timestampCell.getTimestampValue());
        assertEquals("stringValue", stringCell.getStringValue());
        assertEquals(0, integerArrayCell.getRepeatedValue().get(0).getLongValue());
        assertEquals(1, integerArrayCell.getRepeatedValue().get(1).getLongValue());
        assertEquals(false, booleanCell.getBooleanValue());
        assertArrayEquals(BYTES, bytesCell.getBytesValue());
        assertEquals(-14182916000000L, recordCell.getRecordValue().get(0).getTimestampValue());
        assertTrue(recordCell.getRecordValue().get(1).isNull());
        assertEquals(1, recordCell.getRecordValue().get(2).getRepeatedValue().get(0).getLongValue());
        assertEquals(0, recordCell.getRecordValue().get(2).getRepeatedValue().get(1).getLongValue());
        assertEquals(true, recordCell.getRecordValue().get(3).getBooleanValue());
        assertEquals(3, integerCell.getLongValue());
        assertEquals(1.2, floatCell.getDoubleValue(), 0.0001);
        rowCount++;
    }
    assertEquals(2, rowCount);
}
Also used : List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) FieldValue(com.google.cloud.bigquery.FieldValue) Test(org.junit.Test)

Example 4 with FieldValue

use of com.google.cloud.bigquery.FieldValue in project google-cloud-java by GoogleCloudPlatform.

the class ITBigQueryTest method testBytesParameter.

@Test
public void testBytesParameter() throws Exception {
    String query = new StringBuilder().append("SELECT BYTE_LENGTH(@p) AS length").toString();
    QueryParameterValue bytesParameter = QueryParameterValue.bytes(new byte[] { 1, 3 });
    QueryRequest request = QueryRequest.newBuilder(query).setDefaultDataset(DatasetId.of(DATASET)).setMaxWaitTime(60000L).setPageSize(1000L).setUseLegacySql(false).addNamedParameter("p", bytesParameter).build();
    QueryResponse response = queryAndWaitForResponse(request);
    int rowCount = 0;
    for (List<FieldValue> row : response.getResult().getValues()) {
        rowCount++;
        assertEquals(2, row.get(0).getLongValue());
    }
    assertEquals(1, rowCount);
}
Also used : QueryParameterValue(com.google.cloud.bigquery.QueryParameterValue) QueryRequest(com.google.cloud.bigquery.QueryRequest) QueryResponse(com.google.cloud.bigquery.QueryResponse) FieldValue(com.google.cloud.bigquery.FieldValue) Test(org.junit.Test)

Example 5 with FieldValue

use of com.google.cloud.bigquery.FieldValue in project google-cloud-java by GoogleCloudPlatform.

the class ITBigQueryTest method testInsertFromFile.

@Test
public void testInsertFromFile() throws InterruptedException, IOException, TimeoutException {
    String destinationTableName = "test_insert_from_file_table";
    TableId tableId = TableId.of(DATASET, destinationTableName);
    WriteChannelConfiguration configuration = WriteChannelConfiguration.newBuilder(tableId).setFormatOptions(FormatOptions.json()).setCreateDisposition(JobInfo.CreateDisposition.CREATE_IF_NEEDED).setSchema(TABLE_SCHEMA).build();
    TableDataWriteChannel channel = bigquery.writer(configuration);
    try {
        channel.write(ByteBuffer.wrap(JSON_CONTENT.getBytes(StandardCharsets.UTF_8)));
    } finally {
        channel.close();
    }
    Job job = channel.getJob().waitFor();
    LoadStatistics statistics = job.getStatistics();
    assertEquals(1L, statistics.getInputFiles().longValue());
    assertEquals(2L, statistics.getOutputRows().longValue());
    LoadJobConfiguration jobConfiguration = job.getConfiguration();
    assertEquals(TABLE_SCHEMA, jobConfiguration.getSchema());
    assertNull(jobConfiguration.getSourceUris());
    assertNull(job.getStatus().getError());
    Page<List<FieldValue>> rows = bigquery.listTableData(tableId);
    int rowCount = 0;
    for (List<FieldValue> row : rows.getValues()) {
        FieldValue timestampCell = row.get(0);
        FieldValue stringCell = row.get(1);
        FieldValue integerArrayCell = row.get(2);
        FieldValue booleanCell = row.get(3);
        FieldValue bytesCell = row.get(4);
        FieldValue recordCell = row.get(5);
        FieldValue integerCell = row.get(6);
        FieldValue floatCell = row.get(7);
        assertEquals(FieldValue.Attribute.PRIMITIVE, timestampCell.getAttribute());
        assertEquals(FieldValue.Attribute.PRIMITIVE, stringCell.getAttribute());
        assertEquals(FieldValue.Attribute.REPEATED, integerArrayCell.getAttribute());
        assertEquals(FieldValue.Attribute.PRIMITIVE, booleanCell.getAttribute());
        assertEquals(FieldValue.Attribute.PRIMITIVE, bytesCell.getAttribute());
        assertEquals(FieldValue.Attribute.RECORD, recordCell.getAttribute());
        assertEquals(FieldValue.Attribute.PRIMITIVE, integerCell.getAttribute());
        assertEquals(FieldValue.Attribute.PRIMITIVE, floatCell.getAttribute());
        assertEquals(1408452095220000L, timestampCell.getTimestampValue());
        assertEquals("stringValue", stringCell.getStringValue());
        assertEquals(0, integerArrayCell.getRepeatedValue().get(0).getLongValue());
        assertEquals(1, integerArrayCell.getRepeatedValue().get(1).getLongValue());
        assertEquals(false, booleanCell.getBooleanValue());
        assertArrayEquals(BYTES, bytesCell.getBytesValue());
        assertEquals(-14182916000000L, recordCell.getRecordValue().get(0).getTimestampValue());
        assertTrue(recordCell.getRecordValue().get(1).isNull());
        assertEquals(1, recordCell.getRecordValue().get(2).getRepeatedValue().get(0).getLongValue());
        assertEquals(0, recordCell.getRecordValue().get(2).getRepeatedValue().get(1).getLongValue());
        assertEquals(true, recordCell.getRecordValue().get(3).getBooleanValue());
        assertEquals(3, integerCell.getLongValue());
        assertEquals(1.2, floatCell.getDoubleValue(), 0.0001);
        rowCount++;
    }
    assertEquals(2, rowCount);
    assertTrue(bigquery.delete(DATASET, destinationTableName));
}
Also used : TableId(com.google.cloud.bigquery.TableId) LoadStatistics(com.google.cloud.bigquery.JobStatistics.LoadStatistics) TableDataWriteChannel(com.google.cloud.bigquery.TableDataWriteChannel) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) LoadJobConfiguration(com.google.cloud.bigquery.LoadJobConfiguration) FieldValue(com.google.cloud.bigquery.FieldValue) Job(com.google.cloud.bigquery.Job) WriteChannelConfiguration(com.google.cloud.bigquery.WriteChannelConfiguration) Test(org.junit.Test)

Aggregations

FieldValue (com.google.cloud.bigquery.FieldValue)31 QueryResult (com.google.cloud.bigquery.QueryResult)13 Test (org.junit.Test)10 QueryJobConfiguration (com.google.cloud.bigquery.QueryJobConfiguration)9 QueryResponse (com.google.cloud.bigquery.QueryResponse)9 QueryRequest (com.google.cloud.bigquery.QueryRequest)8 List (java.util.List)8 TableId (com.google.cloud.bigquery.TableId)7 BigQuery (com.google.cloud.bigquery.BigQuery)5 ArrayList (java.util.ArrayList)5 ParticipantCriteria (org.pmiops.workbench.cohortbuilder.ParticipantCriteria)5 Job (com.google.cloud.bigquery.Job)4 TableResult (com.google.cloud.bigquery.TableResult)4 ImmutableList (com.google.common.collect.ImmutableList)4 InsertAllResponse (com.google.cloud.bigquery.InsertAllResponse)3 Schema (com.google.cloud.bigquery.Schema)3 TableInfo (com.google.cloud.bigquery.TableInfo)3 SimpleDateFormat (java.text.SimpleDateFormat)3 HashMap (java.util.HashMap)3 PageRequest (org.pmiops.workbench.cohortreview.util.PageRequest)3