Search in sources :

Example 11 with TableName

use of com.google.cloud.bigquery.storage.v1beta2.TableName in project java-bigquerystorage by googleapis.

the class ITBigQueryTimeEncoderTest method TestTimeEncoding.

@Test
public void TestTimeEncoding() throws IOException, InterruptedException, ExecutionException, Descriptors.DescriptorValidationException {
    TableName parent = TableName.of(ServiceOptions.getDefaultProjectId(), DATASET, TABLE);
    TableFieldSchema TEST_STRING = TableFieldSchema.newBuilder().setType(TableFieldSchema.Type.STRING).setMode(TableFieldSchema.Mode.NULLABLE).setName("test_str").build();
    TableFieldSchema TEST_TIME = TableFieldSchema.newBuilder().setType(TableFieldSchema.Type.TIME).setMode(TableFieldSchema.Mode.REPEATED).setName("test_time_micros").build();
    TableFieldSchema TEST_DATETIME = TableFieldSchema.newBuilder().setType(TableFieldSchema.Type.DATETIME).setMode(TableFieldSchema.Mode.REPEATED).setName("test_datetime_micros").build();
    TableSchema tableSchema = TableSchema.newBuilder().addFields(0, TEST_STRING).addFields(1, TEST_TIME).addFields(2, TEST_DATETIME).build();
    try (JsonStreamWriter jsonStreamWriter = JsonStreamWriter.newBuilder(parent.toString(), tableSchema).build()) {
        JSONObject row = new JSONObject();
        row.put("test_str", "Start of the day");
        row.put("test_time_micros", new JSONArray(new long[] { CivilTimeEncoder.encodePacked64TimeMicros(LocalTime.of(13, 14, 15, 16_000_000)), CivilTimeEncoder.encodePacked64TimeMicros(LocalTime.of(23, 59, 59, 999_999_000)), CivilTimeEncoder.encodePacked64TimeMicros(LocalTime.of(0, 0, 0, 0)), CivilTimeEncoder.encodePacked64TimeMicros(LocalTime.of(1, 2, 3, 4_000)), CivilTimeEncoder.encodePacked64TimeMicros(LocalTime.of(5, 6, 7, 8_000)) }));
        row.put("test_datetime_micros", new JSONArray(new long[] { CivilTimeEncoder.encodePacked64DatetimeMicros(LocalDateTime.of(1, 1, 1, 12, 0, 0, 0)), CivilTimeEncoder.encodePacked64DatetimeMicros(LocalDateTime.of(1995, 5, 19, 10, 30, 45, 0)), CivilTimeEncoder.encodePacked64DatetimeMicros(LocalDateTime.of(2000, 1, 1, 0, 0, 0, 0)), CivilTimeEncoder.encodePacked64DatetimeMicros(LocalDateTime.of(2026, 3, 11, 5, 45, 12, 9_000_000)), CivilTimeEncoder.encodePacked64DatetimeMicros(LocalDateTime.of(2050, 1, 2, 3, 4, 5, 6_000)) }));
        JSONArray jsonArr = new JSONArray(new JSONObject[] { row });
        ApiFuture<AppendRowsResponse> response = jsonStreamWriter.append(jsonArr, -1);
        Assert.assertFalse(response.get().getAppendResult().hasOffset());
        TableResult result = bigquery.listTableData(tableInfo.getTableId(), BigQuery.TableDataListOption.startIndex(0L));
        Iterator<FieldValueList> iter = result.getValues().iterator();
        FieldValueList currentRow;
        currentRow = iter.next();
        assertEquals("Start of the day", currentRow.get(0).getValue());
        assertEquals("13:14:15.016000", currentRow.get(1).getRepeatedValue().get(0).getStringValue());
        assertEquals("23:59:59.999999", currentRow.get(1).getRepeatedValue().get(1).getStringValue());
        assertEquals("00:00:00", currentRow.get(1).getRepeatedValue().get(2).getStringValue());
        assertEquals("01:02:03.000004", currentRow.get(1).getRepeatedValue().get(3).getStringValue());
        assertEquals("05:06:07.000008", currentRow.get(1).getRepeatedValue().get(4).getStringValue());
        assertEquals("0001-01-01T12:00:00", currentRow.get(2).getRepeatedValue().get(0).getStringValue());
        assertEquals("1995-05-19T10:30:45", currentRow.get(2).getRepeatedValue().get(1).getStringValue());
        assertEquals("2000-01-01T00:00:00", currentRow.get(2).getRepeatedValue().get(2).getStringValue());
        assertEquals("2026-03-11T05:45:12.009000", currentRow.get(2).getRepeatedValue().get(3).getStringValue());
        assertEquals("2050-01-02T03:04:05.000006", currentRow.get(2).getRepeatedValue().get(4).getStringValue());
    }
}
Also used : TableName(com.google.cloud.bigquery.storage.v1beta2.TableName) TableResult(com.google.cloud.bigquery.TableResult) TableSchema(com.google.cloud.bigquery.storage.v1beta2.TableSchema) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) AppendRowsResponse(com.google.cloud.bigquery.storage.v1beta2.AppendRowsResponse) FieldValueList(com.google.cloud.bigquery.FieldValueList) TableFieldSchema(com.google.cloud.bigquery.storage.v1beta2.TableFieldSchema) JsonStreamWriter(com.google.cloud.bigquery.storage.v1beta2.JsonStreamWriter) Test(org.junit.Test)

