Hadoop formats

Project Configuration

Support for Hadoop is contained in the flink-hadoop-compatibility Maven module.

Add the following dependency to your pom.xml to use hadoop

  1. <dependency>
  2. <groupId>org.apache.flink</groupId>
  3. <artifactId>flink-hadoop-compatibility_2.12</artifactId>
  4. <version>1.19.0</version>
  5. </dependency>

If you want to run your Flink application locally (e.g. from your IDE), you also need to add a hadoop-client dependency such as:

  1. <dependency>
  2. <groupId>org.apache.hadoop</groupId>
  3. <artifactId>hadoop-client</artifactId>
  4. <version>2.10.2</version>
  5. <scope>provided</scope>
  6. </dependency>

Using Hadoop InputFormats

To use Hadoop InputFormats with Flink the format must first be wrapped using either readHadoopFile or createHadoopInput of the HadoopInputs utility class. The former is used for input formats derived from FileInputFormat while the latter has to be used for general purpose input formats. The resulting InputFormat can be used to create a data source by using ExecutionEnvironment#createInput.

The resulting DataStream contains 2-tuples where the first field is the key and the second field is the value retrieved from the Hadoop InputFormat.

The following example shows how to use Hadoop’s TextInputFormat.

Java

  1. StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
  2. KeyValueTextInputFormat textInputFormat = new KeyValueTextInputFormat();
  3. DataStream<Tuple2<Text, Text>> input = env.createInput(HadoopInputs.readHadoopFile(
  4. textInputFormat, Text.class, Text.class, textPath));
  5. // Do something with the data.
  6. [...]

Scala

  1. val env = StreamExecutionEnvironment.getExecutionEnvironment
  2. val textInputFormat = new KeyValueTextInputFormat
  3. val input: DataStream[(Text, Text)] =
  4. env.createInput(HadoopInputs.readHadoopFile(
  5. textInputFormat, classOf[Text], classOf[Text], textPath))
  6. // Do something with the data.
  7. [...]