Search in sources :

Example 1 with BigQuery

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

the class RemoteBigQueryHelperTest method testForceDelete.

@Test
public void testForceDelete() throws InterruptedException, ExecutionException {
    BigQuery bigqueryMock = EasyMock.createMock(BigQuery.class);
    EasyMock.expect(bigqueryMock.delete(DATASET_NAME, DatasetDeleteOption.deleteContents())).andReturn(true);
    EasyMock.replay(bigqueryMock);
    assertTrue(RemoteBigQueryHelper.forceDelete(bigqueryMock, DATASET_NAME));
    EasyMock.verify(bigqueryMock);
}
Also used : BigQuery(com.google.cloud.bigquery.BigQuery) Test(org.junit.Test)

Example 2 with BigQuery

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

the class CreateTableAndLoadData method main.

public static void main(String... args) throws InterruptedException, TimeoutException {
    BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
    TableId tableId = TableId.of("dataset", "table");
    Table table = bigquery.getTable(tableId);
    if (table == null) {
        System.out.println("Creating table " + tableId);
        Field integerField = Field.of("fieldName", Field.Type.integer());
        Schema schema = Schema.of(integerField);
        table = bigquery.create(TableInfo.of(tableId, StandardTableDefinition.of(schema)));
    }
    System.out.println("Loading data into table " + tableId);
    Job loadJob = table.load(FormatOptions.csv(), "gs://bucket/path");
    loadJob = loadJob.waitFor();
    if (loadJob.getStatus().getError() != null) {
        System.out.println("Job completed with errors");
    } else {
        System.out.println("Job succeeded");
    }
}
Also used : TableId(com.google.cloud.bigquery.TableId) Field(com.google.cloud.bigquery.Field) BigQuery(com.google.cloud.bigquery.BigQuery) Table(com.google.cloud.bigquery.Table) Schema(com.google.cloud.bigquery.Schema) Job(com.google.cloud.bigquery.Job)

Example 3 with BigQuery

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

the class BigQueryExample method main.

@SuppressWarnings("unchecked")
public static void main(String... args) throws Exception {
    if (args.length < 1) {
        System.out.println("Missing required project id and action");
        printUsage();
        return;
    }
    BigQueryOptions.Builder optionsBuilder = BigQueryOptions.newBuilder();
    BigQueryAction action;
    String actionName;
    if (args.length >= 2 && !ACTIONS.containsKey(args[0])) {
        actionName = args[1];
        optionsBuilder.setProjectId(args[0]);
        action = ACTIONS.get(args[1]);
        args = Arrays.copyOfRange(args, 2, args.length);
    } else {
        actionName = args[0];
        action = ACTIONS.get(args[0]);
        args = Arrays.copyOfRange(args, 1, args.length);
    }
    if (action == null) {
        System.out.println("Unrecognized action.");
        printUsage();
        return;
    }
    BigQuery bigquery = optionsBuilder.build().getService();
    Object arg;
    try {
        arg = action.parse(args);
    } catch (IllegalArgumentException ex) {
        System.out.printf("Invalid input for action '%s'. %s%n", actionName, ex.getMessage());
        System.out.printf("Expected: %s%n", action.params());
        return;
    } catch (Exception ex) {
        System.out.println("Failed to parse arguments.");
        ex.printStackTrace();
        return;
    }
    action.run(bigquery, arg);
}
Also used : BigQuery(com.google.cloud.bigquery.BigQuery) BigQueryOptions(com.google.cloud.bigquery.BigQueryOptions)

Example 4 with BigQuery

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

the class InsertDataAndQueryTable method main.

public static void main(String... args) throws InterruptedException {
    // Create a service instance
    BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
    // Create a dataset
    String datasetId = "my_dataset_id";
    bigquery.create(DatasetInfo.newBuilder(datasetId).build());
    TableId tableId = TableId.of(datasetId, "my_table_id");
    // Table field definition
    Field stringField = Field.of("StringField", Field.Type.string());
    // Table schema definition
    Schema schema = Schema.of(stringField);
    // Create a table
    StandardTableDefinition tableDefinition = StandardTableDefinition.of(schema);
    bigquery.create(TableInfo.of(tableId, tableDefinition));
    // Define rows to insert
    Map<String, Object> firstRow = new HashMap<>();
    Map<String, Object> secondRow = new HashMap<>();
    firstRow.put("StringField", "value1");
    secondRow.put("StringField", "value2");
    // Create an insert request
    InsertAllRequest insertRequest = InsertAllRequest.newBuilder(tableId).addRow(firstRow).addRow(secondRow).build();
    // Insert rows
    InsertAllResponse insertResponse = bigquery.insertAll(insertRequest);
    // Check if errors occurred
    if (insertResponse.hasErrors()) {
        System.out.println("Errors occurred while inserting rows");
    }
    // Create a query request
    QueryRequest queryRequest = QueryRequest.newBuilder("SELECT * FROM my_dataset_id.my_table_id").setMaxWaitTime(60000L).setPageSize(1000L).build();
    // Request query to be executed and wait for results
    QueryResponse queryResponse = bigquery.query(queryRequest);
    while (!queryResponse.jobCompleted()) {
        Thread.sleep(1000L);
        queryResponse = bigquery.getQueryResults(queryResponse.getJobId());
    }
    // Read rows
    System.out.println("Table rows:");
    for (List<FieldValue> row : queryResponse.getResult().iterateAll()) {
        System.out.println(row);
    }
}
Also used : TableId(com.google.cloud.bigquery.TableId) BigQuery(com.google.cloud.bigquery.BigQuery) QueryRequest(com.google.cloud.bigquery.QueryRequest) HashMap(java.util.HashMap) Schema(com.google.cloud.bigquery.Schema) InsertAllResponse(com.google.cloud.bigquery.InsertAllResponse) Field(com.google.cloud.bigquery.Field) QueryResponse(com.google.cloud.bigquery.QueryResponse) FieldValue(com.google.cloud.bigquery.FieldValue) StandardTableDefinition(com.google.cloud.bigquery.StandardTableDefinition) InsertAllRequest(com.google.cloud.bigquery.InsertAllRequest)

Aggregations

BigQuery (com.google.cloud.bigquery.BigQuery)4 Field (com.google.cloud.bigquery.Field)2 Schema (com.google.cloud.bigquery.Schema)2 TableId (com.google.cloud.bigquery.TableId)2 BigQueryOptions (com.google.cloud.bigquery.BigQueryOptions)1 FieldValue (com.google.cloud.bigquery.FieldValue)1 InsertAllRequest (com.google.cloud.bigquery.InsertAllRequest)1 InsertAllResponse (com.google.cloud.bigquery.InsertAllResponse)1 Job (com.google.cloud.bigquery.Job)1 QueryRequest (com.google.cloud.bigquery.QueryRequest)1 QueryResponse (com.google.cloud.bigquery.QueryResponse)1 StandardTableDefinition (com.google.cloud.bigquery.StandardTableDefinition)1 Table (com.google.cloud.bigquery.Table)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1