|
| 1 | +/* |
| 2 | + * Copyright 2021 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.bigquery; |
| 18 | + |
| 19 | +import com.google.common.base.MoreObjects; |
| 20 | +import java.util.Objects; |
| 21 | + |
| 22 | +/** |
| 23 | + * Google BigQuery options for AVRO format. This class wraps some properties of AVRO files used by |
| 24 | + * BigQuery to parse external data. |
| 25 | + */ |
| 26 | +public final class AvroOptions extends FormatOptions { |
| 27 | + |
| 28 | + private static final long serialVersionUID = 2293570529308612712L; |
| 29 | + |
| 30 | + private final Boolean useAvroLogicalTypes; |
| 31 | + |
| 32 | + public static final class Builder { |
| 33 | + |
| 34 | + private Boolean useAvroLogicalTypes; |
| 35 | + |
| 36 | + private Builder() {} |
| 37 | + |
| 38 | + private Builder(AvroOptions avroOptions) { |
| 39 | + this.useAvroLogicalTypes = avroOptions.useAvroLogicalTypes; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * [Optional] Sets whether BigQuery should interpret logical types as the corresponding BigQuery |
| 44 | + * data type (for example, TIMESTAMP), instead of using the raw type (for example, INTEGER). |
| 45 | + */ |
| 46 | + public Builder setUseAvroLogicalTypes(boolean useAvroLogicalTypes) { |
| 47 | + this.useAvroLogicalTypes = useAvroLogicalTypes; |
| 48 | + return this; |
| 49 | + } |
| 50 | + |
| 51 | + /** Creates a {@code AvroOptions} object. */ |
| 52 | + public AvroOptions build() { |
| 53 | + return new AvroOptions(this); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + private AvroOptions(Builder builder) { |
| 58 | + super(FormatOptions.AVRO); |
| 59 | + this.useAvroLogicalTypes = builder.useAvroLogicalTypes; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Returns whether BigQuery should interpret logical types as the corresponding BigQuery data type |
| 64 | + * (for example, TIMESTAMP), instead of using the raw type (for example, INTEGER). |
| 65 | + */ |
| 66 | + public Boolean useAvroLogicalTypes() { |
| 67 | + return useAvroLogicalTypes; |
| 68 | + } |
| 69 | + |
| 70 | + public Builder toBuilder() { |
| 71 | + return new Builder(this); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public String toString() { |
| 76 | + return MoreObjects.toStringHelper(this) |
| 77 | + .add("type", getType()) |
| 78 | + .add("useAvroLogicalTypes", useAvroLogicalTypes) |
| 79 | + .toString(); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public int hashCode() { |
| 84 | + return Objects.hash(getType(), useAvroLogicalTypes); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public boolean equals(Object obj) { |
| 89 | + return obj == this |
| 90 | + || obj instanceof AvroOptions && Objects.equals(toPb(), ((AvroOptions) obj).toPb()); |
| 91 | + } |
| 92 | + |
| 93 | + com.google.api.services.bigquery.model.AvroOptions toPb() { |
| 94 | + com.google.api.services.bigquery.model.AvroOptions avroOptions = |
| 95 | + new com.google.api.services.bigquery.model.AvroOptions(); |
| 96 | + avroOptions.setUseAvroLogicalTypes(useAvroLogicalTypes); |
| 97 | + return avroOptions; |
| 98 | + } |
| 99 | + |
| 100 | + /** Returns a builder for a AvroOptions object. */ |
| 101 | + public static AvroOptions.Builder newBuilder() { |
| 102 | + return new AvroOptions.Builder(); |
| 103 | + } |
| 104 | + |
| 105 | + static AvroOptions fromPb(com.google.api.services.bigquery.model.AvroOptions avroOptions) { |
| 106 | + Builder builder = newBuilder(); |
| 107 | + if (avroOptions.getUseAvroLogicalTypes() != null) { |
| 108 | + builder.setUseAvroLogicalTypes(avroOptions.getUseAvroLogicalTypes()); |
| 109 | + } |
| 110 | + return builder.build(); |
| 111 | + } |
| 112 | +} |
0 commit comments