1+ /*
2+ * Licensed to the Apache Software Foundation (ASF) under one
3+ * or more contributor license agreements. See the NOTICE file
4+ * distributed with this work for additional information
5+ * regarding copyright ownership. The ASF licenses this file
6+ * to you under the Apache License, Version 2.0 (the
7+ * "License"); you may not use this file except in compliance
8+ * with the License. You may obtain a copy of the License at
9+ *
10+ * http://www.apache.org/licenses/LICENSE-2.0
11+ *
12+ * Unless required by applicable law or agreed to in writing, software
13+ * distributed under the License is distributed on an "AS IS" BASIS,
14+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+ * See the License for the specific language governing permissions and
16+ * limitations under the License.
17+ */
18+
19+ package org .apache .wayang .flink .operators ;
20+
21+ import org .apache .flink .api .common .eventtime .WatermarkStrategy ;
22+ import org .apache .flink .connector .file .src .FileSource ;
23+ import org .apache .flink .connector .file .src .reader .TextLineInputFormat ;
24+ import org .apache .flink .core .fs .Path ;
25+ import org .apache .flink .streaming .api .datastream .DataStream ;
26+ import org .apache .flink .streaming .api .environment .StreamExecutionEnvironment ;
27+
28+ import org .apache .wayang .basic .operators .TextFileSource ;
29+ import org .apache .wayang .core .optimizer .OptimizationContext .OperatorContext ;
30+ import org .apache .wayang .core .optimizer .costs .LoadProfileEstimators ;
31+ import org .apache .wayang .core .platform .ChannelDescriptor ;
32+ import org .apache .wayang .core .platform .ChannelInstance ;
33+ import org .apache .wayang .core .platform .lineage .ExecutionLineageNode ;
34+ import org .apache .wayang .core .util .Tuple ;
35+ import org .apache .wayang .flink .channels .DataStreamChannel ;
36+ import org .apache .wayang .flink .execution .FlinkExecutor ;
37+
38+ import java .time .Duration ;
39+ import java .util .Collection ;
40+ import java .util .List ;
41+
42+ /**
43+ * Opens a Flink continuous {@link DataStream} that monitors a file directory.
44+ */
45+ public class FlinkContinuousTextFileSource extends TextFileSource implements FlinkExecutionOperator {
46+
47+ public FlinkContinuousTextFileSource (final TextFileSource that ) {
48+ super (that );
49+ }
50+
51+ public FlinkContinuousTextFileSource (final String inputUrl ) {
52+ super (inputUrl );
53+ }
54+
55+ @ Override
56+ public List <ChannelDescriptor > getSupportedInputChannels (final int index ) {
57+ throw new UnsupportedOperationException ("Unimplemented method 'getSupportedInputChannels'" );
58+ }
59+
60+ @ Override
61+ public List <ChannelDescriptor > getSupportedOutputChannels (final int index ) {
62+ throw new UnsupportedOperationException ("Unimplemented method 'getSupportedOutputChannels'" );
63+ }
64+
65+ @ Override
66+ public Tuple <Collection <ExecutionLineageNode >, Collection <ChannelInstance >> evaluate (final ChannelInstance [] inputs ,
67+ final ChannelInstance [] outputs , final FlinkExecutor flinkExecutor , final OperatorContext operatorContext )
68+ throws Exception {
69+ assert inputs .length == this .getNumInputs ();
70+ assert outputs .length == this .getNumOutputs ();
71+
72+ final DataStreamChannel .Instance output = (DataStreamChannel .Instance ) outputs [0 ];
73+
74+ final StreamExecutionEnvironment env = StreamExecutionEnvironment .getExecutionEnvironment ();
75+
76+ final FileSource <String > fs = FileSource
77+ .forRecordStreamFormat (new TextLineInputFormat (), new Path (this .getInputUrl ()))
78+ //TODO: I set manually 1 here but should be in config.
79+ .monitorContinuously (Duration .ofSeconds (1 ))
80+ .build ();
81+
82+ final DataStream <String > dataStream = env .fromSource (fs , WatermarkStrategy .noWatermarks (),
83+ "FlinkDataStreamFileSource[" + this .getInputUrl () + "]" );
84+
85+ output .accept (dataStream );
86+
87+ final ExecutionLineageNode prepareLineageNode = new ExecutionLineageNode (operatorContext );
88+ prepareLineageNode .add (LoadProfileEstimators .createFromSpecification (
89+ "wayang.flink.textfilesource.load.prepare" , flinkExecutor .getConfiguration ()));
90+
91+ final ExecutionLineageNode mainLineageNode = new ExecutionLineageNode (operatorContext );
92+ mainLineageNode .add (LoadProfileEstimators .createFromSpecification (
93+ "wayang.flink.textfilesource.load.main" , flinkExecutor .getConfiguration ()));
94+
95+ output .getLineage ().addPredecessor (mainLineageNode );
96+
97+ return prepareLineageNode .collectAndMark ();
98+ }
99+
100+ @ Override
101+ public boolean containsAction () {
102+ throw new UnsupportedOperationException ("Unimplemented method 'containsAction'" );
103+ }
104+
105+ }
0 commit comments