use of com.google.cloud.bigquery.FieldValue in project google-cloud-java by GoogleCloudPlatform.
the class BigQuerySnippets method listTableDataFromId.
/**
* Example of listing table rows, specifying the page size.
*/
// [TARGET listTableData(TableId, TableDataListOption...)]
// [VARIABLE "my_dataset_name"]
// [VARIABLE "my_table_name"]
public Page<List<FieldValue>> listTableDataFromId(String datasetName, String tableName) {
// [START listTableDataFromId]
TableId tableIdObject = TableId.of(datasetName, tableName);
Page<List<FieldValue>> tableData = bigquery.listTableData(tableIdObject, TableDataListOption.pageSize(100));
for (List<FieldValue> row : tableData.iterateAll()) {
// do something with the row
}
// [END listTableDataFromId]
return tableData;
}
use of com.google.cloud.bigquery.FieldValue in project google-cloud-java by GoogleCloudPlatform.
the class BigQuerySnippets method runQueryWithParameters.
/**
* Example of running a query with query parameters.
*/
// [TARGET query(QueryRequest)]
// [VARIABLE "SELECT distinct(corpus) FROM `bigquery-public-data.samples.shakespeare` where word_count > @wordCount"]
public QueryResponse runQueryWithParameters(String query) throws InterruptedException {
// [START runQueryWithParameters]
QueryRequest request = QueryRequest.newBuilder(query).setUseLegacySql(// standard SQL is required to use query parameters
false).addNamedParameter("wordCount", QueryParameterValue.int64(5)).build();
QueryResponse response = bigquery.query(request);
// Wait for things to finish
while (!response.jobCompleted()) {
Thread.sleep(1000);
response = bigquery.getQueryResults(response.getJobId());
}
if (response.hasErrors()) {
// handle errors
}
QueryResult result = response.getResult();
for (List<FieldValue> row : result.iterateAll()) {
// do something with the data
}
// [END runQueryWithParameters]
return response;
}
use of com.google.cloud.bigquery.FieldValue in project google-cloud-java by GoogleCloudPlatform.
the class BigQuerySnippets method queryResults.
/**
* Example of getting the results of query.
*/
// [TARGET getQueryResults(JobId, QueryResultsOption...)]
// [VARIABLE "SELECT unique(corpus) FROM [bigquery-public-data:samples.shakespeare]"]
public QueryResponse queryResults(final String query) throws InterruptedException {
// [START queryResults]
QueryRequest request = QueryRequest.of(query);
QueryResponse response = bigquery.query(request);
// Wait for things to finish
while (!response.jobCompleted()) {
Thread.sleep(1000);
response = bigquery.getQueryResults(response.getJobId());
}
if (response.hasErrors()) {
// handle errors
}
QueryResult result = response.getResult();
for (List<FieldValue> row : result.iterateAll()) {
// do something with the data
}
// [END queryResults]
return response;
}
use of com.google.cloud.bigquery.FieldValue in project google-cloud-java by GoogleCloudPlatform.
the class ITBigQueryTest method testQueryJob.
@Test
public void testQueryJob() throws InterruptedException, TimeoutException {
String tableName = "test_query_job_table";
String query = new StringBuilder().append("SELECT TimestampField, StringField, BooleanField FROM ").append(TABLE_ID.getTable()).toString();
TableId destinationTable = TableId.of(DATASET, tableName);
QueryJobConfiguration configuration = QueryJobConfiguration.newBuilder(query).setDefaultDataset(DatasetId.of(DATASET)).setDestinationTable(destinationTable).build();
Job remoteJob = bigquery.create(JobInfo.of(configuration));
remoteJob = remoteJob.waitFor();
assertNull(remoteJob.getStatus().getError());
QueryResponse response = bigquery.getQueryResults(remoteJob.getJobId());
while (!response.jobCompleted()) {
Thread.sleep(1000);
response = bigquery.getQueryResults(response.getJobId());
}
assertFalse(response.hasErrors());
assertEquals(QUERY_RESULT_SCHEMA, response.getResult().getSchema());
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(bigquery.delete(DATASET, tableName));
Job queryJob = bigquery.getJob(remoteJob.getJobId());
JobStatistics.QueryStatistics statistics = queryJob.getStatistics();
assertNotNull(statistics.getQueryPlan());
}
use of com.google.cloud.bigquery.FieldValue in project google-cloud-java by GoogleCloudPlatform.
the class ITBigQueryTest method testCreateExternalTable.
@Test
public void testCreateExternalTable() throws InterruptedException {
String tableName = "test_create_external_table";
TableId tableId = TableId.of(DATASET, tableName);
ExternalTableDefinition externalTableDefinition = ExternalTableDefinition.of("gs://" + BUCKET + "/" + JSON_LOAD_FILE, TABLE_SCHEMA, FormatOptions.json());
TableInfo tableInfo = TableInfo.of(tableId, externalTableDefinition);
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);
assertTrue(remoteTable.getDefinition() instanceof ExternalTableDefinition);
assertEquals(createdTable.getTableId(), remoteTable.getTableId());
assertEquals(TABLE_SCHEMA, remoteTable.getDefinition().getSchema());
QueryRequest request = QueryRequest.newBuilder("SELECT TimestampField, StringField, IntegerArrayField, BooleanField FROM " + DATASET + "." + 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);
}
long integerValue = 0;
int rowCount = 0;
for (List<FieldValue> row : response.getResult().getValues()) {
FieldValue timestampCell = row.get(0);
FieldValue stringCell = row.get(1);
FieldValue integerCell = row.get(2);
FieldValue booleanCell = row.get(3);
assertEquals(FieldValue.Attribute.PRIMITIVE, timestampCell.getAttribute());
assertEquals(FieldValue.Attribute.PRIMITIVE, stringCell.getAttribute());
assertEquals(FieldValue.Attribute.PRIMITIVE, integerCell.getAttribute());
assertEquals(FieldValue.Attribute.PRIMITIVE, booleanCell.getAttribute());
assertEquals(1408452095220000L, timestampCell.getTimestampValue());
assertEquals("stringValue", stringCell.getStringValue());
assertEquals(integerValue, integerCell.getLongValue());
assertEquals(false, booleanCell.getBooleanValue());
integerValue = ~integerValue & 0x1;
rowCount++;
}
assertEquals(4, rowCount);
assertTrue(remoteTable.delete());
}
Aggregations