根据Flink

时间:2019-04-10 12:22:47

标签: apache-flink google-cloud-dataproc

由于Flink支持Hadoop FileSystem抽象,并且有一个GCS connector-库在Google Cloud Storage之上实现。

如何使用此存储库中的代码创建Flink文件源?

1 个答案:

答案 0 :(得分:2)

要实现这一目标,您需要:

  1. Install and configure Flink群集上的GCS连接器。
  2. 将Hadoop和Flink依赖项(包括HDFS connector)添加到您的项目中:
    <dependency>
        <groupId>org.apache.flink</groupId>
        <artifactId>flink-scala_2.11</artifactId>
        <version>${flink.version}</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.flink</groupId>
        <artifactId>flink-connector-filesystem_2.11</artifactId>
        <version>${flink.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.flink</groupId>
        <artifactId>flink-hadoop-compatibility_2.11</artifactId>
        <version>${flink.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-common</artifactId>
        <version>${hadoop.version}</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-mapreduce-client-core</artifactId>
        <version>${hadoop.version}</version>
        <scope>provided</scope>
    </dependency>
    
  3. Use it创建具有GCS路径的数据源:

    import org.apache.flink.api.java.DataSet;
    import org.apache.flink.api.java.ExecutionEnvironment;
    import org.apache.flink.api.java.tuple.Tuple2;
    import org.apache.flink.hadoopcompatibility.HadoopInputs;
    import org.apache.hadoop.io.LongWritable;
    import org.apache.hadoop.io.Text;
    import org.apache.hadoop.mapred.TextInputFormat;
    
    ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
    
    DataSet<Tuple2<LongWritable, Text>> input =
        env.createInput(
            HadoopInputs.readHadoopFile(
                new TextInputFormat(), LongWritable.class, Text.class, "gs://bucket/path/some*pattern/"));