Search in sources :

Example 1 with DatasetReference

use of com.google.api.services.bigquery.model.DatasetReference in project beam by apache.

the class ExampleUtils method setupBigQueryTable.

private void setupBigQueryTable(String projectId, String datasetId, String tableId, TableSchema schema) throws IOException {
    if (bigQueryClient == null) {
        bigQueryClient = newBigQueryClient(options.as(BigQueryOptions.class)).build();
    }
    Datasets datasetService = bigQueryClient.datasets();
    if (executeNullIfNotFound(datasetService.get(projectId, datasetId)) == null) {
        Dataset newDataset = new Dataset().setDatasetReference(new DatasetReference().setProjectId(projectId).setDatasetId(datasetId));
        datasetService.insert(projectId, newDataset).execute();
    }
    Tables tableService = bigQueryClient.tables();
    Table table = executeNullIfNotFound(tableService.get(projectId, datasetId, tableId));
    if (table == null) {
        Table newTable = new Table().setSchema(schema).setTableReference(new TableReference().setProjectId(projectId).setDatasetId(datasetId).setTableId(tableId));
        tableService.insert(projectId, datasetId, newTable).execute();
    } else if (!table.getSchema().equals(schema)) {
        throw new RuntimeException("Table exists and schemas do not match, expecting: " + schema.toPrettyString() + ", actual: " + table.getSchema().toPrettyString());
    }
}
Also used : Datasets(com.google.api.services.bigquery.Bigquery.Datasets) DatasetReference(com.google.api.services.bigquery.model.DatasetReference) TableReference(com.google.api.services.bigquery.model.TableReference) Table(com.google.api.services.bigquery.model.Table) Dataset(com.google.api.services.bigquery.model.Dataset) Tables(com.google.api.services.bigquery.Bigquery.Tables) BigQueryOptions(org.apache.beam.sdk.io.gcp.bigquery.BigQueryOptions)

Example 2 with DatasetReference

use of com.google.api.services.bigquery.model.DatasetReference in project DataflowJavaSDK-examples by GoogleCloudPlatform.

the class ExampleUtils method setupBigQueryTable.

private void setupBigQueryTable(String projectId, String datasetId, String tableId, TableSchema schema) throws IOException {
    if (bigQueryClient == null) {
        bigQueryClient = newBigQueryClient(options.as(BigQueryOptions.class)).build();
    }
    Datasets datasetService = bigQueryClient.datasets();
    if (executeNullIfNotFound(datasetService.get(projectId, datasetId)) == null) {
        Dataset newDataset = new Dataset().setDatasetReference(new DatasetReference().setProjectId(projectId).setDatasetId(datasetId));
        datasetService.insert(projectId, newDataset).execute();
    }
    Tables tableService = bigQueryClient.tables();
    Table table = executeNullIfNotFound(tableService.get(projectId, datasetId, tableId));
    if (table == null) {
        Table newTable = new Table().setSchema(schema).setTableReference(new TableReference().setProjectId(projectId).setDatasetId(datasetId).setTableId(tableId));
        tableService.insert(projectId, datasetId, newTable).execute();
    } else if (!table.getSchema().equals(schema)) {
        throw new RuntimeException("Table exists and schemas do not match, expecting: " + schema.toPrettyString() + ", actual: " + table.getSchema().toPrettyString());
    }
}
Also used : Datasets(com.google.api.services.bigquery.Bigquery.Datasets) DatasetReference(com.google.api.services.bigquery.model.DatasetReference) TableReference(com.google.api.services.bigquery.model.TableReference) Table(com.google.api.services.bigquery.model.Table) Dataset(com.google.api.services.bigquery.model.Dataset) Tables(com.google.api.services.bigquery.Bigquery.Tables) BigQueryOptions(org.apache.beam.sdk.io.gcp.bigquery.BigQueryOptions)

Example 3 with DatasetReference

use of com.google.api.services.bigquery.model.DatasetReference in project beam by apache.

the class BigQueryTableRowIterator method createDataset.

// Create a new BigQuery dataset
private void createDataset(String datasetId, @Nullable String location) throws IOException, InterruptedException {
    Dataset dataset = new Dataset();
    DatasetReference reference = new DatasetReference();
    reference.setProjectId(projectId);
    reference.setDatasetId(datasetId);
    dataset.setDatasetReference(reference);
    if (location != null) {
        dataset.setLocation(location);
    }
    executeWithBackOff(client.datasets().insert(projectId, dataset), String.format("Error when trying to create the temporary dataset %s in project %s.", datasetId, projectId));
}
Also used : DatasetReference(com.google.api.services.bigquery.model.DatasetReference) Dataset(com.google.api.services.bigquery.model.Dataset)

Aggregations

Dataset (com.google.api.services.bigquery.model.Dataset)3 DatasetReference (com.google.api.services.bigquery.model.DatasetReference)3 Datasets (com.google.api.services.bigquery.Bigquery.Datasets)2 Tables (com.google.api.services.bigquery.Bigquery.Tables)2 Table (com.google.api.services.bigquery.model.Table)2 TableReference (com.google.api.services.bigquery.model.TableReference)2 BigQueryOptions (org.apache.beam.sdk.io.gcp.bigquery.BigQueryOptions)2