基于Java开发Streaming篇

Posted 飞跃小龙猫

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于Java开发Streaming篇相关的知识,希望对你有一定的参考价值。

package com.hj.spark;
import java.util.Arrays;
import java.util.Iterator;

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.JavaReceiverInputDStream;
import org.apache.spark.streaming.api.java.JavaStreamingContext;

import scala.Tuple2;
public class SparkStreaming {

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

		SparkConf conf = new SparkConf().setAppName("NetwordCount").setMaster("local[2]");
		// 功能入口
		JavaStreamingContext jssc =new  JavaStreamingContext(conf, Durations.seconds(1));
		// 创建一个Dstream 接收来自TCP的数据流 主机名 端口号
		JavaReceiverInputDStream<String> lines = jssc.socketTextStream("hadoop", 9999);
		
		JavaDStream<String> words = lines.flatMap(new FlatMapFunction<String, String>() {

			@Override
			public Iterator<String> call(String s) throws Exception {
				// TODO Auto-generated method stub
				return Arrays.asList(s.split(" ")).iterator();
				
			}
		});
		
		JavaPairDStream<String, Integer> pairs = words.mapToPair(new PairFunction<String, String, Integer>() {

			@Override
			public Tuple2<String, Integer> call(String s) throws Exception {
				// TODO Auto-generated method stub
				return new Tuple2<String, Integer>(s, 1);
			}
		});
		
		// reduceByKey
		JavaPairDStream<String, Integer> wordCount = pairs.reduceByKey(new Function2<Integer, Integer, Integer>() {
			
			@Override
			public Integer call(Integer arg0, Integer arg1) throws Exception {
				// TODO Auto-generated method stub
				return arg0 + arg1;
			}
		});
		
		wordCount.print();
		jssc.start();
		jssc.awaitTermination();
	}

}

以上是关于基于Java开发Streaming篇的主要内容,如果未能解决你的问题,请参考以下文章

Spark Streaming + Kafka整合(Kafka broker版本0.8.2.1+)

[Sparkhadoop]Spark Streaming整合kafka实战

iOS_直播类app_HTTP Live Streaming

iOS_直播类app_HTTP Live Streaming

自己开发的在线视频下载工具,基于Java多线程

Spark Streaming基于案例详解