Search in sources :

Example 96 with TableReference

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

the class FakeDatasetService method createTable.

@Override
public void createTable(Table table) throws IOException {
    TableReference tableReference = table.getTableReference();
    validateWholeTableReference(tableReference);
    synchronized (tables) {
        Map<String, TableContainer> dataset = tables.get(tableReference.getProjectId(), tableReference.getDatasetId());
        if (dataset == null) {
            throwNotFound("Tried to get a dataset %s:%s, but no such table was set", tableReference.getProjectId(), tableReference.getDatasetId());
        }
        dataset.computeIfAbsent(tableReference.getTableId(), k -> {
            TableContainer tableContainer = new TableContainer(table);
            // Create the default stream.
            String streamName = String.format("projects/%s/datasets/%s/tables/%s/streams/_default", tableReference.getProjectId(), tableReference.getDatasetId(), BigQueryHelpers.stripPartitionDecorator(tableReference.getTableId()));
            writeStreams.put(streamName, new Stream(streamName, tableContainer, Type.COMMITTED));
            return tableContainer;
        });
    }
}
Also used : TableReference(com.google.api.services.bigquery.model.TableReference) WriteStream(com.google.cloud.bigquery.storage.v1.WriteStream) ByteString(com.google.protobuf.ByteString) FormatString(com.google.errorprone.annotations.FormatString)

Example 97 with TableReference

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

the class BigQueryResourceNaming method createTempTableReference.

static TableReference createTempTableReference(String projectId, String jobUuid, Optional<String> tempDatasetIdOpt) {
    String tempDatasetId = tempDatasetIdOpt.orElse("temp_dataset_" + jobUuid);
    String queryTempTableId = "temp_table_" + jobUuid;
    return new TableReference().setProjectId(projectId).setDatasetId(tempDatasetId).setTableId(queryTempTableId);
}
Also used : TableReference(com.google.api.services.bigquery.model.TableReference)

Example 98 with TableReference

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

the class BigQuerySourceBase method extractFiles.

protected ExtractResult extractFiles(PipelineOptions options) throws Exception {
    BigQueryOptions bqOptions = options.as(BigQueryOptions.class);
    TableReference tableToExtract = getTableToExtract(bqOptions);
    BigQueryServices.DatasetService datasetService = bqServices.getDatasetService(bqOptions);
    Table table = datasetService.getTable(tableToExtract);
    if (table == null) {
        throw new IOException(String.format("Cannot start an export job since table %s does not exist", BigQueryHelpers.toTableSpec(tableToExtract)));
    }
    TableSchema schema = table.getSchema();
    JobService jobService = bqServices.getJobService(bqOptions);
    String extractJobId = BigQueryResourceNaming.createJobIdPrefix(options.getJobName(), stepUuid, JobType.EXPORT);
    final String extractDestinationDir = resolveTempLocation(bqOptions.getTempLocation(), "BigQueryExtractTemp", stepUuid);
    String bqLocation = BigQueryHelpers.getDatasetLocation(datasetService, tableToExtract.getProjectId(), tableToExtract.getDatasetId());
    List<ResourceId> tempFiles = executeExtract(extractJobId, tableToExtract, jobService, bqOptions.getProject(), extractDestinationDir, bqLocation, useAvroLogicalTypes);
    return new ExtractResult(schema, tempFiles);
}
Also used : TableReference(com.google.api.services.bigquery.model.TableReference) Table(com.google.api.services.bigquery.model.Table) TableSchema(com.google.api.services.bigquery.model.TableSchema) ResourceId(org.apache.beam.sdk.io.fs.ResourceId) IOException(java.io.IOException) JobService(org.apache.beam.sdk.io.gcp.bigquery.BigQueryServices.JobService)

Example 99 with TableReference

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

the class BigQueryTableSource method getEstimatedSizeBytes.

