-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature] Adapt to sink v2 api FLIP-191
Signed-off-by: PengFei Li <[email protected]>
- Loading branch information
Showing
13 changed files
with
632 additions
and
27 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
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
32 changes: 32 additions & 0 deletions
32
src/main/java/com/starrocks/connector/flink/table/sink/v2/StarRocksCommittable.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,32 @@ | ||
/* | ||
* Copyright 2021-present StarRocks, Inc. All rights reserved. | ||
* | ||
* 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.starrocks.connector.flink.table.sink.v2; | ||
|
||
import com.starrocks.data.load.stream.StreamLoadSnapshot; | ||
|
||
public class StarRocksCommittable { | ||
|
||
private final StreamLoadSnapshot labelSnapshot; | ||
|
||
public StarRocksCommittable(StreamLoadSnapshot labelSnapshot) { | ||
this.labelSnapshot = labelSnapshot; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...main/java/com/starrocks/connector/flink/table/sink/v2/StarRocksCommittableSerializer.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,50 @@ | ||
/* | ||
* Copyright 2021-present StarRocks, Inc. All rights reserved. | ||
* | ||
* 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.starrocks.connector.flink.table.sink.v2; | ||
|
||
import com.starrocks.connector.flink.tools.JsonWrapper; | ||
import org.apache.flink.core.io.SimpleVersionedSerializer; | ||
|
||
import java.io.IOException; | ||
|
||
public class StarRocksCommittableSerializer implements SimpleVersionedSerializer<StarRocksCommittable> { | ||
|
||
private final JsonWrapper jsonWrapper; | ||
|
||
public StarRocksCommittableSerializer() { | ||
this.jsonWrapper = new JsonWrapper(); | ||
} | ||
|
||
@Override | ||
public int getVersion() { | ||
return 1; | ||
} | ||
|
||
@Override | ||
public byte[] serialize(StarRocksCommittable committable) throws IOException { | ||
return jsonWrapper.toJSONBytes(committable); | ||
} | ||
|
||
@Override | ||
public StarRocksCommittable deserialize(int version, byte[] serialized) throws IOException { | ||
return jsonWrapper.parseObject(serialized, StarRocksCommittable.class); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/com/starrocks/connector/flink/table/sink/v2/StarRocksCommitter.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,43 @@ | ||
/* | ||
* Copyright 2021-present StarRocks, Inc. All rights reserved. | ||
* | ||
* 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.starrocks.connector.flink.table.sink.v2; | ||
|
||
import org.apache.flink.api.connector.sink2.Committer; | ||
|
||
import java.io.IOException; | ||
import java.util.Collection; | ||
|
||
public class StarRocksCommitter implements Committer<StarRocksCommittable> { | ||
|
||
public StarRocksCommitter() { | ||
} | ||
|
||
@Override | ||
public void commit(Collection<CommitRequest<StarRocksCommittable>> committables) | ||
throws IOException, InterruptedException { | ||
|
||
} | ||
|
||
@Override | ||
public void close() throws Exception { | ||
|
||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
src/main/java/com/starrocks/connector/flink/table/sink/v2/StarRocksSink.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,109 @@ | ||
/* | ||
* Copyright 2021-present StarRocks, Inc. All rights reserved. | ||
* | ||
* 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.starrocks.connector.flink.table.sink.v2; | ||
|
||
import com.starrocks.connector.flink.manager.StarRocksSinkTable; | ||
import com.starrocks.connector.flink.row.sink.StarRocksIRowTransformer; | ||
import com.starrocks.connector.flink.row.sink.StarRocksISerializer; | ||
import com.starrocks.connector.flink.row.sink.StarRocksSerializerFactory; | ||
import com.starrocks.connector.flink.table.sink.StarRocksSinkOptions; | ||
import com.starrocks.data.load.stream.properties.StreamLoadProperties; | ||
import org.apache.flink.api.connector.sink2.Committer; | ||
import org.apache.flink.api.connector.sink2.StatefulSink; | ||
import org.apache.flink.api.connector.sink2.TwoPhaseCommittingSink; | ||
import org.apache.flink.core.io.SimpleVersionedSerializer; | ||
import org.apache.flink.table.api.TableSchema; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
|
||
public class StarRocksSink<InputT> | ||
implements StatefulSink<InputT, StarRocksWriterState>, TwoPhaseCommittingSink<InputT, StarRocksCommittable> { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(StarRocksSink.class); | ||
|
||
private final StarRocksSinkOptions sinkOptions; | ||
private final StarRocksISerializer serializer; | ||
private final StarRocksIRowTransformer<InputT> rowTransformer; | ||
private final StreamLoadProperties streamLoadProperties; | ||
|
||
|
||
public StarRocksSink(StarRocksSinkOptions sinkOptions, TableSchema schema, StarRocksIRowTransformer<InputT> rowTransformer) { | ||
this.sinkOptions = sinkOptions; | ||
this.rowTransformer = rowTransformer; | ||
StarRocksSinkTable sinkTable = StarRocksSinkTable.builder() | ||
.sinkOptions(sinkOptions) | ||
.build(); | ||
sinkTable.validateTableStructure(sinkOptions, schema); | ||
// StarRocksJsonSerializer depends on SinkOptions#supportUpsertDelete which is decided in | ||
// StarRocksSinkTable#validateTableStructure, so create serializer after validating table structure | ||
this.serializer = StarRocksSerializerFactory.createSerializer(sinkOptions, schema.getFieldNames()); | ||
rowTransformer.setStarRocksColumns(sinkTable.getFieldMapping()); | ||
rowTransformer.setTableSchema(schema); | ||
this.streamLoadProperties = sinkOptions.getProperties(sinkTable); | ||
} | ||
|
||
public StarRocksSink(StarRocksSinkOptions sinkOptions) { | ||
this.sinkOptions = sinkOptions; | ||
this.serializer = null; | ||
this.rowTransformer = null; | ||
this.streamLoadProperties = sinkOptions.getProperties(null); | ||
} | ||
|
||
@Override | ||
public StarRocksWriter<InputT> createWriter(InitContext context) throws IOException { | ||
return restoreWriter(context, Collections.emptyList()); | ||
} | ||
|
||
@Override | ||
public StarRocksWriter<InputT> restoreWriter(InitContext context, Collection<StarRocksWriterState> recoveredState) | ||
throws IOException { | ||
try { | ||
return new StarRocksWriter<>( | ||
sinkOptions, | ||
serializer, | ||
rowTransformer, | ||
streamLoadProperties, | ||
context, | ||
Collections.emptyList()); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Failed to create writer.", e); | ||
} | ||
} | ||
|
||
@Override | ||
public SimpleVersionedSerializer<StarRocksWriterState> getWriterStateSerializer() { | ||
return new StarRocksWriterStateSerializer(); | ||
} | ||
|
||
@Override | ||
public Committer<StarRocksCommittable> createCommitter() throws IOException { | ||
return new StarRocksCommitter(); | ||
} | ||
|
||
@Override | ||
public SimpleVersionedSerializer<StarRocksCommittable> getCommittableSerializer() { | ||
return new StarRocksCommittableSerializer(); | ||
} | ||
} |
Oops, something went wrong.