Flink 的 Batch 批处理例子 Word count 入门

Flink 认为 Batch 是 Streaming 的一个特例,所以 Flink 底层引擎是一个流式引擎,在上面实现了流处理和批处理。这篇文章先介绍 Batch例子

 

Word Count

 

WordCounts 是大数据系统处理中的入门案例。它从一个文本集合中计算单词出现的频率。这个算法分成两个步骤:

第一步:文本 split 切分成独立的单词

第二部:分组和统计

 

package org.apache.flink.examples.scala.wordcount

import org.apache.flink.api.java.utils.ParameterTool
import org.apache.flink.api.scala._
import org.apache.flink.examples.java.wordcount.util.WordCountData

/**
 * Implements the "WordCount" program that computes a simple word occurrence histogram
 * over text files. 
 *
 * The input is a plain text file with lines separated by newline characters.
 *
 * Usage:
 * {{{
 *   WordCount --input <path> --output <path>
 * }}}
 *
 * If no parameters are provided, the program is run with default data from
 * [[org.apache.flink.examples.java.wordcount.util.WordCountData]]
 *
 * This example shows how to:
 *
 *   - write a simple Flink program.
 *   - use Tuple data types.
 *   - write and use user-defined functions.
 *
 */
object WordCount {

  def main(args: Array[String]) {

    val params: ParameterTool = ParameterTool.fromArgs(args)

    // set up execution environment
    val env = ExecutionEnvironment.getExecutionEnvironment

    // make parameters available in the web interface
    env.getConfig.setGlobalJobParameters(params)

    // scala 的 val 赋值语句如此灵活!
    val text =
      if (params.has("input")) {
        env.readTextFile(params.get("input"))
      } else {
        println("Executing WordCount example with default input data set.")
        println("Use --input to specify file input.")
        env.fromCollection(WordCountData.WORDS)
      }

    
    val counts = text.flatMap { _.toLowerCase.split("\\W+") filter { _.nonEmpty } }
      .map { (_, 1) }
      .groupBy(0)
      .sum(1)
    // 可以再加一个 sort

    if (params.has("output")) {
      counts.writeAsCsv(params.get("output"), "\n", " ")
      env.execute("Scala WordCount Example")
    } else {
      println("Printing result to stdout. Use --output to specify output path.")
      counts.print()
    }

  }
}

作为测试数据,任意的文本文件都可以使用

 

Running an example

In order to run a Flink example, we assume you have a running Flink instance available. The “Quickstart” and “Setup” tabs in the navigation describe various ways of starting Flink.

The easiest way is running the ./bin/start-cluster.sh, which by default starts a local cluster with one JobManager and one TaskManager.

Each binary release of Flink contains an examples directory with jar files for each of the examples on this page.

To run the WordCount example, issue the following command:

./bin/flink run ./examples/batch/WordCount.jar

The other examples can be started in a similar way.

Note that many examples run without passing any arguments for them, by using build-in data. To run WordCount with real data, you have to pass the path to the data:

./bin/flink run ./examples/batch/WordCount.jar --input /path/to/some/text/data --output /path/to/result

Note that non-local file systems require a schema prefix, such as hdfs://.

 

比如可以统计 Flink 目录下 LICENSE 文件中的单词计数,如果不指定 output, 计算结果就直接输出到标准输出:

./bin/flink run ./examples/batch/WordCount.jar --input ./LICENSE
输出:
Setting HADOOP_CONF_DIR=/etc/hadoop/conf because no HADOOP_CONF_DIR was set.
Starting execution of program
Printing result to stdout. Use --output to specify output path.
(0,3)
(1,2)
(2,4)
(2004,1)
(3,1)
(4,1)
(5,1)
(50,1)
(6,1)
(7,1)
(8,1)
(9,2)
(a,22)
(above,1)
(acceptance,1)
(accepting,3)
(act,1)
(acting,1)
(acts,1)
(add,2)
(addendum,1)
(additional,5)
(additions,1)
(advised,1)
。。。。

 

 

 

 

 

 

Flink SQL 批处理是指使用 Flink 提供的 SQL API 对批量数据进行处理和分析的过程。在 Flink 中,可以通过编写 SQL 查询语句来对批量数据进行过滤、聚合、计算等操作,从而实现对大规模数据的高效处理。 通过 Flink SQL 批处理,可以快速地实现对数据的批量处理,同时可以利用 Flink 强大的并行计算能力和优化的执行引擎来提高处理效率和性能。在实际应用中,Flink SQL 批处理可以用于数据清洗、转换、统计、报表生成等各种数据处理场景。 Flink SQL 批处理的流程通常包括以下几个步骤:数据源读取、SQL 查询编写、执行计算、结果输出等。首先,需要从数据源中读取批量数据,可以是文件、数据库、消息队列等形式。然后,可以编写 SQL 查询语句来对数据进行筛选、聚合、计算等操作。接下来,Flink 会根据 SQL 查询语句生成相应的计算图,并对数据进行并行计算。最后,计算结果可以输出到文件、数据库、消息队列等目的地。 Flink SQL 批处理具有扩展性好、性能高、易用性强等优点,适合于需要对大规模批量数据进行处理和分析的场景。同时,Flink 还提供了丰富的内置函数和用户自定义函数接口,可以满足各种复杂数据处理需求。综上所述,Flink SQL 批处理是一种强大的数据处理工具,可以帮助用户快速高效地处理大规模批量数据。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值