Example 12 with TableName

use of com.google.cloud.bigquery.storage.v1beta2.TableName in project java-bigquerystorage by googleapis.

the class ITBigQueryBigDecimalByteStringEncoderTest method TestBigDecimalEncoding.

@Test
public void TestBigDecimalEncoding() throws IOException, InterruptedException, ExecutionException, Descriptors.DescriptorValidationException {
    TableName parent = TableName.of(ServiceOptions.getDefaultProjectId(), DATASET, TABLE);
    TableFieldSchema TEST_NUMERIC_ZERO = TableFieldSchema.newBuilder().setType(TableFieldSchema.Type.NUMERIC).setMode(TableFieldSchema.Mode.NULLABLE).setName("test_numeric_zero").build();
    TableFieldSchema TEST_NUMERIC_ONE = TableFieldSchema.newBuilder().setType(TableFieldSchema.Type.NUMERIC).setMode(TableFieldSchema.Mode.NULLABLE).setName("test_numeric_one").build();
    TableFieldSchema TEST_NUMERIC_REPEATED = TableFieldSchema.newBuilder().setType(TableFieldSchema.Type.NUMERIC).setMode(TableFieldSchema.Mode.REPEATED).setName("test_numeric_repeated").build();
    TableSchema tableSchema = TableSchema.newBuilder().addFields(0, TEST_NUMERIC_ZERO).addFields(1, TEST_NUMERIC_ONE).addFields(2, TEST_NUMERIC_REPEATED).build();
    try (JsonStreamWriter jsonStreamWriter = JsonStreamWriter.newBuilder(parent.toString(), tableSchema).build()) {
        JSONObject row = new JSONObject();
        row.put("test_numeric_zero", BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal("0")));
        row.put("test_numeric_one", BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal("1.2")));
        row.put("test_numeric_repeated", new JSONArray(new byte[][] { BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal("0")).toByteArray(), BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal("1.2")).toByteArray(), BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal("-1.2")).toByteArray(), BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal("99999999999999999999999999999.999999999")).toByteArray(), BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal("-99999999999999999999999999999.999999999")).toByteArray() }));
        JSONArray jsonArr = new JSONArray(new JSONObject[] { row });
        ApiFuture<AppendRowsResponse> response = jsonStreamWriter.append(jsonArr, -1);
        AppendRowsResponse arr = response.get();
        AppendResult ar = arr.getAppendResult();
        boolean ho = ar.hasOffset();
        TableResult result = bigquery.listTableData(tableInfo.getTableId(), BigQuery.TableDataListOption.startIndex(0L));
        Iterator<FieldValueList> iter = result.getValues().iterator();
        FieldValueList currentRow;
        currentRow = iter.next();
        assertEquals("0", currentRow.get(0).getStringValue());
        assertEquals("1.2", currentRow.get(1).getStringValue());
        assertEquals("0", currentRow.get(2).getRepeatedValue().get(0).getStringValue());
        assertEquals("1.2", currentRow.get(2).getRepeatedValue().get(1).getStringValue());
        assertEquals("-1.2", currentRow.get(2).getRepeatedValue().get(2).getStringValue());
        assertEquals("99999999999999999999999999999.999999999", currentRow.get(2).getRepeatedValue().get(3).getStringValue());
        assertEquals("-99999999999999999999999999999.999999999", currentRow.get(2).getRepeatedValue().get(4).getStringValue());
    }
}
Also used : TableSchema(com.google.cloud.bigquery.storage.v1beta2.TableSchema) JSONArray(org.json.JSONArray) AppendRowsResponse(com.google.cloud.bigquery.storage.v1beta2.AppendRowsResponse) TableFieldSchema(com.google.cloud.bigquery.storage.v1beta2.TableFieldSchema) BigDecimal(java.math.BigDecimal) TableName(com.google.cloud.bigquery.storage.v1beta2.TableName) TableResult(com.google.cloud.bigquery.TableResult) JSONObject(org.json.JSONObject) AppendResult(com.google.cloud.bigquery.storage.v1beta2.AppendRowsResponse.AppendResult) FieldValueList(com.google.cloud.bigquery.FieldValueList) JsonStreamWriter(com.google.cloud.bigquery.storage.v1beta2.JsonStreamWriter) Test(org.junit.Test)

