AiTi修炼|重剑无锋,拈花微笑 | IT修炼,技术fans.重剑无锋,拈花微笑才是最高境界,聊以此站记录一些个人学习、思考以及感悟Java,Scala,Spark,Hadoop,Storm,Nodejs,RSpark,Storm,Hbase,Hive,Flume等IT技术的历程
  • Home
Header
Header
$pageNumDef = 0;

Spark Streaming之基于HDFS数据源的实时wordCount

Spark Streaming基于HDFS的数据源进行实时计算,其主要原理是spark Streaming会监视指定的HDFS目录,并且处理出现在目录中的文件。因此注意的是:

  • 所有放入HDFS目录中的文件必须是相同的格式;

  • 必须使用移动或者重命名的方式,将文件移入目录,否则不会被处理;

  • 一旦文件处理过之后,该文件的内容即使改变,也不会再处理了;

  • 基于HDFS文件的数据源是没有Receiver的,因此不会占用一个cpu core,这是与socket方式数据源最大的区别。

基于HDFS数据源的实时wordCount代码实现比较简单具体实例如下,当然在生产实际中该方式并不常用,比较常见的企业级应用是与消息队列结合实现实时计算如kafka.

  • java版本代码实现如下:
import java.util.Arrays;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.function.FlatMapFunction;
import org.apache.spark.api.java.function.Function2;
import org.apache.spark.api.java.function.PairFunction;
import org.apache.spark.streaming.Durations;
import org.apache.spark.streaming.api.java.JavaDStream;
import org.apache.spark.streaming.api.java.JavaPairDStream;
import org.apache.spark.streaming.api.java.JavaStreamingContext;

import scala.Tuple2;

/**
 * 基于HDFS文件的实时wordcount程序,每5秒计算一次
 * @author rscala
 *
 */
public class HDFSWordCount {

    public static void main(String[] args) {
        SparkConf conf = new SparkConf()
                .setMaster("local[2]")
                .setAppName("HDFSWordCount");  
        JavaStreamingContext jssc = new JavaStreamingContext(conf, Durations.seconds(5));

        // 首先,使用JavaStreamingContext的textFileStream()方法,针对HDFS目录创建输入数据流
        JavaDStream<String> lines = jssc.textFileStream("hdfs://spark1:9020/wordcount_streaming");

        // 执行wordcount操作
        JavaDStream<String> words = lines.flatMap(new FlatMapFunction<String, String>() {

            private static final long serialVersionUID = 1L;

            @Override
            public Iterable<String> call(String line) throws Exception {
                return Arrays.asList(line.split(" "));
            }

        });

        JavaPairDStream<String, Integer> pairs = words.mapToPair(

                new PairFunction<String, String, Integer>() {

                    private static final long serialVersionUID = 1L;

                    @Override
                    public Tuple2<String, Integer> call(String word)
                            throws Exception {
                        return new Tuple2<String, Integer>(word, 1);
                    }

                });

        JavaPairDStream<String, Integer> wordCounts = pairs.reduceByKey(

                new Function2<Integer, Integer, Integer>() {

                    private static final long serialVersionUID = 1L;

                    @Override
                    public Integer call(Integer v1, Integer v2) throws Exception {
                        return v1 + v2;
                    }

                });

        wordCounts.print();

        jssc.start();
        jssc.awaitTermination();
        jssc.close();
    }

}

  • scala版本代码实现如下:
import org.apache.spark.SparkConf
import org.apache.spark.streaming.StreamingContext
import org.apache.spark.streaming.Seconds

/**
 * @author rscala
 */
object HDFSWordCount {

  def main(args: Array[String]): Unit = {
    val conf = new SparkConf()
        .setMaster("local[2]")  
        .setAppName("HDFSWordCount")
    val ssc = new StreamingContext(conf, Seconds(5))

    val lines = ssc.textFileStream("hdfs://spark1:9020/wordcount_steaming")  
    val words = lines.flatMap { _.split(" ") }  
    val pairs = words.map { word => (word, 1) }  
    val wordCounts = pairs.reduceByKey(_ + _)  

    wordCounts.print()  

    ssc.start()
    ssc.awaitTermination()
  }

}

rscala.com版权所有,本文Spark Streaming之基于HDFS数据源的实时wordCount
转载请注明出处:http://rscala.com/index.php/522.html