-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from vinted/refactor/simplify-structure
Refactor/simplify structure
- Loading branch information
Showing
22 changed files
with
295 additions
and
285 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
15 changes: 15 additions & 0 deletions
15
src/main/java/com/vinted/flink/bigquery/client/BigQueryStreamWriter.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,15 @@ | ||
package com.vinted.flink.bigquery.client; | ||
|
||
import com.google.api.core.ApiFuture; | ||
import com.google.cloud.bigquery.storage.v1.AppendRowsResponse; | ||
import com.vinted.flink.bigquery.model.Rows; | ||
|
||
public interface BigQueryStreamWriter<T> extends AutoCloseable { | ||
ApiFuture<AppendRowsResponse> append(Rows<T> data); | ||
ApiFuture<AppendRowsResponse> append(Rows<T> data, long offset); | ||
|
||
String getStreamName(); | ||
|
||
String getWriterId(); | ||
boolean isClosed(); | ||
} |
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
64 changes: 64 additions & 0 deletions
64
src/main/java/com/vinted/flink/bigquery/client/JsonStreamWriter.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,64 @@ | ||
package com.vinted.flink.bigquery.client; | ||
|
||
import com.google.api.core.ApiFuture; | ||
import com.google.cloud.bigquery.storage.v1.AppendRowsResponse; | ||
import com.google.protobuf.Descriptors; | ||
import com.vinted.flink.bigquery.model.Rows; | ||
import com.vinted.flink.bigquery.serializer.RowValueSerializer; | ||
import org.json.JSONArray; | ||
import org.json.JSONObject; | ||
|
||
import java.io.IOException; | ||
|
||
public class JsonStreamWriter<A> implements BigQueryStreamWriter<A>{ | ||
private final RowValueSerializer<A> rowSerializer; | ||
|
||
private final com.google.cloud.bigquery.storage.v1.JsonStreamWriter writer; | ||
|
||
public JsonStreamWriter(RowValueSerializer<A> rowSerializer, com.google.cloud.bigquery.storage.v1.JsonStreamWriter writer) { | ||
this.rowSerializer = rowSerializer; | ||
this.writer = writer; | ||
} | ||
|
||
@Override | ||
public ApiFuture<AppendRowsResponse> append(Rows<A> data) { | ||
var rowArray = new JSONArray(); | ||
data.getData().forEach(row -> rowArray.put(new JSONObject(new String(rowSerializer.serialize(row))))); | ||
try { | ||
return writer.append(rowArray); | ||
} catch (IOException | Descriptors.DescriptorValidationException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public ApiFuture<AppendRowsResponse> append(Rows<A> data, long offset) { | ||
var rowArray = new JSONArray(); | ||
data.getData().forEach(row -> rowArray.put(new JSONObject(new String(rowSerializer.serialize(row))))); | ||
try { | ||
return writer.append(rowArray, offset); | ||
} catch (IOException | Descriptors.DescriptorValidationException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public String getStreamName() { | ||
return writer.getStreamName(); | ||
} | ||
|
||
@Override | ||
public String getWriterId() { | ||
return writer.getWriterId(); | ||
} | ||
|
||
@Override | ||
public boolean isClosed() { | ||
return writer.isClosed() || writer.isUserClosed(); | ||
} | ||
|
||
@Override | ||
public void close() throws Exception { | ||
writer.close(); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/com/vinted/flink/bigquery/client/ProtoStreamWriter.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,60 @@ | ||
package com.vinted.flink.bigquery.client; | ||
|
||
import com.google.api.core.ApiFuture; | ||
import com.google.cloud.bigquery.storage.v1.AppendRowsResponse; | ||
import com.google.cloud.bigquery.storage.v1.ProtoRows; | ||
import com.google.cloud.bigquery.storage.v1.StreamWriter; | ||
import com.google.protobuf.ByteString; | ||
import com.vinted.flink.bigquery.model.Rows; | ||
import com.vinted.flink.bigquery.serializer.RowValueSerializer; | ||
|
||
import java.util.stream.Collectors; | ||
|
||
public class ProtoStreamWriter<A> implements BigQueryStreamWriter<A>{ | ||
private final RowValueSerializer<A> rowSerializer; | ||
|
||
private final StreamWriter writer; | ||
|
||
public ProtoStreamWriter(RowValueSerializer<A> rowSerializer, StreamWriter writer) { | ||
this.rowSerializer = rowSerializer; | ||
this.writer = writer; | ||
} | ||
|
||
@Override | ||
public ApiFuture<AppendRowsResponse> append(Rows<A> data) { | ||
var prows = ProtoRows | ||
.newBuilder() | ||
.addAllSerializedRows(data.getData().stream().map(r -> ByteString.copyFrom(rowSerializer.serialize(r))).collect(Collectors.toList())) | ||
.build(); | ||
return writer.append(prows); | ||
} | ||
|
||
@Override | ||
public ApiFuture<AppendRowsResponse> append(Rows<A> data, long offset) { | ||
var prows = ProtoRows | ||
.newBuilder() | ||
.addAllSerializedRows(data.getData().stream().map(r -> ByteString.copyFrom(rowSerializer.serialize(r))).collect(Collectors.toList())) | ||
.build(); | ||
return writer.append(prows, offset); | ||
} | ||
|
||
@Override | ||
public String getStreamName() { | ||
return writer.getStreamName(); | ||
} | ||
|
||
@Override | ||
public String getWriterId() { | ||
return writer.getWriterId(); | ||
} | ||
|
||
@Override | ||
public boolean isClosed() { | ||
return writer.isClosed() || writer.isUserClosed(); | ||
} | ||
|
||
@Override | ||
public void close() throws Exception { | ||
writer.close(); | ||
} | ||
} |
Oops, something went wrong.