@Override
public synchronized long getEstimatedSizeBytes(PipelineOptions options) throws Exception {
    if (tableSizeBytes.get() == null) {
        BigQueryOptions bqOptions = options.as(BigQueryOptions.class);
        TableReference tableRef = tableDef.getTableReference(bqOptions);
        Table table = bqServices.getDatasetService(bqOptions).getTable(tableRef);
        Long numBytes = table.getNumBytes();
        if (table.getStreamingBuffer() != null && table.getStreamingBuffer().getEstimatedBytes() != null) {
            numBytes += table.getStreamingBuffer().getEstimatedBytes().longValue();
        }
        tableSizeBytes.compareAndSet(null, numBytes);
    }
    return tableSizeBytes.get();
}
Also used : TableReference(com.google.api.services.bigquery.model.TableReference) Table(com.google.api.services.bigquery.model.Table)

Example 100 with TableReference

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

the class BigQueryUtils method toTableReference.

/**
 * @param fullTableId - Is one of the two forms commonly used to refer to bigquery tables in the
 *     beam codebase:
 *     <ul>
 *       <li>projects/{project_id}/datasets/{dataset_id}/tables/{table_id}
 *       <li>myproject:mydataset.mytable
 *       <li>myproject.mydataset.mytable
 *     </ul>
 *
 * @return a BigQueryTableIdentifier by parsing the fullTableId. If it cannot be parsed properly
 *     null is returned.
 */
@Nullable
public static TableReference toTableReference(String fullTableId) {
    // Try parsing the format:
    // "projects/{project_id}/datasets/{dataset_id}/tables/{table_id}"
    Matcher m = TABLE_RESOURCE_PATTERN.matcher(fullTableId);
    if (m.matches()) {
        return new TableReference().setProjectId(m.group("PROJECT")).setDatasetId(m.group("DATASET")).setTableId(m.group("TABLE"));
    }
    // If that failed, try the format:
    // "{project_id}:{dataset_id}.{table_id}" or
    // "{project_id}.{dataset_id}.{table_id}"
    m = SIMPLE_TABLE_PATTERN.matcher(fullTableId);
    if (m.matches()) {
        return new TableReference().setProjectId(m.group("PROJECT")).setDatasetId(m.group("DATASET")).setTableId(m.group("TABLE"));
    }
    return null;
}
Also used : TableReference(com.google.api.services.bigquery.model.TableReference) Matcher(java.util.regex.Matcher) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Aggregations

TableReference (com.google.api.services.bigquery.model.TableReference)139 Test (org.junit.Test)75 TableRow (com.google.api.services.bigquery.model.TableRow)68 Table (com.google.api.services.bigquery.model.Table)61 TableSchema (com.google.api.services.bigquery.model.TableSchema)36 DatasetServiceImpl (org.apache.beam.sdk.io.gcp.bigquery.BigQueryServicesImpl.DatasetServiceImpl)29 TableFieldSchema (com.google.api.services.bigquery.model.TableFieldSchema)22 TableDataInsertAllResponse (com.google.api.services.bigquery.model.TableDataInsertAllResponse)19 FakeBigQueryServices (org.apache.beam.sdk.io.gcp.testing.FakeBigQueryServices)19 MockSleeper (com.google.api.client.testing.util.MockSleeper)17 BigQueryHelpers.createTempTableReference (org.apache.beam.sdk.io.gcp.bigquery.BigQueryHelpers.createTempTableReference)17 FailsafeValueInSingleWindow (org.apache.beam.sdk.values.FailsafeValueInSingleWindow)16 JobStatus (com.google.api.services.bigquery.model.JobStatus)15 ReadSession (com.google.cloud.bigquery.storage.v1.ReadSession)15 BigQueryResourceNaming.createTempTableReference (org.apache.beam.sdk.io.gcp.bigquery.BigQueryResourceNaming.createTempTableReference)15 ErrorProto (com.google.api.services.bigquery.model.ErrorProto)14 JobStatistics (com.google.api.services.bigquery.model.JobStatistics)14 CreateReadSessionRequest (com.google.cloud.bigquery.storage.v1.CreateReadSessionRequest)14 ByteString (com.google.protobuf.ByteString)14 IOException (java.io.IOException)14