(六):Flink 编程模板,Batch 示例,远程运行flink作业,CDH中HADOOP_CLASSPATH


2018/11/11

java模板

doc 地址:https://ci.apache.org/projects/flink/flink-docs-release-1.6/quickstart/java_api_quickstart.html。

  1. 新建一个目录,在该目录下:
  2. curl https://flink.apache.org/q/quickstart.sh | bash -s 1.6.1
    在这里插入图片描述
  3. 导入IDEA中一看,有些懵逼,还真只是个框架:
    在这里插入图片描述
  4. 这个模板的pom文件中,有不少可以借鉴的写法,以及打包减少体积和未来冲突可能性的办法。

ExecutionEnvironment

ExecutionEnvironment有两个子类:

  • {@link LocalEnvironment} will cause execution in the current JVM;
  • {@link RemoteEnvironment} will cause execution on a remote setup.

环境的初始化:

/** The environment of the context (local by default, cluster if invoked through command line). */

	private static ExecutionEnvironmentFactory contextEnvironmentFactory;

默认创建本地执行环境:

/** The local execution environment will run the program in a
	 * multi-threaded fashion in the same JVM as the environment was created in. The default
	 * parallelism of the local environment is the number of hardware contexts
	 */
public static LocalEnvironment createLocalEnvironment() {
   
		return createLocalEnvironment(defaultLocalDop);
	}

一些成员变量:
在这里插入图片描述

Batch 示例

wordcount

java版

该示例说明了一些Flink的DataSet API的使用。
github下载源码后,代码路径为:flink161_src/flink-release-1.6.1/flink-examples/flink-examples-batch/src

public class WordCount {
   
    public static void main(String[] args) throws Exception {
   
        final ParameterTool params = ParameterTool.fromArgs(args);
        // set up the execution environment
        final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
        // make parameters available in the web interface
        env.getConfig().setGlobalJobParameters(params);
        // get input data
        DataSet<String> text;
        if (params.has("input")) {
   
            // read the text file from given input path
            text = env.readTextFile(params.get("input"));
        } else {
   
            // get default test text data
            System.out.println("Executing WordCount example with default input data set.");
            System.out.println("Use --input to specify file input.");
            text = WordCountData.getDefaultTextLineDataSet(env);
        }

        DataSet<Tuple2<String, Integer>> counts =
                <
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值