Search in sources :

Example 1 with AppendResult

use of com.google.cloud.bigquery.storage.v1beta2.AppendRowsResponse.AppendResult 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 2 with AppendResult

use of com.google.cloud.bigquery.storage.v1beta2.AppendRowsResponse.AppendResult 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)

Aggregations

FieldValueList (com.google.cloud.bigquery.FieldValueList)2 TableResult (com.google.cloud.bigquery.TableResult)2 AppendRowsResponse (com.google.cloud.bigquery.storage.v1beta2.AppendRowsResponse)2 AppendResult (com.google.cloud.bigquery.storage.v1beta2.AppendRowsResponse.AppendResult)2 JsonStreamWriter (com.google.cloud.bigquery.storage.v1beta2.JsonStreamWriter)2 TableFieldSchema (com.google.cloud.bigquery.storage.v1beta2.TableFieldSchema)2 TableName (com.google.cloud.bigquery.storage.v1beta2.TableName)2 TableSchema (com.google.cloud.bigquery.storage.v1beta2.TableSchema)2 BigDecimal (java.math.BigDecimal)2 JSONArray (org.json.JSONArray)2 JSONObject (org.json.JSONObject)2 Test (org.junit.Test)2