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);
}
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());
}
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);
}
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);
}
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));
}
Aggregations