-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature][Core] Upgrade flink source translation
- Loading branch information
1 parent
3c02b32
commit 573b25c
Showing
12 changed files
with
656 additions
and
20 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
52 changes: 52 additions & 0 deletions
52
...common/src/main/java/org/apache/seatunnel/translation/flink/source/FlinkRowCollector.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,52 @@ | ||
/* | ||
* 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 org.apache.seatunnel.translation.flink.source; | ||
|
||
import org.apache.seatunnel.api.source.Collector; | ||
import org.apache.seatunnel.api.table.type.SeaTunnelRow; | ||
import org.apache.seatunnel.api.table.type.SeaTunnelRowType; | ||
import org.apache.seatunnel.translation.flink.serialization.FlinkRowConverter; | ||
|
||
import org.apache.flink.api.connector.source.ReaderOutput; | ||
import org.apache.flink.types.Row; | ||
|
||
public class FlinkRowCollector implements Collector<SeaTunnelRow> { | ||
|
||
private final ReaderOutput<Row> readerOutput; | ||
|
||
private final FlinkRowConverter rowSerialization; | ||
|
||
public FlinkRowCollector(ReaderOutput<Row> readerOutput, SeaTunnelRowType seaTunnelRowType) { | ||
this.readerOutput = readerOutput; | ||
this.rowSerialization = new FlinkRowConverter(seaTunnelRowType); | ||
} | ||
|
||
@Override | ||
public void collect(SeaTunnelRow record) { | ||
try { | ||
readerOutput.collect(rowSerialization.convert(record)); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public Object getCheckpointLock() { | ||
return this; | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
...flink-common/src/main/java/org/apache/seatunnel/translation/flink/source/FlinkSource.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,104 @@ | ||
/* | ||
* 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 org.apache.seatunnel.translation.flink.source; | ||
|
||
import org.apache.seatunnel.api.serialization.Serializer; | ||
import org.apache.seatunnel.api.source.SeaTunnelSource; | ||
import org.apache.seatunnel.api.source.SourceSplit; | ||
import org.apache.seatunnel.api.source.SourceSplitEnumerator; | ||
import org.apache.seatunnel.api.table.type.SeaTunnelRow; | ||
import org.apache.seatunnel.api.table.type.SeaTunnelRowType; | ||
import org.apache.seatunnel.translation.flink.serialization.FlinkSimpleVersionedSerializer; | ||
import org.apache.seatunnel.translation.flink.utils.TypeConverterUtils; | ||
|
||
import org.apache.flink.api.common.typeinfo.TypeInformation; | ||
import org.apache.flink.api.connector.source.Boundedness; | ||
import org.apache.flink.api.connector.source.Source; | ||
import org.apache.flink.api.connector.source.SourceReader; | ||
import org.apache.flink.api.connector.source.SourceReaderContext; | ||
import org.apache.flink.api.connector.source.SplitEnumerator; | ||
import org.apache.flink.api.connector.source.SplitEnumeratorContext; | ||
import org.apache.flink.api.java.typeutils.ResultTypeQueryable; | ||
import org.apache.flink.core.io.SimpleVersionedSerializer; | ||
import org.apache.flink.types.Row; | ||
|
||
import java.io.Serializable; | ||
|
||
public class FlinkSource<SplitT extends SourceSplit, EnumStateT extends Serializable> | ||
implements Source<Row, SplitWrapper<SplitT>, EnumStateT>, ResultTypeQueryable<Row> { | ||
|
||
private final SeaTunnelSource<SeaTunnelRow, SplitT, EnumStateT> source; | ||
|
||
public FlinkSource(SeaTunnelSource<SeaTunnelRow, SplitT, EnumStateT> source) { | ||
this.source = source; | ||
} | ||
|
||
@Override | ||
public Boundedness getBoundedness() { | ||
org.apache.seatunnel.api.source.Boundedness boundedness = source.getBoundedness(); | ||
return boundedness == org.apache.seatunnel.api.source.Boundedness.BOUNDED | ||
? Boundedness.BOUNDED | ||
: Boundedness.CONTINUOUS_UNBOUNDED; | ||
} | ||
|
||
@Override | ||
public SourceReader<Row, SplitWrapper<SplitT>> createReader(SourceReaderContext readerContext) | ||
throws Exception { | ||
org.apache.seatunnel.api.source.SourceReader.Context context = | ||
new FlinkSourceReaderContext(readerContext, source); | ||
org.apache.seatunnel.api.source.SourceReader<SeaTunnelRow, SplitT> reader = | ||
source.createReader(context); | ||
return new FlinkSourceReader<>(reader, (SeaTunnelRowType) source.getProducedType()); | ||
} | ||
|
||
@Override | ||
public SplitEnumerator<SplitWrapper<SplitT>, EnumStateT> createEnumerator( | ||
SplitEnumeratorContext<SplitWrapper<SplitT>> enumContext) throws Exception { | ||
SourceSplitEnumerator.Context<SplitT> context = | ||
new FlinkSourceSplitEnumeratorContext<>(enumContext); | ||
SourceSplitEnumerator<SplitT, EnumStateT> enumerator = source.createEnumerator(context); | ||
return new FlinkSourceEnumerator<>(enumerator, enumContext); | ||
} | ||
|
||
@Override | ||
public SplitEnumerator<SplitWrapper<SplitT>, EnumStateT> restoreEnumerator( | ||
SplitEnumeratorContext<SplitWrapper<SplitT>> enumContext, EnumStateT checkpoint) | ||
throws Exception { | ||
FlinkSourceSplitEnumeratorContext<SplitT> context = | ||
new FlinkSourceSplitEnumeratorContext<>(enumContext); | ||
SourceSplitEnumerator<SplitT, EnumStateT> enumerator = | ||
source.restoreEnumerator(context, checkpoint); | ||
return new FlinkSourceEnumerator<>(enumerator, enumContext); | ||
} | ||
|
||
@Override | ||
public SimpleVersionedSerializer<SplitWrapper<SplitT>> getSplitSerializer() { | ||
return new SplitWrapperSerializer<>(source.getSplitSerializer()); | ||
} | ||
|
||
@Override | ||
public SimpleVersionedSerializer<EnumStateT> getEnumeratorCheckpointSerializer() { | ||
Serializer<EnumStateT> enumeratorStateSerializer = source.getEnumeratorStateSerializer(); | ||
return new FlinkSimpleVersionedSerializer<>(enumeratorStateSerializer); | ||
} | ||
|
||
@Override | ||
public TypeInformation<Row> getProducedType() { | ||
return (TypeInformation<Row>) TypeConverterUtils.convert(source.getProducedType()); | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
...on/src/main/java/org/apache/seatunnel/translation/flink/source/FlinkSourceEnumerator.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,87 @@ | ||
/* | ||
* 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 org.apache.seatunnel.translation.flink.source; | ||
|
||
import org.apache.seatunnel.api.source.SourceSplit; | ||
import org.apache.seatunnel.api.source.SourceSplitEnumerator; | ||
|
||
import org.apache.flink.api.connector.source.SplitEnumerator; | ||
import org.apache.flink.api.connector.source.SplitEnumeratorContext; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class FlinkSourceEnumerator<SplitT extends SourceSplit, EnumStateT> | ||
implements SplitEnumerator<SplitWrapper<SplitT>, EnumStateT> { | ||
|
||
private final SourceSplitEnumerator<SplitT, EnumStateT> sourceSplitEnumerator; | ||
|
||
private final SplitEnumeratorContext<SplitWrapper<SplitT>> enumeratorContext; | ||
|
||
private boolean isRun = false; | ||
|
||
public FlinkSourceEnumerator( | ||
SourceSplitEnumerator<SplitT, EnumStateT> enumerator, | ||
SplitEnumeratorContext<SplitWrapper<SplitT>> enumContext) { | ||
this.sourceSplitEnumerator = enumerator; | ||
this.enumeratorContext = enumContext; | ||
} | ||
|
||
@Override | ||
public void start() { | ||
sourceSplitEnumerator.open(); | ||
} | ||
|
||
@Override | ||
public void handleSplitRequest(int subtaskId, @Nullable String requesterHostname) { | ||
sourceSplitEnumerator.handleSplitRequest(subtaskId); | ||
} | ||
|
||
@Override | ||
public void addSplitsBack(List<SplitWrapper<SplitT>> splits, int subtaskId) { | ||
sourceSplitEnumerator.addSplitsBack( | ||
splits.stream().map(SplitWrapper::getSourceSplit).collect(Collectors.toList()), | ||
subtaskId); | ||
} | ||
|
||
@Override | ||
public void addReader(int subtaskId) { | ||
sourceSplitEnumerator.registerReader(subtaskId); | ||
if (!isRun) { | ||
try { | ||
sourceSplitEnumerator.run(); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
isRun = true; | ||
} | ||
} | ||
|
||
@Override | ||
public EnumStateT snapshotState(long checkpointId) throws Exception { | ||
return sourceSplitEnumerator.snapshotState(checkpointId); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
sourceSplitEnumerator.close(); | ||
} | ||
} |
Oops, something went wrong.