spark streaming简单示例
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.</modelVersion>
<parent>
<artifactId>bigdata</artifactId>
<groupId>qinfeng.zheng</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>qinfeng.zheng</groupId>
<artifactId>spark-streaming</artifactId>
<version>1.0-SNAPSHOT</version> <dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.</artifactId>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming_2.</artifactId>
</dependency> <dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming-kafka_2.</artifactId>
</dependency> </dependencies> <build>
<sourceDirectory>src/main/scala</sourceDirectory>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
<configuration>
<args>
<arg>-make:transitive</arg>
<arg>-dependencyfile</arg>
<arg>${project.build.directory}/.scala_dependencies</arg>
</args>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.</version>
<configuration>
<useFile>false</useFile>
<disableXmlReport>true</disableXmlReport>
<includes>
<include>**/*Test.*</include>
<include>**/*Suite.*</include>
</includes>
</configuration>
</plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>qinfeng.zheng.java.KafkaReceiverWordCount</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
import org.apache.spark.SparkConf
import org.apache.spark.streaming.dstream.DStream
import org.apache.spark.streaming.{Seconds, StreamingContext} /**
* 创建时间: 10:57 2018/7/8
* 修改时间:
* 编码人员: ZhengQf
* 版 本: 0.0.1
* 功能描述: 流式读取hdfs://hdp01:9000/wc/目录下面的文件内容,计算wordcount
* 最好打成jar上传到linux服务器上运行.windows平台有时不会打印内容
*/
object HDFSWordCount {
def main(args: Array[String]): Unit = {
// System.setProperty("HADOOP_USER_NAME","root")
val conf = new SparkConf().setAppName("HDFSWordCount").setMaster("local")
// val sc = new SparkContext(conf)
// val rdd = sc.textFile("hdfs://hdp01:9000/wc/wc.txt")
// rdd.foreach(print)
val scc = new StreamingContext(conf, Seconds(10));
//同一个文件名的文件不会重复读取,即便是修改了文件内容也不会重复读取
val lines = scc.textFileStream("D:\\tmp\\wc") //读取本地文件
//读取hdfs上的文件,在window读取hdfs可能存在问题
// val lines = scc.textFileStream("hdfs://hdp01:9000/wc/") val words: DStream[String] = lines.flatMap(_.split(" "))
val wordPairs: DStream[(String, Int)] = words.map((_, 1))
val wc: DStream[(String, Int)] = wordPairs.reduceByKey(_ + _)
//wc.saveAsTextFiles("./stream/") //指定计算结果的存储路径
wc.print() //print action算子
scc.start()
scc.awaitTermination()
scc.stop()
} }
spark streaming简单示例的更多相关文章
- Spark Streaming编程示例
近期也有开始研究使用spark streaming来实现流式处理.本文以流式计算word count为例,简单描述如何进行spark streaming编程. 1. 依赖的jar包 参考<分别用 ...
- 整合Kafka到Spark Streaming——代码示例和挑战
作者Michael G. Noll是瑞士的一位工程师和研究员,效力于Verisign,是Verisign实验室的大规模数据分析基础设施(基础Hadoop)的技术主管.本文,Michael详细的演示了如 ...
- Hadoop、storm和Spark Streaming简单介绍(非原创)
文章大纲 一.Hadoop是什么二.storm是什么三.Spark Streaming是什么四.Spark与storm比较五.参考文章 一.Hadoop是什么 1. 简介 Hadoop是一个由Ap ...
- spark streaming 实时计算
spark streaming 开发实例 本文将分以下几部分 spark 开发环境配置 如何创建spark项目 编写streaming代码示例 如何调试 环境配置: spark 原生语言是scala, ...
- Spark Streaming初探
1. 介绍 Spark Streaming是Spark生态系统中一个重要的框架,建立在Spark Core之上,与Spark SQL.GraphX.MLib相并列. Spark Streaming是 ...
- [spark]Spark Streaming教程
(一)官方入门示例 废话不说,先来个示例,有个感性认识再介绍. 这个示例来自spark自带的example,基本步骤如下: (1)使用以下命令输入流消息: $ nc -lk 9999 (2)在一个 ...
- 学习笔记:spark Streaming的入门
spark Streaming的入门 1.概述 spark streaming 是spark core api的一个扩展,可实现实时数据的可扩展,高吞吐量,容错流处理. 从上图可以看出,数据可以有很多 ...
- 【Spark】Spark Streaming + Kafka direct 的 offset 存入Zookeeper并重用
Spark Streaming + Kafka direct 的 offset 存入Zookeeper并重用 streaming offset设置_百度搜索 将 Spark Streaming + K ...
- Spark Streaming的简单介绍
本文讲解Spark流数据处理之Spark Streaming.本文的写作时值Spark 1.6.2发布之际,Spark 2.0预览版也已发布,Spark发展如此迅速,请随时关注Spark Stream ...
随机推荐
- ZOJ 3329 One Person Game(概率DP,求期望)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3754 题目大意: 有三个骰子,分别有K1,K2,K3个面,一次投掷可以得到三个 ...
- mac的jvm调优工具
安装好JDK之后调优工具所在位置为: /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/jvisualvm j ...
- content is not supported outside 'script" or asp content' region
https://stackoverflow.com/questions/48915080/asp-net-content-is-not-supported-outside-the-script-or- ...
- Jmeter中if 控制器的使用
使用if控制器有两种方式:1.不勾选“interpret condition as variable expression”直接输入我们需要判断的表达式即可,判断表达式为真时,执行if控制器下的请求, ...
- Vagrant 构建 LNMP 一致环境
GitHub 地址 <--- 所有文件都在这里 前提条件 安装 Vagrant,VirtualBox. 设置 下载软件并放入 soft 目录 MySQL:mysql-5.7.22-1.el7.x ...
- memcpy复制字符串的注意事项/memcpy不能用来拷贝类类型
strcpy复制src到dst,最后将dst的下一个位置置为'\0',所以dst是以'\0'结尾的字符串 ] = "abcde"; cout << c1 << ...
- Python算法每日一题--002--求众数
给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3]输出: 3示 ...
- 爬虫中GET方法应用基本模型
根据get方法,更改界面url从而获取信息 GET请求URL附带查询参数 POST请求保存在form表单中 分析百度贴吧url特点: 分析url https://tieba.baidu.com/f是贴 ...
- 【SD系列】SAP SD模块-销售收入科目的配置
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[SD系列]SAP SD模块-销售收入科目的配置 ...
- spring-第二篇ApplicationContext国际化及事件机制
1.ApplicationContext接口获取spring容器 ApplicationContext是BeanFactory接口的子接口,BeanFactory的常用实现类是Default ...