Example 13 with TableName

use of com.google.cloud.bigquery.storage.v1beta2.TableName in project java-bigquerystorage by googleapis.

the class ITBigQueryTimeEncoderTest method TestTimeEncoding.

@Test
public void TestTimeEncoding() throws IOException, InterruptedException, ExecutionException, Descriptors.DescriptorValidationException {
    TableName parent = TableName.of(ServiceOptions.getDefaultProjectId(), DATASET, TABLE);
    TableFieldSchema TEST_STRING = TableFieldSchema.newBuilder().setType(TableFieldSchema.Type.STRING).setMode(TableFieldSchema.Mode.NULLABLE).setName("test_str").build();
    TableFieldSchema TEST_TIME = TableFieldSchema.newBuilder().setType(TableFieldSchema.Type.TIME).setMode(TableFieldSchema.Mode.REPEATED).setName("test_time_micros").build();
    TableFieldSchema TEST_DATETIME = TableFieldSchema.newBuilder().setType(TableFieldSchema.Type.DATETIME).setMode(TableFieldSchema.Mode.REPEATED).setName("test_datetime_micros").build();
    TableFieldSchema TEST_DATE_REPEATED = TableFieldSchema.newBuilder().setType(TableFieldSchema.Type.DATE).setMode(TableFieldSchema.Mode.REPEATED).setName("test_date_repeated").build();
    TableFieldSchema TEST_DATE = TableFieldSchema.newBuilder().setType(TableFieldSchema.Type.DATE).setMode(TableFieldSchema.Mode.NULLABLE).setName("test_date").build();
    TableSchema tableSchema = TableSchema.newBuilder().addFields(0, TEST_STRING).addFields(1, TEST_TIME).addFields(2, TEST_DATETIME).addFields(3, TEST_DATE_REPEATED).addFields(4, TEST_DATE).build();
    try (JsonStreamWriter jsonStreamWriter = JsonStreamWriter.newBuilder(parent.toString(), tableSchema).build()) {
        JSONObject row = new JSONObject();
        row.put("test_str", "Start of the day");
        row.put("test_time_micros", new JSONArray(new long[] { CivilTimeEncoder.encodePacked64TimeMicros(LocalTime.of(13, 14, 15, 16_000_000)), CivilTimeEncoder.encodePacked64TimeMicros(LocalTime.of(23, 59, 59, 999_999_000)), CivilTimeEncoder.encodePacked64TimeMicros(LocalTime.of(0, 0, 0, 0)), CivilTimeEncoder.encodePacked64TimeMicros(LocalTime.of(1, 2, 3, 4_000)), CivilTimeEncoder.encodePacked64TimeMicros(LocalTime.of(5, 6, 7, 8_000)) }));
        row.put("test_datetime_micros", new JSONArray(new long[] { CivilTimeEncoder.encodePacked64DatetimeMicros(LocalDateTime.of(1, 1, 1, 12, 0, 0, 0)), CivilTimeEncoder.encodePacked64DatetimeMicros(LocalDateTime.of(1995, 5, 19, 10, 30, 45, 0)), CivilTimeEncoder.encodePacked64DatetimeMicros(LocalDateTime.of(2000, 1, 1, 0, 0, 0, 0)), CivilTimeEncoder.encodePacked64DatetimeMicros(LocalDateTime.of(2026, 3, 11, 5, 45, 12, 9_000_000)), CivilTimeEncoder.encodePacked64DatetimeMicros(LocalDateTime.of(2050, 1, 2, 3, 4, 5, 6_000)) }));
        row.put("test_date_repeated", new JSONArray(new int[] { 0, 300, 14238 }));
        row.put("test_date", 300);
        JSONArray jsonArr = new JSONArray(new JSONObject[] { row });
        ApiFuture<AppendRowsResponse> response = jsonStreamWriter.append(jsonArr, -1);
        Assert.assertFalse(response.get().getAppendResult().hasOffset());
        TableResult result = bigquery.listTableData(tableInfo.getTableId(), BigQuery.TableDataListOption.startIndex(0L));
        Iterator<FieldValueList> iter = result.getValues().iterator();
        FieldValueList currentRow;
        currentRow = iter.next();
        assertEquals("Start of the day", currentRow.get(0).getValue());
        assertEquals("13:14:15.016000", currentRow.get(1).getRepeatedValue().get(0).getStringValue());
        assertEquals("23:59:59.999999", currentRow.get(1).getRepeatedValue().get(1).getStringValue());
        assertEquals("00:00:00", currentRow.get(1).getRepeatedValue().get(2).getStringValue());
        assertEquals("01:02:03.000004", currentRow.get(1).getRepeatedValue().get(3).getStringValue());
        assertEquals("05:06:07.000008", currentRow.get(1).getRepeatedValue().get(4).getStringValue());
        assertEquals("0001-01-01T12:00:00", currentRow.get(2).getRepeatedValue().get(0).getStringValue());
        assertEquals("1995-05-19T10:30:45", currentRow.get(2).getRepeatedValue().get(1).getStringValue());
        assertEquals("2000-01-01T00:00:00", currentRow.get(2).getRepeatedValue().get(2).getStringValue());
        assertEquals("1970-01-01", currentRow.get(3).getRepeatedValue().get(0).getStringValue());
        assertEquals("1970-10-28", currentRow.get(3).getRepeatedValue().get(1).getStringValue());
        assertEquals("2008-12-25", currentRow.get(3).getRepeatedValue().get(2).getStringValue());
        assertEquals("1970-10-28", currentRow.get(4).getStringValue());
        assertEquals("2026-03-11T05:45:12.009000", currentRow.get(2).getRepeatedValue().get(3).getStringValue());
        assertEquals("2050-01-02T03:04:05.000006", currentRow.get(2).getRepeatedValue().get(4).getStringValue());
    }
}
Also used : TableName(com.google.cloud.bigquery.storage.v1beta2.TableName) TableResult(com.google.cloud.bigquery.TableResult) TableSchema(com.google.cloud.bigquery.storage.v1beta2.TableSchema) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) AppendRowsResponse(com.google.cloud.bigquery.storage.v1beta2.AppendRowsResponse) FieldValueList(com.google.cloud.bigquery.FieldValueList) TableFieldSchema(com.google.cloud.bigquery.storage.v1beta2.TableFieldSchema) JsonStreamWriter(com.google.cloud.bigquery.storage.v1beta2.JsonStreamWriter) Test(org.junit.Test)

