-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for partitioned BigQuery tables (#163)
- Loading branch information
Showing
9 changed files
with
260 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
...ry/src/main/java/com/google/cloud/hadoop/io/bigquery/output/BigQueryTimePartitioning.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright 2019 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 | ||
* | ||
* https://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.hadoop.io.bigquery.output; | ||
|
||
import com.google.api.client.json.JsonParser; | ||
import com.google.api.client.json.jackson2.JacksonFactory; | ||
import com.google.api.services.bigquery.model.TimePartitioning; | ||
import java.io.IOException; | ||
|
||
/** | ||
* Wrapper for BigQuery {@link TimePartitioning}. | ||
* | ||
* <p>This class is used to avoid client code to depend on BigQuery API classes, so that there is no | ||
* potential conflict between different versions of BigQuery API libraries in the client. | ||
* | ||
* @see TimePartitioning. | ||
*/ | ||
public class BigQueryTimePartitioning { | ||
private final TimePartitioning timePartitioning; | ||
|
||
public BigQueryTimePartitioning() { | ||
this.timePartitioning = new TimePartitioning(); | ||
} | ||
|
||
public BigQueryTimePartitioning(TimePartitioning timePartitioning) { | ||
this.timePartitioning = timePartitioning; | ||
} | ||
|
||
public String getType() { | ||
return timePartitioning.getType(); | ||
} | ||
|
||
public void setType(String type) { | ||
timePartitioning.setType(type); | ||
} | ||
|
||
public String getField() { | ||
return timePartitioning.getField(); | ||
} | ||
|
||
public void setField(String field) { | ||
timePartitioning.setField(field); | ||
} | ||
|
||
public long getExpirationMs() { | ||
return timePartitioning.getExpirationMs(); | ||
} | ||
|
||
public void setExpirationMs(long expirationMs) { | ||
timePartitioning.setExpirationMs(expirationMs); | ||
} | ||
|
||
public Boolean getRequirePartitionFilter() { | ||
return timePartitioning.getRequirePartitionFilter(); | ||
} | ||
|
||
public void setRequirePartitionFilter(Boolean requirePartitionFilter) { | ||
timePartitioning.setRequirePartitionFilter(requirePartitionFilter); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return timePartitioning.hashCode(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (!(obj instanceof BigQueryTimePartitioning)) { | ||
return false; | ||
} | ||
BigQueryTimePartitioning other = (BigQueryTimePartitioning) obj; | ||
return timePartitioning.equals(other.timePartitioning); | ||
} | ||
|
||
TimePartitioning get() { | ||
return timePartitioning; | ||
} | ||
|
||
static TimePartitioning getFromJson(String json) throws IOException { | ||
JsonParser parser = JacksonFactory.getDefaultInstance().createJsonParser(json); | ||
return parser.parseAndClose(TimePartitioning.class); | ||
} | ||
|
||
public String getAsJson() throws IOException { | ||
return JacksonFactory.getDefaultInstance().toString(timePartitioning); | ||
} | ||
|
||
static BigQueryTimePartitioning wrap(TimePartitioning tableTimePartitioning) { | ||
return new BigQueryTimePartitioning(tableTimePartitioning); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...rc/test/java/com/google/cloud/hadoop/io/bigquery/output/BigQueryTimePartitioningTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright 2019 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 | ||
* | ||
* https://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.hadoop.io.bigquery.output; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import java.io.IOException; | ||
import org.junit.Test; | ||
|
||
public class BigQueryTimePartitioningTest { | ||
|
||
public static final String TIME_PARTITIONING_JSON = | ||
"{\"expirationMs\":\"1000\",\"field\":\"ingestDate\",\"requirePartitionFilter\":true," | ||
+ "\"type\":\"DAY\"}"; | ||
|
||
@Test | ||
public void testConvertToJson() throws IOException { | ||
BigQueryTimePartitioning bigQueryTimePartitioning = new BigQueryTimePartitioning(); | ||
bigQueryTimePartitioning.setType("DAY"); | ||
bigQueryTimePartitioning.setExpirationMs(1000L); | ||
bigQueryTimePartitioning.setField("ingestDate"); | ||
bigQueryTimePartitioning.setRequirePartitionFilter(true); | ||
|
||
assertThat(bigQueryTimePartitioning.getAsJson()).isEqualTo(TIME_PARTITIONING_JSON); | ||
} | ||
|
||
@Test | ||
public void testConvertFromJson() throws IOException { | ||
BigQueryTimePartitioning bigQueryTimePartitioning = new BigQueryTimePartitioning(); | ||
bigQueryTimePartitioning.setType("DAY"); | ||
bigQueryTimePartitioning.setExpirationMs(1000L); | ||
bigQueryTimePartitioning.setField("ingestDate"); | ||
bigQueryTimePartitioning.setRequirePartitionFilter(true); | ||
|
||
assertThat(BigQueryTimePartitioning.getFromJson(TIME_PARTITIONING_JSON)) | ||
.isEqualTo(bigQueryTimePartitioning.get()); | ||
} | ||
|
||
@Test | ||
public void testConversion_OnlyTypeIsPresent() throws IOException { | ||
BigQueryTimePartitioning bigQueryTimePartitioning = new BigQueryTimePartitioning(); | ||
bigQueryTimePartitioning.setType("DAY"); | ||
String json = bigQueryTimePartitioning.getAsJson(); | ||
|
||
assertThat(json).isEqualTo("{\"type\":\"DAY\"}"); | ||
assertThat(BigQueryTimePartitioning.getFromJson(json).getType()).isEqualTo("DAY"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters