/
StandardTableDefinition.java
301 lines (261 loc) · 10.3 KB
/
StandardTableDefinition.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* Copyright 2016 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;
import com.google.api.services.bigquery.model.Streamingbuffer;
import com.google.api.services.bigquery.model.Table;
import com.google.auto.value.AutoValue;
import com.google.common.base.MoreObjects;
import java.io.Serializable;
import java.math.BigInteger;
import java.util.Objects;
import javax.annotation.Nullable;
/**
* A Google BigQuery default table definition. This definition is used for standard, two-dimensional
* tables with individual records organized in rows, and a data type assigned to each column (also
* called a field). Individual fields within a record may contain nested and repeated children
* fields. Every table is described by a schema that describes field names, types, and other
* information.
*
* @see <a href="https://cloud.google.com/bigquery/docs/tables">Managing Tables</a>
*/
@AutoValue
public abstract class StandardTableDefinition extends TableDefinition {
private static final long serialVersionUID = 2113445776046717900L;
/**
* Google BigQuery Table's Streaming Buffer information. This class contains information on a
* table's streaming buffer as the estimated size in number of rows/bytes.
*/
public static class StreamingBuffer implements Serializable {
private static final long serialVersionUID = 822027055549277843L;
private final Long estimatedRows;
private final Long estimatedBytes;
private final Long oldestEntryTime;
StreamingBuffer(Long estimatedRows, Long estimatedBytes, Long oldestEntryTime) {
this.estimatedRows = estimatedRows;
this.estimatedBytes = estimatedBytes;
this.oldestEntryTime = oldestEntryTime;
}
/** Returns a lower-bound estimate of the number of rows currently in the streaming buffer. */
public Long getEstimatedRows() {
return estimatedRows;
}
/** Returns a lower-bound estimate of the number of bytes currently in the streaming buffer. */
public Long getEstimatedBytes() {
return estimatedBytes;
}
/**
* Returns the timestamp of the oldest entry in the streaming buffer, in milliseconds since
* epoch. Returns {@code null} if the streaming buffer is empty.
*/
public Long getOldestEntryTime() {
return oldestEntryTime;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("estimatedRows", estimatedRows)
.add("estimatedBytes", estimatedBytes)
.add("oldestEntryTime", oldestEntryTime)
.toString();
}
@Override
public int hashCode() {
return Objects.hash(estimatedRows, estimatedBytes, oldestEntryTime);
}
@Override
public boolean equals(Object obj) {
return obj instanceof StreamingBuffer
&& Objects.equals(toPb(), ((StreamingBuffer) obj).toPb());
}
Streamingbuffer toPb() {
Streamingbuffer buffer = new Streamingbuffer();
if (estimatedBytes != null) {
buffer.setEstimatedBytes(BigInteger.valueOf(estimatedBytes));
}
if (estimatedRows != null) {
buffer.setEstimatedRows(BigInteger.valueOf(estimatedRows));
}
if (oldestEntryTime != null) {
buffer.setOldestEntryTime(BigInteger.valueOf(oldestEntryTime));
}
return buffer;
}
static StreamingBuffer fromPb(Streamingbuffer streamingBufferPb) {
Long oldestEntryTime = null;
if (streamingBufferPb.getOldestEntryTime() != null) {
oldestEntryTime = streamingBufferPb.getOldestEntryTime().longValue();
}
return new StreamingBuffer(
streamingBufferPb.getEstimatedRows() != null
? streamingBufferPb.getEstimatedRows().longValue()
: null,
streamingBufferPb.getEstimatedBytes() != null
? streamingBufferPb.getEstimatedBytes().longValue()
: null,
oldestEntryTime);
}
}
@AutoValue.Builder
public abstract static class Builder
extends TableDefinition.Builder<StandardTableDefinition, Builder> {
public abstract Builder setNumBytes(Long numBytes);
public abstract Builder setNumLongTermBytes(Long numLongTermBytes);
public abstract Builder setNumRows(Long numRows);
public abstract Builder setLocation(String location);
public abstract Builder setStreamingBuffer(StreamingBuffer streamingBuffer);
public abstract Builder setType(Type type);
/** Sets the table schema. */
public abstract Builder setSchema(Schema schema);
/**
* Sets the time partitioning configuration for the table. If not set, the table is not
* time-partitioned.
*/
public abstract Builder setTimePartitioning(TimePartitioning timePartitioning);
/**
* Sets the range partitioning configuration for the table. Only one of timePartitioning and
* rangePartitioning should be specified.
*/
public abstract Builder setRangePartitioning(RangePartitioning rangePartitioning);
/**
* Set the clustering configuration for the table. If not set, the table is not clustered.
* Clustering is only available for partitioned tables.
*/
public abstract Builder setClustering(Clustering clustering);
/** Creates a {@code StandardTableDefinition} object. */
public abstract StandardTableDefinition build();
}
/** Returns the size of this table in bytes, excluding any data in the streaming buffer. */
@Nullable
public abstract Long getNumBytes();
/**
* Returns the number of bytes considered "long-term storage" for reduced billing purposes.
*
* @see <a href="https://cloud.google.com/bigquery/pricing#long-term-storage">Long Term Storage
* Pricing</a>
*/
@Nullable
public abstract Long getNumLongTermBytes();
/** Returns the number of rows in this table, excluding any data in the streaming buffer. */
@Nullable
public abstract Long getNumRows();
/**
* Returns the geographic location where the table should reside. This value is inherited from the
* dataset.
*
* @see <a
* href="https://cloud.google.com/bigquery/docs/managing_jobs_datasets_projects#dataset-location">
* Dataset Location</a>
*/
@Nullable
public abstract String getLocation();
/**
* Returns information on the table's streaming buffer if any exists. Returns {@code null} if no
* streaming buffer exists.
*/
@Nullable
public abstract StreamingBuffer getStreamingBuffer();
/**
* Returns the time partitioning configuration for this table. If {@code null}, the table is not
* time-partitioned.
*/
@Nullable
public abstract TimePartitioning getTimePartitioning();
/**
* Returns the range partitioning configuration for this table. If {@code null}, the table is not
* range-partitioned.
*/
@Nullable
public abstract RangePartitioning getRangePartitioning();
/**
* Returns the clustering configuration for this table. If {@code null}, the table is not
* clustered.
*/
@Nullable
public abstract Clustering getClustering();
/** Returns a builder for a BigQuery standard table definition. */
public static Builder newBuilder() {
return new AutoValue_StandardTableDefinition.Builder().setType(Type.TABLE);
}
/**
* Creates a BigQuery standard table definition given its schema.
*
* @param schema the schema of the table
*/
public static StandardTableDefinition of(Schema schema) {
return newBuilder().setSchema(schema).build();
}
/** Returns a builder for the {@code StandardTableDefinition} object. */
public abstract Builder toBuilder();
@Override
Table toPb() {
Table tablePb = super.toPb();
if (getNumRows() != null) {
tablePb.setNumRows(BigInteger.valueOf(getNumRows()));
}
tablePb.setNumBytes(getNumBytes());
tablePb.setNumLongTermBytes(getNumLongTermBytes());
tablePb.setLocation(getLocation());
if (getStreamingBuffer() != null) {
tablePb.setStreamingBuffer(getStreamingBuffer().toPb());
}
if (getTimePartitioning() != null) {
tablePb.setTimePartitioning(getTimePartitioning().toPb());
}
if (getRangePartitioning() != null) {
tablePb.setRangePartitioning(getRangePartitioning().toPb());
}
if (getClustering() != null) {
tablePb.setClustering(getClustering().toPb());
}
return tablePb;
}
@SuppressWarnings("unchecked")
static StandardTableDefinition fromPb(Table tablePb) {
Builder builder = newBuilder().table(tablePb);
if (tablePb.getNumRows() != null) {
builder.setNumRows(tablePb.getNumRows().longValue());
}
if (tablePb.getStreamingBuffer() != null) {
builder.setStreamingBuffer(StreamingBuffer.fromPb(tablePb.getStreamingBuffer()));
}
if (tablePb.getTimePartitioning() != null) {
try {
builder.setTimePartitioning(TimePartitioning.fromPb(tablePb.getTimePartitioning()));
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(
"Illegal Argument - Got unexpected time partitioning "
+ tablePb.getTimePartitioning().toString()
+ " in project "
+ tablePb.getTableReference().getProjectId()
+ " in dataset "
+ tablePb.getTableReference().getDatasetId()
+ " in table "
+ tablePb.getTableReference().getTableId(),
e);
}
}
if (tablePb.getRangePartitioning() != null) {
builder.setRangePartitioning(RangePartitioning.fromPb(tablePb.getRangePartitioning()));
}
if (tablePb.getClustering() != null) {
builder.setClustering(Clustering.fromPb(tablePb.getClustering()));
}
if (tablePb.getNumLongTermBytes() != null) {
builder.setNumLongTermBytes(tablePb.getNumLongTermBytes());
}
return builder.setNumBytes(tablePb.getNumBytes()).setLocation(tablePb.getLocation()).build();
}
}