Example 14 with TableName

use of com.google.cloud.bigquery.storage.v1beta2.TableName in project java-bigquerystorage by googleapis.

the class ITBigQueryBigDecimalByteStringEncoderTest method TestBigDecimalEncoding.

@Test
public void TestBigDecimalEncoding() throws IOException, InterruptedException, ExecutionException, Descriptors.DescriptorValidationException {
    TableName parent = TableName.of(ServiceOptions.getDefaultProjectId(), DATASET, TABLE);
    TableFieldSchema TEST_NUMERIC_ZERO = TableFieldSchema.newBuilder().setType(TableFieldSchema.Type.NUMERIC).setMode(TableFieldSchema.Mode.NULLABLE).setName("test_numeric_zero").build();
    TableFieldSchema TEST_NUMERIC_ONE = TableFieldSchema.newBuilder().setType(TableFieldSchema.Type.NUMERIC).setMode(TableFieldSchema.Mode.NULLABLE).setName("test_numeric_one").build();
    TableFieldSchema TEST_NUMERIC_REPEATED = TableFieldSchema.newBuilder().setType(TableFieldSchema.Type.NUMERIC).setMode(TableFieldSchema.Mode.REPEATED).setName("test_numeric_repeated").build();
    TableSchema tableSchema = TableSchema.newBuilder().addFields(0, TEST_NUMERIC_ZERO).addFields(1, TEST_NUMERIC_ONE).addFields(2, TEST_NUMERIC_REPEATED).build();
    try (JsonStreamWriter jsonStreamWriter = JsonStreamWriter.newBuilder(parent.toString(), tableSchema).build()) {
        JSONObject row = new JSONObject();
        row.put("test_numeric_zero", BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal("0")));
        row.put("test_numeric_one", BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal("1.2")));
        row.put("test_numeric_repeated", new JSONArray(new byte[][] { BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal("0")).toByteArray(), BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal("1.2")).toByteArray(), BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal("-1.2")).toByteArray(), BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal("99999999999999999999999999999.999999999")).toByteArray(), BigDecimalByteStringEncoder.encodeToNumericByteString(new BigDecimal("-99999999999999999999999999999.999999999")).toByteArray() }));
        JSONArray jsonArr = new JSONArray(new JSONObject[] { row });
        ApiFuture<AppendRowsResponse> response = jsonStreamWriter.append(jsonArr, -1);
        AppendRowsResponse arr = response.get();
        AppendResult ar = arr.getAppendResult();
        boolean ho = ar.hasOffset();
        TableResult result = bigquery.listTableData(tableInfo.getTableId(), BigQuery.TableDataListOption.startIndex(0L));
        Iterator<FieldValueList> iter = result.getValues().iterator();
        FieldValueList currentRow;
        currentRow = iter.next();
        assertEquals("0", currentRow.get(0).getStringValue());
        assertEquals("1.2", currentRow.get(1).getStringValue());
        assertEquals("0", currentRow.get(2).getRepeatedValue().get(0).getStringValue());
        assertEquals("1.2", currentRow.get(2).getRepeatedValue().get(1).getStringValue());
        assertEquals("-1.2", currentRow.get(2).getRepeatedValue().get(2).getStringValue());
        assertEquals("99999999999999999999999999999.999999999", currentRow.get(2).getRepeatedValue().get(3).getStringValue());
        assertEquals("-99999999999999999999999999999.999999999", currentRow.get(2).getRepeatedValue().get(4).getStringValue());
    }
}
Also used : TableSchema(com.google.cloud.bigquery.storage.v1beta2.TableSchema) JSONArray(org.json.JSONArray) AppendRowsResponse(com.google.cloud.bigquery.storage.v1beta2.AppendRowsResponse) TableFieldSchema(com.google.cloud.bigquery.storage.v1beta2.TableFieldSchema) BigDecimal(java.math.BigDecimal) TableName(com.google.cloud.bigquery.storage.v1beta2.TableName) TableResult(com.google.cloud.bigquery.TableResult) JSONObject(org.json.JSONObject) AppendResult(com.google.cloud.bigquery.storage.v1beta2.AppendRowsResponse.AppendResult) FieldValueList(com.google.cloud.bigquery.FieldValueList) JsonStreamWriter(com.google.cloud.bigquery.storage.v1beta2.JsonStreamWriter) Test(org.junit.Test)

