-
Notifications
You must be signed in to change notification settings - Fork 336
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
to #103, Support PB format serializer in Kafka Source #185
Open
qidian99
wants to merge
1
commit into
bytedance:master
Choose a base branch
from
qidian99:issue/#103_support_pb_format_in_kafka
base: master
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.
Open
Changes from all commits
Commits
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
19 changes: 19 additions & 0 deletions
19
bitsail-components/bitsail-component-formats/bitsail-component-format-pb/pom.xml
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,19 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>bitsail-component-formats</artifactId> | ||
<groupId>com.bytedance.bitsail</groupId> | ||
<version>${revision}</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>bitsail-component-format-pb</artifactId> | ||
|
||
<properties> | ||
<maven.compiler.source>8</maven.compiler.source> | ||
<maven.compiler.target>8</maven.compiler.target> | ||
</properties> | ||
|
||
</project> |
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
100 changes: 100 additions & 0 deletions
100
...com/bytedance/bitsail/connector/legacy/kafka/deserialization/PbDeserializationSchema.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,100 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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.bytedance.bitsail.connector.legacy.kafka.deserialization; | ||
|
||
import com.bytedance.bitsail.batch.file.parser.PbBytesParser; | ||
import com.bytedance.bitsail.common.configuration.BitSailConfiguration; | ||
import com.bytedance.bitsail.flink.core.typeinfo.PrimitiveColumnTypeInfo; | ||
|
||
import com.google.protobuf.Descriptors; | ||
import org.apache.flink.annotation.Internal; | ||
import org.apache.flink.api.common.serialization.DeserializationSchema; | ||
import org.apache.flink.api.common.typeinfo.TypeInformation; | ||
import org.apache.flink.api.java.typeutils.RowTypeInfo; | ||
import org.apache.flink.types.Row; | ||
import org.apache.flink.util.Collector; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
@Internal | ||
public class PbDeserializationSchema implements DeserializationSchema<Row> { | ||
private static final long serialVersionUID = -2556547991095476394L; | ||
private final PbBytesParser parser; | ||
private final RowTypeInfo rowTypeInfo; | ||
private final int arity; | ||
|
||
public PbDeserializationSchema(BitSailConfiguration jobConf) throws Exception { | ||
this.parser = new PbBytesParser(jobConf); | ||
|
||
List<Descriptors.FieldDescriptor> fields = parser.getDescriptor().getFields(); | ||
this.arity = fields.size(); | ||
PrimitiveColumnTypeInfo<?>[] types = new PrimitiveColumnTypeInfo[arity]; | ||
String[] fieldNames = new String[arity]; | ||
for (int i = 0; i < arity; i++) { | ||
Descriptors.FieldDescriptor field = fields.get(i); | ||
types[i] = getColumnTypeInfo(field); | ||
fieldNames[i] = field.getJsonName(); | ||
} | ||
this.rowTypeInfo = new RowTypeInfo(types, fieldNames); | ||
} | ||
|
||
private PrimitiveColumnTypeInfo<?> getColumnTypeInfo(Descriptors.FieldDescriptor field) { | ||
switch (field.getJavaType()) { | ||
case INT: | ||
case LONG: | ||
return PrimitiveColumnTypeInfo.LONG_COLUMN_TYPE_INFO; | ||
case FLOAT: | ||
case DOUBLE: | ||
return PrimitiveColumnTypeInfo.DOUBLE_COLUMN_TYPE_INFO; | ||
case BOOLEAN: | ||
return PrimitiveColumnTypeInfo.BOOL_COLUMN_TYPE_INFO; | ||
case BYTE_STRING: | ||
return PrimitiveColumnTypeInfo.BYTES_COLUMN_TYPE_INFO; | ||
case MESSAGE: | ||
case STRING: | ||
case ENUM: | ||
default: | ||
return PrimitiveColumnTypeInfo.STRING_COLUMN_TYPE_INFO; | ||
} | ||
} | ||
|
||
@Override | ||
public void open(DeserializationSchema.InitializationContext context) throws Exception { | ||
} | ||
|
||
@Override | ||
public Row deserialize(byte[] value) throws IOException { | ||
return this.parser.parse(new Row(arity), value, 0, value.length, null, rowTypeInfo); | ||
} | ||
|
||
@Override | ||
public void deserialize(byte[] value, Collector<Row> out) throws IOException { | ||
out.collect(deserialize(value)); | ||
} | ||
|
||
@Override | ||
public boolean isEndOfStream(Row row) { | ||
return false; | ||
} | ||
|
||
@Override | ||
public TypeInformation<Row> getProducedType() { | ||
return rowTypeInfo; | ||
} | ||
} |
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
7 changes: 7 additions & 0 deletions
7
...connectors/bitsail-connectors-legacy/bitsail-connector-kafka/src/test/resources/kafka.fds
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,7 @@ | ||
|
||
Z | ||
kafka.proto"C | ||
ProtoTest | ||
id (Rid | ||
name ( Rname | ||
date ( Rdatebproto3 |
7 changes: 7 additions & 0 deletions
7
...nnectors/bitsail-connectors-legacy/bitsail-connector-kafka/src/test/resources/kafka.proto
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,7 @@ | ||
syntax = "proto3"; | ||
|
||
message ProtoTest { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd suggest we include all supported types here, including some complex type. |
||
int64 ID = 1; | ||
string NAME = 2; | ||
string DATE = 3; | ||
} |
20 changes: 20 additions & 0 deletions
20
...bitsail-connectors-legacy/bitsail-connector-kafka/src/test/resources/kafka_full_types.fds
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ran this ITCase and one test was failed. looks like the kafka producer didn't stop when we switch to the second test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added clean up logic on the producer service to stop creating records. All test cases pass now