美文网首页
Flink Demo工程

Flink Demo工程

作者: 晚歌歌 | 来源:发表于2021-11-01 15:44 被阅读0次

https://ci.apache.org/projects/flink/flink-docs-release-1.2/quickstart/java_api_quickstart.html#quickstart-script
mvn archetype:generate -DarchetypeGroupId=org.apache.flink -DarchetypeArtifactId=flink-quickstart-java -DarchetypeVersion=1.2.0

image.png
package com.flink;

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.util.Collector;

/**
 * Implements the "WordCount" program that computes a simple word occurrence histogram
 * over some sample data
 *
 * <p>
 * This example shows how to:
 * <ul>
 * <li>write a simple Flink program.
 * <li>use Tuple data types.
 * <li>write and use user-defined functions.
 * </ul>
 *
 */
public class WordCount {

    //
    //  Program
    //

    public static void main(String[] args) throws Exception {

        // set up the execution environment
        final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

        // get input data
        DataSet<String> text = env.fromElements(
                "To be, or not to be,--that is the question:--",
                "Whether 'tis nobler in the mind to suffer",
                "The slings and arrows of outrageous fortune",
                "Or to take arms against a sea of troubles,"
                );

        DataSet<Tuple2<String, Integer>> counts =
                // split up the lines in pairs (2-tuples) containing: (word,1)
                text.flatMap(new LineSplitter())
                // group by the tuple field "0" and sum up tuple field "1"
                .groupBy(0)
                .sum(1);

        // execute and print result
        counts.print();

    }

    //
    //  User Functions
    //

    /**
     * Implements the string tokenizer that splits sentences into words as a user-defined
     * FlatMapFunction. The function takes a line (String) and splits it into
     * multiple pairs in the form of "(word,1)" (Tuple2<String, Integer>).
     */
    public static final class LineSplitter implements FlatMapFunction<String, Tuple2<String, Integer>> {

        @Override
        public void flatMap(String value, Collector<Tuple2<String, Integer>> out) {
            // normalize and split the line
            String[] tokens = value.toLowerCase().split("\\W+");

            // emit the pairs
            for (String token : tokens) {
                if (token.length() > 0) {
                    out.collect(new Tuple2<String, Integer>(token, 1));
                }
            }
        }
    }
}

相关文章

  • Flink Demo工程

    https://ci.apache.org/projects/flink/flink-docs-release-1...

  • flink demo坏境搭建

    #flink demo开发坏境搭建flink 入门idea的demo,Flink 提供了比较方便的创建 Flink...

  • 2021-01-12

    计划 hive , flink sql, flink demo 实际 FLINK SQL栏目https://www...

  • Flink Kafka Doris实战demo

    Flink Kafka Doris实战demo 环境: Flink 1.12 Doris 0.12 Kafka 1...

  • Flink demo

    下载 Flink https://flink.apache.org/downloads.html#apache-f...

  • Flink1.7从安装到体验

    本文要点:实战Flink1.7的安装和Demo运行过程 1、准备工作;2、下载Flink1.7;3、安装Flink...

  • flink demo 可以通过IDE工具直接运行

    运行 flink demo 的时候,我第一个问题是,需要安装 flink ,运行在 flink 环境呢,还是在id...

  • Flink Steam Demo

    使用mvn创建项目 增加最后一个选项防止卡死在创建阶段. 修改依赖 编写代码创建WikipediaAnalysis...

  • flink ClickHourseSink demo

    sink demo 依赖 两个选一个

  • 组件,代码规范探讨

    1.组件规范 需要开发新组件时,首先要创建一个demo工程,然后在demo工程中创建组件工程。要求demo工程能独...

网友评论

      本文标题:Flink Demo工程

      本文链接:https://www.haomeiwen.com/subject/yjgzaltx.html