/** Example of loading a parquet file from GCS to a table. */ public void loadTableGcsParquet(String datasetName) throws InterruptedException { // [START bigquery_load_table_gcs_parquet] String sourceUri = "gs://cloud-samples-data/bigquery/us-states/us-states.parquet"; TableId tableId = TableId.of(datasetName, "us_states"); LoadJobConfiguration configuration = LoadJobConfiguration.builder(tableId, sourceUri) .setFormatOptions(FormatOptions.parquet()) .build(); // Load the table Job loadJob = bigquery.create(JobInfo.of(configuration)); loadJob = loadJob.waitFor(); // Check the table StandardTableDefinition destinationTable = bigquery.getTable(tableId).getDefinition(); System.out.println("State: " + loadJob.getStatus().getState()); System.out.printf("Loaded %d rows.\n", destinationTable.getNumRows()); // [END bigquery_load_table_gcs_parquet] }
/** Example of copying multiple tables to a destination. */ public void copyTables(String datasetId, String destinationTableId) throws InterruptedException { generateTableWithDdl(datasetId, "table1"); generateTableWithDdl(datasetId, "table2"); // [START bigquery_copy_table_multiple_source] TableId destinationTable = TableId.of(datasetId, destinationTableId); CopyJobConfiguration configuration = CopyJobConfiguration.newBuilder( destinationTable, Arrays.asList(TableId.of(datasetId, "table1"), TableId.of(datasetId, "table2"))) .build(); // Copy the tables. Job job = bigquery.create(JobInfo.of(configuration)); job = job.waitFor(); // Check the table StandardTableDefinition table = bigquery.getTable(destinationTable).getDefinition(); System.out.println("State: " + job.getStatus().getState()); System.out.printf("Copied %d rows.\n", table.getNumRows()); // [END bigquery_copy_table_multiple_source] }
@Override Table toPb() { Table tablePb = super.toPb(); if (getNumRows() != null) { tablePb.setNumRows(BigInteger.valueOf(getNumRows())); } tablePb.setNumBytes(getNumBytes()); tablePb.setLocation(getLocation()); if (getStreamingBuffer() != null) { tablePb.setStreamingBuffer(getStreamingBuffer().toPb()); } if (getTimePartitioning() != null) { tablePb.setTimePartitioning(getTimePartitioning().toPb()); } if (getClustering() != null) { tablePb.setClustering(getClustering().toPb()); } return tablePb; }
bigquery.getTable(TableId.of(datasetId, recoverTableId)).getDefinition(); System.out.println("State: " + job.getStatus().getState()); System.out.printf("Recovered %d rows.\n", table.getNumRows());
return ((StandardTableDefinition) bigquery.getTable(tableId).getDefinition()).getNumRows();
private void compareStandardTableDefinition( StandardTableDefinition expected, StandardTableDefinition value) { assertEquals(expected, value); assertEquals(expected.getSchema(), value.getSchema()); assertEquals(expected.getType(), value.getType()); assertEquals(expected.getNumBytes(), value.getNumBytes()); assertEquals(expected.getNumRows(), value.getNumRows()); assertEquals(expected.getLocation(), value.getLocation()); assertEquals(expected.getStreamingBuffer(), value.getStreamingBuffer()); assertEquals(expected.getType(), value.getType()); assertEquals(expected.getTimePartitioning(), value.getTimePartitioning()); assertEquals(expected.getClustering(), value.getClustering()); assertEquals(expected.hashCode(), value.hashCode()); } }
@Test public void testOf() { StandardTableDefinition definition = StandardTableDefinition.of(TABLE_SCHEMA); assertEquals(TableDefinition.Type.TABLE, TABLE_DEFINITION.getType()); assertEquals(TABLE_SCHEMA, TABLE_DEFINITION.getSchema()); assertNull(definition.getLocation()); assertNull(definition.getNumBytes()); assertNull(definition.getNumRows()); assertNull(definition.getStreamingBuffer()); assertNull(definition.getTimePartitioning()); assertNull(definition.getClustering()); }
@Test public void testBuilder() { assertEquals(TableDefinition.Type.TABLE, TABLE_DEFINITION.getType()); assertEquals(TABLE_SCHEMA, TABLE_DEFINITION.getSchema()); assertEquals(LOCATION, TABLE_DEFINITION.getLocation()); assertEquals(NUM_BYTES, TABLE_DEFINITION.getNumBytes()); assertEquals(NUM_ROWS, TABLE_DEFINITION.getNumRows()); assertEquals(STREAMING_BUFFER, TABLE_DEFINITION.getStreamingBuffer()); assertEquals(TIME_PARTITIONING, TABLE_DEFINITION.getTimePartitioning()); assertEquals(CLUSTERING, TABLE_DEFINITION.getClustering()); }
@Test public void testCreateAndGetTableWithSelectedField() { String tableName = "test_create_and_get_selected_fields_table"; TableId tableId = TableId.of(DATASET, tableName); StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA); Table createdTable = bigquery.create( TableInfo.newBuilder(tableId, tableDefinition) .setLabels(Collections.singletonMap("a", "b")) .build()); assertNotNull(createdTable); assertEquals(DATASET, createdTable.getTableId().getDataset()); assertEquals(tableName, createdTable.getTableId().getTable()); Table remoteTable = bigquery.getTable( DATASET, tableName, TableOption.fields(TableField.CREATION_TIME, TableField.LABELS)); assertNotNull(remoteTable); assertTrue(remoteTable.getDefinition() instanceof StandardTableDefinition); assertEquals(createdTable.getTableId(), remoteTable.getTableId()); assertEquals(TableDefinition.Type.TABLE, remoteTable.getDefinition().getType()); assertThat(remoteTable.getLabels()).containsExactly("a", "b"); assertNotNull(remoteTable.getCreationTime()); assertNull(remoteTable.getDefinition().getSchema()); assertNull(remoteTable.getLastModifiedTime()); assertNull(remoteTable.<StandardTableDefinition>getDefinition().getNumBytes()); assertNull(remoteTable.<StandardTableDefinition>getDefinition().getNumRows()); assertNull(remoteTable.<StandardTableDefinition>getDefinition().getTimePartitioning()); assertNull(remoteTable.<StandardTableDefinition>getDefinition().getClustering()); assertTrue(remoteTable.delete()); }
@Test public void testUpdateTableWithSelectedFields() { String tableName = "test_update_with_selected_fields_table"; StandardTableDefinition tableDefinition = StandardTableDefinition.of(TABLE_SCHEMA); TableInfo tableInfo = TableInfo.of(TableId.of(DATASET, tableName), tableDefinition); Table createdTable = bigquery.create(tableInfo); assertNotNull(createdTable); Table updatedTable = bigquery.update( tableInfo.toBuilder().setDescription("newDescr").build(), TableOption.fields(TableField.DESCRIPTION)); assertTrue(updatedTable.getDefinition() instanceof StandardTableDefinition); assertEquals(DATASET, updatedTable.getTableId().getDataset()); assertEquals(tableName, updatedTable.getTableId().getTable()); assertEquals("newDescr", updatedTable.getDescription()); assertNull(updatedTable.getDefinition().getSchema()); assertNull(updatedTable.getLastModifiedTime()); assertNull(updatedTable.<StandardTableDefinition>getDefinition().getNumBytes()); assertNull(updatedTable.<StandardTableDefinition>getDefinition().getNumRows()); assertTrue(createdTable.delete()); }
assertNotNull(remoteTable.getLastModifiedTime()); assertNotNull(remoteTable.<StandardTableDefinition>getDefinition().getNumBytes()); assertNotNull(remoteTable.<StandardTableDefinition>getDefinition().getNumRows()); assertEquals( partitioning, remoteTable.<StandardTableDefinition>getDefinition().getTimePartitioning());
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof StandardTableDefinition) { StandardTableDefinition that = (StandardTableDefinition) o; return (this.type.equals(that.getType())) && ((this.schema == null) ? (that.getSchema() == null) : this.schema.equals(that.getSchema())) && ((this.numBytes == null) ? (that.getNumBytes() == null) : this.numBytes.equals(that.getNumBytes())) && ((this.numRows == null) ? (that.getNumRows() == null) : this.numRows.equals(that.getNumRows())) && ((this.location == null) ? (that.getLocation() == null) : this.location.equals(that.getLocation())) && ((this.streamingBuffer == null) ? (that.getStreamingBuffer() == null) : this.streamingBuffer.equals(that.getStreamingBuffer())) && ((this.timePartitioning == null) ? (that.getTimePartitioning() == null) : this.timePartitioning.equals(that.getTimePartitioning())) && ((this.clustering == null) ? (that.getClustering() == null) : this.clustering.equals(that.getClustering())); } return false; }
private Builder(StandardTableDefinition source) { this.type = source.getType(); this.schema = source.getSchema(); this.numBytes = source.getNumBytes(); this.numRows = source.getNumRows(); this.location = source.getLocation(); this.streamingBuffer = source.getStreamingBuffer(); this.timePartitioning = source.getTimePartitioning(); this.clustering = source.getClustering(); } @Override
@Override Table toPb() { Table tablePb = super.toPb(); if (getNumRows() != null) { tablePb.setNumRows(BigInteger.valueOf(getNumRows())); } tablePb.setNumBytes(getNumBytes()); tablePb.setLocation(getLocation()); if (getStreamingBuffer() != null) { tablePb.setStreamingBuffer(getStreamingBuffer().toPb()); } if (getTimePartitioning() != null) { tablePb.setTimePartitioning(getTimePartitioning().toPb()); } if (getClustering() != null) { tablePb.setClustering(getClustering().toPb()); } return tablePb; }