场景:使用Spark Streaming接收HDFS上的文件数据与关系型数据库中的表进行相关的查询操作;

使用技术:Spark Streaming + Spark JDBC External DataSources

 
HDFS上文件的数据格式为:id、name、cityId,分隔符为tab 
1       zhangsan        1
2 lisi 1
3 wangwu 2
4 zhaoliu 3
MySQL的表city结构为:id int, name varchar
1    bj
2 sz
3 sh
本案例的结果为:select s.id, s.name, s.cityId, c.name from student s join city c on s.cityId=c.id;

示例代码:

package com.asiainfo.ocdc

case class Student(id: Int, name: String, cityId: Int)
package com.asiainfo.ocdc

import org.apache.spark.SparkConf
import org.apache.spark.streaming.{Seconds, StreamingContext}
import org.apache.spark.SparkContext
import org.apache.spark.sql.hive.HiveContext /**
* Spark Streaming处理HDFS上的数据并结合Spark JDBC外部数据源处理
*
* @author luogankun
*/
object HDFSStreaming {
def main(args: Array[String]) { if (args.length < 1) {
System.err.println("Usage: HDFSStreaming <path>")
System.exit(1)
} val location = args(0) val sparkConf = new SparkConf()
val sc = new SparkContext(sparkConf)
val ssc = new StreamingContext(sc, Seconds(5)) val sqlContext = new HiveContext(sc)
import sqlContext._ import com.luogankun.spark.jdbc._
//使用External Data Sources处理MySQL中的数据
val cities = sqlContext.jdbcTable("jdbc:mysql://hadoop000:3306/test", "root", "root", "select id, name from city")
//将cities RDD注册成city临时表
cities.registerTempTable("city") val inputs = ssc.textFileStream(location)
inputs.foreachRDD(rdd => {
if (rdd.partitions.length > 0) {
//将Streaming中接收到的数据注册成student临时表
rdd.map(_.split("\t")).map(x => Student(x(0).toInt, x(1), x(2).toInt)).registerTempTable("student"); //关联Streaming和MySQL表进行查询操作
sqlContext.sql("select s.id, s.name, s.cityId, c.name from student s join city c on s.cityId=c.id").collect().foreach(println)
}
}) ssc.start()
ssc.awaitTermination()
}
}

提交到集群执行脚本:sparkstreaming_hdfs_jdbc.sh

#!/bin/sh
. /etc/profile
set -x cd $SPARK_HOME/bin spark-submit \
--name HDFSStreaming \
--class com.asiainfo.ocdc.HDFSStreaming \
--master spark://hadoop000:7077 \
--executor-memory 1G \
--total-executor-cores \
/home/spark/software/source/streaming-app/target/streaming-app-V00B01C00-SNAPSHOT-jar-with-dependencies.jar \
hdfs://hadoop000:8020/data/hdfs

Spark Streaming、HDFS结合Spark JDBC External DataSouces处理案例的更多相关文章

  1. Spark Streaming、Kafka结合Spark JDBC External DataSouces处理案例

    场景:使用Spark Streaming接收Kafka发送过来的数据与关系型数据库中的表进行相关的查询操作: Kafka发送过来的数据格式为:id.name.cityId,分隔符为tab zhangs ...

  2. Spark Streaming揭秘 Day35 Spark core思考

    Spark Streaming揭秘 Day35 Spark core思考 Spark上的子框架,都是后来加上去的.都是在Spark core上完成的,所有框架一切的实现最终还是由Spark core来 ...

  3. Spark Streaming之四:Spark Streaming 与 Kafka 集成分析

    前言 Spark Streaming 诞生于2013年,成为Spark平台上流式处理的解决方案,同时也给大家提供除Storm 以外的另一个选择.这篇内容主要介绍Spark Streaming 数据接收 ...

  4. Offset Management For Apache Kafka With Apache Spark Streaming

    An ingest pattern that we commonly see being adopted at Cloudera customers is Apache Spark Streaming ...

  5. <Spark><Spark Streaming>

    Overview Spark Streaming为用户提供了一套与batch jobs十分相似的API,以编写streaming应用 与Spark的基本概念RDDs类似,Spark Streaming ...

  6. 大数据开发实战:Spark Streaming流计算开发

    1.背景介绍 Storm以及离线数据平台的MapReduce和Hive构成了Hadoop生态对实时和离线数据处理的一套完整处理解决方案.除了此套解决方案之外,还有一种非常流行的而且完整的离线和 实时数 ...

  7. 小记---------spark组件与其他组件的比较 spark/mapreduce ;spark sql/hive ; spark streaming/storm

    Spark与Hadoop的对比   Scala是Spark的主要编程语言,但Spark还支持Java.Python.R作为编程语言 Hadoop的编程语言是Java    

  8. Apache Spark 2.2.0 中文文档 - Spark Streaming 编程指南

    Spark Streaming 编程指南 概述 一个入门示例 基础概念 依赖 初始化 StreamingContext Discretized Streams (DStreams)(离散化流) Inp ...

  9. Spark踩坑记——Spark Streaming+Kafka

    [TOC] 前言 在WeTest舆情项目中,需要对每天千万级的游戏评论信息进行词频统计,在生产者一端,我们将数据按照每天的拉取时间存入了Kafka当中,而在消费者一端,我们利用了spark strea ...

随机推荐

  1. Java in a Nutshell学习笔记

    1, bytecode永远是大段 2,其它语言要在java里运行,要么实现类似于javac的编译器,把该语言解释成为class文件.要么,直接重新实现JVM,直接解释该语言3,Java和C++区别: ...

  2. 使用squid配置透明代理并对上网行为进行控制

    使用Squid配置透明代理 环境:CentOS 6.4 + squid-3.1.10-20.el6_5.3.x86_64 1.检查squid是否默认安装,没有安装先安装 rpm -qa squid 假 ...

  3. PHP 配置文件中open_basedir选项作用

    如下是php.ini中的原文说明以及默认配置: ; open_basedir, if set, limits all file operations to the defined directory  ...

  4. priority_queue 优先队列用法

    //采用默认优先关系: //(priority_queue<int>que;) //Queue 0: // 91 83 72 56 47 36 22 14 10 7 3 // //采用结构 ...

  5. Brief Tour of the Standard Library

    10.1. Operating System Interface The os module provides dozens of functions for interacting with the ...

  6. USB协议-USB设备的枚举过程

    USB主机在检测到USB设备插入后,就要对设备进行枚举了.为什么要枚举?枚举就是从设备读取各种描述符信息,这样主机就可以根据这些信息来加载合适的驱动程序,从而知道设备是什么样的设备,如何进行通信等. ...

  7. 解决ListView和ScrollView同时使用时滑动的冲突问题

    ScrollView外面包裹了一个ListView,解决其滑动冲突问题: 只需自定义ListView,命名MyListView: public class MyListView extends Lis ...

  8. xmind的第五天笔记

  9. jquery dataTables.min.js API

    demo: http://datatables.net/release-datatables/examples/api/select_single_row.html 选择一行http://datata ...

  10. applicationContext.xml和web.xml的一些配置

    applicationContext.xml <!-- test环境 --> <beans profile="test"> <context:prop ...