Example 15 with TableName

use of com.google.cloud.bigquery.storage.v1beta2.TableName in project java-bigquerystorage by googleapis.

the class ParallelWriteCommittedStream method createStream.

private WriteStream createStream(String projectId, String datasetName, String tableName, BigQueryWriteClient client) {
    LOG.info("Creating a new stream");
    // Initialize a write stream for the specified table.
    // For more information on WriteStream.Type, see:
    // https://googleapis.dev/java/google-cloud-bigquerystorage/latest/com/google/cloud/bigquery/storage/v1/WriteStream.Type.html
    WriteStream stream = WriteStream.newBuilder().setType(WriteStream.Type.COMMITTED).build();
    TableName parentTable = TableName.of(projectId, datasetName, tableName);
    CreateWriteStreamRequest createWriteStreamRequest = CreateWriteStreamRequest.newBuilder().setParent(parentTable.toString()).setWriteStream(stream).build();
    return client.createWriteStream(createWriteStreamRequest);
}
Also used : TableName(com.google.cloud.bigquery.storage.v1.TableName) CreateWriteStreamRequest(com.google.cloud.bigquery.storage.v1.CreateWriteStreamRequest) WriteStream(com.google.cloud.bigquery.storage.v1.WriteStream)

Aggregations

Test (org.junit.Test)33 ByteString (com.google.protobuf.ByteString)28 ArrayList (java.util.ArrayList)20 TableName (com.google.bigtable.v2.TableName)18 TableName (com.google.bigtable.admin.v2.TableName)17 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)13 AbstractMessage (com.google.protobuf.AbstractMessage)13 StatusRuntimeException (io.grpc.StatusRuntimeException)13 Mutation (com.google.bigtable.v2.Mutation)12 JSONArray (org.json.JSONArray)9 JSONObject (org.json.JSONObject)9 ExecutionException (java.util.concurrent.ExecutionException)8 TableName (com.google.cloud.bigquery.storage.v1.TableName)7 ReadModifyWriteRule (com.google.bigtable.v2.ReadModifyWriteRule)6 RowFilter (com.google.bigtable.v2.RowFilter)6 BaseBigtableDataClient (com.google.cloud.bigtable.data.v2.BaseBigtableDataClient)6 ColumnFamily (com.google.bigtable.admin.v2.ColumnFamily)5 CheckAndMutateRowResponse (com.google.bigtable.v2.CheckAndMutateRowResponse)4 ReadModifyWriteRowResponse (com.google.bigtable.v2.ReadModifyWriteRowResponse)4 FieldValueList (com.google.cloud.bigquery.FieldValueList)4