Spark Streaming的样本demo统计
废话不多说,直接上代码
package com.demo; import java.util.List;
import java.util.regex.Pattern; import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.StorageLevels;
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 com.google.common.base.Optional;
import com.google.common.collect.Lists; import scala.Tuple2; public class NetWorkWordCount {
private static final Pattern SPACE = Pattern.compile(" "); public static void main(String[] args) {
//屏蔽日志
Logger.getLogger("org.apache.spark").setLevel(Level.OFF); // Create the context with a 1 second batch size
SparkConf sparkConf = new SparkConf().setAppName("NetworkWordCount").setMaster("local[2]");
JavaStreamingContext ssc = new JavaStreamingContext(sparkConf, Durations.seconds(1)); // Create a JavaReceiverInputDStream on target ip:port and count the
// words in input stream of \n delimited text (eg. generated by 'nc')
// Note that no duplication in storage level only for running locally.
// Replication necessary in distributed scenario for fault tolerance.
JavaReceiverInputDStream<String> lines = ssc.socketTextStream("192.168.49.151",9999, StorageLevels.MEMORY_AND_DISK_SER);
//增加checkpoint
ssc.checkpoint("/home/dinpay/stream/checkpoint");
JavaDStream<String> words = lines.flatMap(new FlatMapFunction<String, String>() {
@Override
public Iterable<String> call(String x) {
return Lists.newArrayList(SPACE.split(x));
}
}); JavaPairDStream<String, Integer> wordCounts = words.mapToPair(
new PairFunction<String, String, Integer>() {
@Override
public Tuple2<String, Integer> call(String s) {
return new Tuple2<String, Integer>(s, 1);
}
});
//无状态统计计算
JavaPairDStream<String, Integer> nostat = wordCounts.reduceByKey(new Function2<Integer, Integer, Integer>() {
@Override
public Integer call(Integer i1, Integer i2) {
return i1 + i2;
}
}); //有状态统计计算
JavaPairDStream<String, Integer> stat = wordCounts.updateStateByKey(new Function2<List<Integer>, Optional<Integer>,
Optional<Integer>>() {
@Override
public Optional<Integer> call(List<Integer> values, Optional<Integer> state){
Integer updateValue = 0;
if(state.isPresent()){
updateValue = state.get();
}
for (Integer value : values) {
updateValue += value;
}
return Optional.of(updateValue);
}
}); //窗口计算 滑动10秒 统计窗口长度是15秒
JavaPairDStream<String, Integer> windowstat = wordCounts
.reduceByKeyAndWindow(new Function2<Integer, Integer, Integer>() {
@Override
public Integer call(Integer i1, Integer i2) {
return i1 + i2;
}
}, Durations.seconds(15), Durations.seconds(30)); //nostat.print();
//stat.print();
windowstat.print(); ssc.start();
ssc.awaitTermination();
ssc.close();
} }
Spark Streaming的样本demo统计的更多相关文章
- Spark Streaming socketTextStream简单demo
正文 SparkStreaming的入口是StreamingContext,通过scala实现 一个简单的实时获取数据.代码SparkStreaming官网也可以找到. object SocketDS ...
- Spark Streaming 进阶与案例实战
Spark Streaming 进阶与案例实战 1.带状态的算子: UpdateStateByKey 2.实战:计算到目前位置累积出现的单词个数写入到MySql中 1.create table CRE ...
- Spark Streaming:大规模流式数据处理的新贵(转)
原文链接:Spark Streaming:大规模流式数据处理的新贵 摘要:Spark Streaming是大规模流式数据处理的新贵,将流式计算分解成一系列短小的批处理作业.本文阐释了Spark Str ...
- Spark Streaming:大规模流式数据处理的新贵
转自:http://www.csdn.net/article/2014-01-28/2818282-Spark-Streaming-big-data 提到Spark Streaming,我们不得不说一 ...
- 初步了解Spark生态系统及Spark Streaming
一. 场景 ◆ Spark[4]: Scope: a MapReduce-like cluster computing framework designed for low-laten ...
- spark streaming - kafka updateStateByKey 统计用户消费金额
场景 餐厅老板想要统计每个用户来他的店里总共消费了多少金额,我们可以使用updateStateByKey来实现 从kafka接收用户消费json数据,统计每分钟用户的消费情况,并且统计所有时间所有用户 ...
- 2、 Spark Streaming方式从socket中获取数据进行简单单词统计
Spark 1.5.2 Spark Streaming 学习笔记和编程练习 Overview 概述 Spark Streaming is an extension of the core Spark ...
- Spark Streaming 002 统计单词的例子
1.准备 事先在hdfs上创建两个目录: 保存上传数据的目录:hdfs://alamps:9000/library/SparkStreaming/data checkpoint的目录:hdfs://a ...
- 59、Spark Streaming与Spark SQL结合使用之top3热门商品实时统计案例
一.top3热门商品实时统计案例 1.概述 Spark Streaming最强大的地方在于,可以与Spark Core.Spark SQL整合使用,之前已经通过transform.foreachRDD ...
随机推荐
- python 列表、元组 详解
python中有6种序列的内置类型,分别为:列表,元组,字符串,Unicode字符串,buffer对象和xrange对象 列表和元组是最常见两种类型. 下面将以列表(list)和元组(tuple)为例 ...
- [oldboy-django][4python面试]cookie和session比较
session定义(知乎网上) Session的数据不是储存在客户端上的,而是储存在服务器上的:而客户端使用Cookie储存一个服务器分配的客户端会话序号(Session ID),当客户端请求服务器时 ...
- 2018CCPC网络赛
A - Buy and Resell HDU - 6438 The Power Cube is used as a stash of Exotic Power. There are nn cities ...
- SystemProperties反射调用
Systemproperties类在android.os下,但这个类是隐藏的,上层程序开发无法直接使用,只能通过java反射来调用 SystemPropertiesInvoke.java packag ...
- git 使用报错记录
错误一:git fatal: unable to write new index file主要原因就是服务器磁盘空间不够导致的,增加服务器空间就OK了在百度上面搜索没得到什么有效信息,在gooogle ...
- ci支持pathinfo
location ~ \.php { #去掉$ root H:/PHPServer/WWW; fastcgi_pass ; fastcgi_index index.php; fastcgi_split ...
- python知识集合
1.list list是一种有序的集合 例子:classmates = ['Michael', 'Bob', 'Tracy']; 方法:1. len len(classmates) //3 2.app ...
- [暑假集训--数论]poj2034 Anti-prime Sequences
Given a sequence of consecutive integers n,n+1,n+2,...,m, an anti-prime sequence is a rearrangement ...
- CSS3 Flex布局和Grid布局
1 flex容器的六个属性 flex实现垂直居中: <div class="box"> <span class="item">< ...
- cf 816E Karen and Supermarket
题目大意 给定\(n\)一颗树,每个点上有一个物品 每个物品有价格\(c[i]\) 有优惠券,能使价格减少\(d[i]\) 但是使用优惠券的前提时购买该物品,且父亲也使用优惠券 给定钱包余额\(lim ...