/
ITBigQueryBigDecimalByteStringEncoderTest.java
157 lines (148 loc) · 6.76 KB
/
ITBigQueryBigDecimalByteStringEncoderTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.bigquery.storage.v1beta2.it;
import static org.junit.Assert.assertEquals;
import com.google.api.core.ApiFuture;
import com.google.cloud.ServiceOptions;
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.DatasetInfo;
import com.google.cloud.bigquery.Field.Mode;
import com.google.cloud.bigquery.FieldValueList;
import com.google.cloud.bigquery.Schema;
import com.google.cloud.bigquery.StandardSQLTypeName;
import com.google.cloud.bigquery.StandardTableDefinition;
import com.google.cloud.bigquery.TableId;
import com.google.cloud.bigquery.TableInfo;
import com.google.cloud.bigquery.TableResult;
import com.google.cloud.bigquery.storage.v1beta2.AppendRowsResponse;
import com.google.cloud.bigquery.storage.v1beta2.AppendRowsResponse.AppendResult;
import com.google.cloud.bigquery.storage.v1beta2.BigDecimalByteStringEncoder;
import com.google.cloud.bigquery.storage.v1beta2.BigQueryWriteClient;
import com.google.cloud.bigquery.storage.v1beta2.JsonStreamWriter;
import com.google.cloud.bigquery.storage.v1beta2.TableName;
import com.google.cloud.bigquery.testing.RemoteBigQueryHelper;
import com.google.protobuf.Descriptors;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Iterator;
import java.util.concurrent.ExecutionException;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class ITBigQueryBigDecimalByteStringEncoderTest {
private static final Logger LOG =
Logger.getLogger(ITBigQueryBigDecimalByteStringEncoderTest.class.getName());
private static final String DATASET = RemoteBigQueryHelper.generateDatasetName();
private static final String TABLE = "testtable";
private static final String DESCRIPTION = "BigQuery Write Java manual client test dataset";
private static BigQueryWriteClient client;
private static TableInfo tableInfo;
private static BigQuery bigquery;
@BeforeClass
public static void beforeClass() throws IOException {
client = BigQueryWriteClient.create();
RemoteBigQueryHelper bigqueryHelper = RemoteBigQueryHelper.create();
bigquery = bigqueryHelper.getOptions().getService();
DatasetInfo datasetInfo =
DatasetInfo.newBuilder(/* datasetId = */ DATASET).setDescription(DESCRIPTION).build();
bigquery.create(datasetInfo);
tableInfo =
TableInfo.newBuilder(
TableId.of(DATASET, TABLE),
StandardTableDefinition.of(
Schema.of(
com.google.cloud.bigquery.Field.newBuilder(
"test_numeric_zero", StandardSQLTypeName.NUMERIC)
.build(),
com.google.cloud.bigquery.Field.newBuilder(
"test_numeric_one", StandardSQLTypeName.NUMERIC)
.build(),
com.google.cloud.bigquery.Field.newBuilder(
"test_numeric_repeated", StandardSQLTypeName.NUMERIC)
.setMode(Mode.REPEATED)
.build())))
.build();
bigquery.create(tableInfo);
}
@AfterClass
public static void afterClass() {
if (client != null) {
client.close();
}
if (bigquery != null) {
RemoteBigQueryHelper.forceDelete(bigquery, DATASET);
}
}
@Test
public void TestBigDecimalEncoding()
throws IOException, InterruptedException, ExecutionException,
Descriptors.DescriptorValidationException {
TableName parent = TableName.of(ServiceOptions.getDefaultProjectId(), DATASET, TABLE);
try (JsonStreamWriter jsonStreamWriter =
JsonStreamWriter.newBuilder(parent.toString(), tableInfo.getDefinition().getSchema())
.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());
}
}
}