如何在将flink数据集写入hdfs时创建作业对象

gmxoilav  于 2021-06-21  发布在  Flink
关注(0)|答案(1)|浏览(390)

我想给hive写一些数据集。我尝试了hivejdbc,但它不支持batchexecute。所以我改为将其写入hdfs,然后生成hive表。
我尝试使用以下代码来编写hdfs:

package test;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.hadoop.mapreduce.HadoopOutputFormat;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.hadoop.fs.Path;
import org.apache.flink.fs.s3hadoop.shaded.org.apache.hadoop.io.IntWritable;
import org.apache.flink.fs.s3presto.shaded.org.apache.hadoop.io.Text;
import org.apache.flink.fs.s3presto.shaded.org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.flink.util.Collector;
import org.apache.hadoop.mapreduce.Job;

public class Test {
    public static void main(String[] args) {

        final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
        DataSet<String> text = env.fromElements(
                "Who's there?",
                "I think I hear them. Stand, ho! Who's there?");

            DataSet<Tuple2<String, Integer>> hadoopResult = text
                .flatMap(new LineSplitter())
                .groupBy(0)
                .sum(1);
            //job and jobConf is null,I do not know how to initialize them (new)
        Job job = null;
        Job jobConf = null;
        HadoopOutputFormat<String, Integer> hadoopOF =
                  new HadoopOutputFormat<String, Integer>(
                    new TextOutputFormat<String, Integer>(), job
                  );
                hadoopOF.getConfiguration().set("mapreduce.output.textoutputformat.separator", " ");
                TextOutputFormat.setOutputPath(jobConf, new Path("hdfs://somewhere/"));
                hadoopResult.output(hadoopOF);
    }

     public static class LineSplitter implements FlatMapFunction<String, Tuple2<String, Integer>> {
        private static final long serialVersionUID = 3100297611484689639L;
            public void flatMap(String line, Collector<Tuple2<String, Integer>> out) {
                for (String word : line.split(" ")) {
                    out.collect(new Tuple2<String, Integer>(word, 1));
                }
            }
        }
}

但是有很多编译错误。所有的代码都是从官方网站复制和缝合这些代码。
我的问题是:如何创建job和jobconf对象,然后将数据集写入hdfs?

q3qa4bjr

q3qa4bjr1#

您将在文档中找到一个完整的示例。
要创建作业,请执行以下操作:

Job job = Job.getInstance();

我认为您不需要jobconf对象——似乎您可以在这两个地方使用job对象。

相关问题