-
Notifications
You must be signed in to change notification settings - Fork 89
feat!: add a simple write API to mimic insertall writes without insert_id #1904
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
yirutang
wants to merge
8
commits into
googleapis:main
Choose a base branch
from
yirutang:unary-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+251
−0
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
126 changes: 126 additions & 0 deletions
126
...loud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/SimpleWriter.java
This file contains hidden or 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,126 @@ | ||
| package com.google.cloud.bigquery.storage.v1; | ||
|
|
||
| import com.google.api.core.ApiFuture; | ||
| import com.google.api.gax.batching.FlowControlSettings; | ||
| import com.google.api.gax.batching.FlowController.LimitExceededBehavior; | ||
| import com.google.common.base.Preconditions; | ||
| import com.google.common.cache.CacheBuilder; | ||
| import com.google.common.cache.CacheLoader; | ||
| import com.google.common.cache.LoadingCache; | ||
| import com.google.common.cache.RemovalListener; | ||
| import com.google.common.cache.RemovalNotification; | ||
| import com.google.protobuf.Descriptors.DescriptorValidationException; | ||
| import java.io.IOException; | ||
| import java.util.concurrent.ExecutionException; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
| import org.json.JSONArray; | ||
|
|
||
| /** | ||
| * A SimpleWriter provides a way to write to BigQuery in a unary fashion. Underneath, it still uses | ||
| * the streaming API through JsonStreamWriter. There is a cache that manages different table writes. | ||
| * Default max table destination cached is 100, you can adjust it through the Builder. | ||
| * | ||
| * <p>Currently, it only supports AT_LEAST_ONCE writes. | ||
| * | ||
| * <p>This class is still in development, DO NO USE IT. | ||
| * | ||
| * <p>TODOS: 1. Make the class thread safe 2. Handle failed writer case | ||
| */ | ||
| public class SimpleWriter { | ||
| private BigQueryWriteClient client; | ||
| private LoadingCache<String, JsonStreamWriter> writerCache; | ||
| private String traceId; | ||
| private static String tableNamePatternString = "projects/[^/]+/datasets/[^/]+/tables/[^/]+"; | ||
| private static Pattern tableNamePattern = Pattern.compile(tableNamePatternString); | ||
| private boolean ignoreUnkownFields; | ||
|
|
||
| /** Constructs a new {@link SimpleWriter.Builder} using the given bigquery write client. */ | ||
| public static SimpleWriter.Builder newBuilder(BigQueryWriteClient client) { | ||
| return new SimpleWriter.Builder(client); | ||
| } | ||
|
|
||
| private SimpleWriter(SimpleWriter.Builder builder) { | ||
| this.client = builder.client; | ||
| this.traceId = builder.traceId; | ||
| this.ignoreUnkownFields = builder.ignoreUnknownField; | ||
| this.writerCache = | ||
| CacheBuilder.from(builder.cacheSpec) | ||
| .removalListener( | ||
| new RemovalListener<String, JsonStreamWriter>() { | ||
| public void onRemoval( | ||
| RemovalNotification<String, JsonStreamWriter> notification) { | ||
| notification.getValue().close(); | ||
| } | ||
| }) | ||
| .build( | ||
| new CacheLoader<String, JsonStreamWriter>() { | ||
| public JsonStreamWriter load(String key) | ||
| throws DescriptorValidationException, IOException, InterruptedException { | ||
| return JsonStreamWriter.newBuilder(key, client) | ||
| .setEnableConnectionPool(true) | ||
| .setTraceId(traceId) | ||
| .setIgnoreUnknownFields(ignoreUnkownFields) | ||
| .setFlowControlSettings( | ||
| FlowControlSettings.newBuilder() | ||
| .setLimitExceededBehavior(LimitExceededBehavior.ThrowException) | ||
| .build()) | ||
| .build(); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| public static final class Builder { | ||
| BigQueryWriteClient client; | ||
| // Cache expriration time is set to the same as connection timeout time. After a connection | ||
| // is cut, we might be missing schema updates to the object, so we will just let the cache | ||
| // expire so that a fresh table schema will be retrieved. | ||
| String cacheSpec = "maximumSize=100,expireAfterWrite=10m"; | ||
| String traceId = "SimpleWriter:null"; | ||
| boolean ignoreUnknownField = false; | ||
|
|
||
| private Builder(BigQueryWriteClient client) { | ||
| this.client = Preconditions.checkNotNull(client); | ||
| } | ||
|
|
||
| /** CacheSpec for the JsonStreamWriter cache. */ | ||
| public SimpleWriter.Builder setCacheSpec(String cacheSpec) { | ||
| this.cacheSpec = cacheSpec; | ||
| return this; | ||
| } | ||
|
|
||
| /** One time trace id to apply to all writes */ | ||
| public SimpleWriter.Builder setTraceId(String traceId) { | ||
| this.traceId = "SimpleWriter_" + traceId; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * One time set ignoreUnknown field. If true, then if the input has unknown fields to bigquery | ||
| * table, the append will not fail. By default, the setting is false. | ||
| */ | ||
| public SimpleWriter.Builder setIgnoreUnknownField(boolean ignoreUnknownField) { | ||
| this.ignoreUnknownField = ignoreUnknownField; | ||
| return this; | ||
| } | ||
|
|
||
| public SimpleWriter build() { | ||
| return new SimpleWriter(this); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Appends data to a BigQuery Table. Rows will appear AT_LEAST_ONCE in BigQuery. | ||
| * | ||
| * @param rows the rows in serialized format to write to BigQuery. | ||
| * @return the append response wrapped in a future. | ||
| */ | ||
| public ApiFuture<AppendRowsResponse> append(String tableName, JSONArray data) | ||
| throws ExecutionException, DescriptorValidationException, IOException { | ||
| Matcher tableNameMatcher = tableNamePattern.matcher(tableName); | ||
| if (!tableNameMatcher.matches()) { | ||
| throw new IllegalArgumentException("Invalid table name: " + tableName); | ||
| } | ||
| return writerCache.get(tableName).append(data); | ||
| } | ||
| } | ||
77 changes: 77 additions & 0 deletions
77
...-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1/SimpleWriterTest.java
This file contains hidden or 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,77 @@ | ||
| package com.google.cloud.bigquery.storage.v1; | ||
|
|
||
| import com.google.api.gax.core.NoCredentialsProvider; | ||
| import com.google.api.gax.grpc.testing.MockGrpcService; | ||
| import com.google.api.gax.grpc.testing.MockServiceHelper; | ||
| import com.google.protobuf.Descriptors.DescriptorValidationException; | ||
| import java.util.Arrays; | ||
| import java.util.UUID; | ||
| import java.util.logging.Logger; | ||
| import org.junit.After; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.JUnit4; | ||
|
|
||
| @RunWith(JUnit4.class) | ||
| public class SimpleWriterTest { | ||
| private static final Logger log = | ||
| Logger.getLogger(com.google.cloud.bigquery.storage.v1.StreamWriterTest.class.getName()); | ||
| private static final String TEST_STREAM_1 = "projects/p/datasets/d1/tables/t1/streams/s1"; | ||
| private static final String TEST_STREAM_2 = "projects/p/datasets/d2/tables/t2/streams/s2"; | ||
| private static final String TEST_TRACE_ID = "DATAFLOW:job_id"; | ||
| private FakeScheduledExecutorService fakeExecutor; | ||
| private FakeBigQueryWrite testBigQueryWrite; | ||
| private static MockServiceHelper serviceHelper; | ||
| private BigQueryWriteClient client; | ||
| private final TableFieldSchema FOO = | ||
| TableFieldSchema.newBuilder() | ||
| .setType(TableFieldSchema.Type.STRING) | ||
| .setMode(TableFieldSchema.Mode.NULLABLE) | ||
| .setName("foo") | ||
| .build(); | ||
| private final TableFieldSchema BAR = | ||
| TableFieldSchema.newBuilder() | ||
| .setType(TableFieldSchema.Type.STRING) | ||
| .setMode(TableFieldSchema.Mode.NULLABLE) | ||
| .setName("bar") | ||
| .build(); | ||
|
|
||
| public SimpleWriterTest() throws DescriptorValidationException {} | ||
|
|
||
| @Before | ||
| public void setUp() throws Exception { | ||
| testBigQueryWrite = new FakeBigQueryWrite(); | ||
| serviceHelper = | ||
| new MockServiceHelper( | ||
| UUID.randomUUID().toString(), Arrays.<MockGrpcService>asList(testBigQueryWrite)); | ||
| serviceHelper.start(); | ||
| fakeExecutor = new FakeScheduledExecutorService(); | ||
| testBigQueryWrite.setExecutor(fakeExecutor); | ||
| client = | ||
| BigQueryWriteClient.create( | ||
| BigQueryWriteSettings.newBuilder() | ||
| .setCredentialsProvider(NoCredentialsProvider.create()) | ||
| .setTransportChannelProvider(serviceHelper.createChannelProvider()) | ||
| .build()); | ||
| } | ||
|
|
||
| @After | ||
| public void tearDown() throws Exception { | ||
| log.info("tearDown called"); | ||
| client.close(); | ||
| serviceHelper.stop(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGoodWrites() throws Exception {} | ||
|
|
||
| @Test | ||
| public void testBadWrites() throws Exception {} | ||
|
|
||
| @Test | ||
| public void testWriterCacheExpired() throws Exception {} | ||
|
|
||
| @Test | ||
| public void testBuilderParams() throws Exception {} | ||
| } |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.