View Javadoc
1   package org.wikimedia.eventutilities.flink.stream;
2   
3   import java.io.BufferedReader;
4   import java.io.IOException;
5   import java.io.InputStreamReader;
6   import java.nio.charset.StandardCharsets;
7   
8   import javax.annotation.Nullable;
9   
10  import org.apache.flink.api.common.serialization.DeserializationSchema;
11  import org.apache.flink.api.common.typeinfo.TypeInformation;
12  import org.apache.flink.configuration.Configuration;
13  import org.apache.flink.connector.file.src.reader.SimpleStreamFormat;
14  import org.apache.flink.connector.file.src.reader.StreamFormat;
15  import org.apache.flink.core.fs.FSDataInputStream;
16  
17  // NOTE: LineStreamFormat was removed from the main source during code review.
18  // This is used by fileStreamSourceBuilder method in TestEventDataStreamFactory.
19  // If we decide we want to promote fileStreamSourceBuilder back into the main EventDataStreamFactory
20  // in the future, we can move this class into main source then.
21  
22  /**
23   * A reader format that reads text lines from a file, and uses the provided DeserializationSchema to deserialize them.
24   *
25   * <p>The reader uses Java's built-in {@link InputStreamReader} to decode the byte stream using
26   * StandardCharsets.UTF_8.
27   *
28   * NOTE: This class is based on Flink 1.15.0's TextLineInputFormat.
29   *
30   * <p>This format does not support optimized recovery from checkpoints. On recovery, it will re-read
31   * and discard the number of lines that were processed before the last checkpoint. That is due to
32   * the fact that the offsets of lines in the file cannot be tracked through the charset decoders
33   * with their internal buffering of stream input and charset decoder state.
34   *
35   */
36  public class LineStreamFormat<T> extends SimpleStreamFormat<T> {
37  
38      private static final long serialVersionUID = 1L;
39  
40      private final DeserializationSchema<T> deserializationSchema;
41  
42      public LineStreamFormat(DeserializationSchema<T> deserializationSchema) {
43          this.deserializationSchema = deserializationSchema;
44      }
45  
46      @Override
47      public Reader<T> createReader(Configuration config, FSDataInputStream stream) {
48          final BufferedReader reader =
49              new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
50          return new Reader<T>(
51              reader,
52              deserializationSchema
53          );
54      }
55  
56      @Override
57      public TypeInformation<T> getProducedType() {
58          return deserializationSchema.getProducedType();
59      }
60  
61      // ------------------------------------------------------------------------
62  
63      /** The actual reader for the {@code LineStreamFormat}. */
64      private static final class Reader<T> implements StreamFormat.Reader<T> {
65  
66          private final BufferedReader reader;
67          private final DeserializationSchema<T> deserializationSchema;
68  
69          Reader(final BufferedReader reader, DeserializationSchema<T> deserializationSchema) {
70              this.deserializationSchema = deserializationSchema;
71              this.reader = reader;
72          }
73  
74          @Nullable
75          @Override
76          public T read() throws IOException {
77              String line = reader.readLine();
78              if (line == null) {
79                  return null;
80              }
81              return deserializationSchema.deserialize(line.getBytes(StandardCharsets.UTF_8));
82          }
83  
84          @Override
85          public void close() throws IOException {
86              reader.close();
87          }
88      }
89  }