sparkStreaming统计各平台最近一分钟实时注册收入 时间段,平台,金额,订单数
样例数据:
__clientip=10.10.9.153&paymentstatus=0&__opip=&memberid=89385239&iamount=1&itype=16&oper_res=1&channeltype=8&__timestamp=1457252427&productid=112&selectbank=&icount=0&ordersrc=web&paymentip=61.159.104.134&orderdate=2016-03-06 16:19:55&subjecttype=zheanaiMessenger&oper_type=1&paydate=&orderamount=259.0&paymentchannel=16&oper_time=2016-03-06 16:20:27&orderid=127145727&iunit=month&bussinessid=80125727&isuse=0
__clientip=10.10.9.175&paymentstatus=0&__opip=&memberid=89378034&iamount=12&itype=17&oper_res=1&channeltype=75&__timestamp=1457252429&productid=124&selectbank=&icount=0&ordersrc=100&paymentip=59.37.137.119&orderdate=2016-03-06 16:20:29&subjecttype=zheanaiMessenger&oper_type=0&paydate=&orderamount=388.0&paymentchannel=1028&oper_time=2016-03-06 16:20:29&orderid=127145736&iunit=month&bussinessid=8012580&isuse=0
__clientip=10.10.9.153&paymentstatus=0&__opip=&memberid=75372899&iamount=12&itype=16&oper_res=1&channeltype=&__timestamp=1457252286&productid=131&selectbank=&icount=0&ordersrc=web&paymentip=113.226.244.206&orderdate=2016-03-06 16:18:06&subjecttype=zheanaiMessenger&oper_type=0&paydate=&orderamount=99.0&paymentchannel=307&oper_time=2016-03-06 16:18:06&orderid=127145700&iunit=month&bussinessid=80125477&isuse=0
__clientip=10.10.9.175&paymentstatus=0&__opip=&memberid=87634711&iamount=1&itype=16&oper_res=1&channeltype=8&__timestamp=1457252432&productid=129&selectbank=&icount=0&ordersrc=web&paymentip=114.246.35.251&orderdate=2016-03-06 16:19:05&subjecttype=zheanaiMessenger&oper_type=1&paydate=&orderamount=19.0&paymentchannel=16&oper_time=2016-03-06 16:20:32&orderid=127145713&iunit=month&bussinessid=66213022&isuse=0
__clientip=10.10.9.153&paymentstatus=0&__opip=&memberid=89172717&iamount=12&itype=17&oper_res=1&channeltype=77&__timestamp=1457252371&productid=124&selectbank=&icount=0&ordersrc=4&paymentip=111.126.43.83&orderdate=2016-03-06 16:19:31&subjecttype=zheanaiMessenger&oper_type=0&paydate=&orderamount=388.0&paymentchannel=1116&oper_time=2016-03-06 16:19:31&orderid=127145723&iunit=month&bussinessid=8012568&isuse=0
spark处理过程如下:
1.读取,ssc自带的receiver,解析(valueSplit方法 处理成kv格式)
2.过滤filterRegex,类似sql中的where条件放弃一些不需要的数据,比如只需要买单的数据而不要下单数据
3.转换,getPlatform、getFormatDate,类似case when
4.创建了一个class命名为result,重写了toString方法。该class存放从kafka中处理后的所有需要的数据字段。
5.写入MySQL,insertIntoMySQL,方法在每个partition中调用
另外代码中使用了getOrCreate以便恢复,利用了计数器简单统计了一下有效记录数
代码如下:
package com.homed.stream
/**
* Created by hadoop on 2016/12/17.
*
*/
import java.sql.Connection
import java.text.SimpleDateFormat
import java.util.Date
import org.apache.log4j.PropertyConfigurator
import org.apache.spark.rdd.RDD
import org.apache.spark.streaming.kafka.KafkaUtils
import org.apache.spark.streaming.{Seconds, StreamingContext, Time}
import org.apache.spark.{SparkConf, SparkContext}
import org.joda.time.DateTime
import org.slf4j.LoggerFactory
import scala.collection.mutable.Map
object KafkaStreaming {
val logger = LoggerFactory.getLogger(this.getClass)
PropertyConfigurator.configure(System.getProperty("user.dir")+"\\src\\log4j.properties")
case class result(ftime:String,hour:String,orderid:Long,memberid:Long,platform:String,iamount:Double,orderamount:Double)extends Serializable{
override def toString: String="%s\t%s\t%d\t%d\t%s\t%.2f\t%.2f".format(ftime, hour,orderid,memberid,platform,iamount,orderamount)
}
def getFormatDate(date:Date,format:SimpleDateFormat): String ={
format.format(date)
}
def stringFormatTime(time:String,simpleformat:SimpleDateFormat): Date ={
simpleformat.parse(time)
}
// kafka中的value解析为Map
def valueSplit(value:String): Map[String,String] ={
val x = value.split("&")
val valueMap:Map[String,String] = Map()
x.foreach { kvs =>
if (!kvs.startsWith("__")){
val kv = kvs.split("=")
if (kv.length==2) {
valueMap += (kv(0) -> kv(1))
}
}
}
valueMap
}
// 实现类似where的条件,tips:优先过滤条件大的减少后续操作
def filterRegex(map:Map[String,String]): Boolean ={
//过滤操作类型,控制为支付操作
val oper_type = map.getOrElse("oper_type","-1")
if(!oper_type.equals("2") && !oper_type.equals("3"))
return false
// 过滤未支付成功记录
if(!map.getOrElse("paymentstatus","0").equals("1"))
return false
// 过滤无效支付ip
val paymentip = map.getOrElse("paymentip",null)
if (paymentip.startsWith("10.10")||paymentip.startsWith("183.62.134")||paymentip.contains("127.0.0.1"))
return false
return true
}
// 实现类似 case when的方法,上报的p字段不一定为数值
def getPlatform(p:String,x:Int): String ={
val platformname = (p,x) match{
case (p,x) if(Array[String]("1","2","3").contains(p)) => "wap"
case (p,x) if(Array[String]("4","8").contains(p)&& x!=18) =>"andriod"
case (p,x) if((Array[String]("5","7","51","100").contains(p))&&(p!=18)) => "ios"
case _ => "pc"
}
platformname
}
// 数据库写入
def insertIntoMySQL(con:Connection,sql:String,data:result): Unit ={
// println(data.toString)
try {
val ps = con.prepareStatement(sql)
ps.setString(1, data.ftime)
ps.setString(2, data.hour)
ps.setLong(3,data.orderid)
ps.setLong(4, data.memberid)
ps.setString(5, data.platform)
ps.setDouble(6, data.iamount)
ps.setDouble(7, data.orderamount)
ps.executeUpdate()
ps.close()
}catch{
case exception:Exception=>
logger.error("Error in execution of query "+exception.getMessage+"\n-----------------------\n"+exception.printStackTrace()+"\n-----------------------------")
}
}
def createContext(zkqurm:String,topic:scala.Predef.Map[String,Int],checkPointDir:String): StreamingContext ={
val simpleformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
val dateFormat = new SimpleDateFormat("yyyyMMdd")
val timeFormat = new SimpleDateFormat("HH:mm")
val sql ="insert into t_ssc_toufang_result_mi(ftime,hour,orderid,memberid,platform,iamount,orderamount) values(?,?,?,?,?,?,?);"
val conf = new SparkConf()
conf.setAppName("Scala Streaming read kafka")
// VM option -Dspark.master=local
// conf.setMaster("local[4]")
val sc = new SparkContext(conf)
val totalcounts = sc.accumulator(0L,"Total count")
val ssc = new StreamingContext(sc,Seconds(60))
//ssc.checkpoint(checkPointDir)
//统计各平台最近一分钟实时注册收入 时间段,平台,金额,订单数
val lines = KafkaUtils.createStream(ssc, zkqurm, "mytopic_local",topic).map(_._2)
val filterRecord = lines.filter(x => !x.isEmpty).map(valueSplit).filter(filterRegex).map{x =>
val orderdate = stringFormatTime(x.getOrElse("orderdate",null),simpleformat)
val day = getFormatDate(orderdate,dateFormat)
val hour = getFormatDate(orderdate,timeFormat)
var orderamount = x.getOrElse("orderamount","0").toDouble
if (x.getOrElse("oper_type",-1)==3)
orderamount = -1*orderamount
val res = new result(
day
,hour
,x.getOrElse("orderid",null).toLong
,x.getOrElse("memberid",null).toLong
,getPlatform(x.getOrElse("ordersrc",null),x.getOrElse("itype",null).toInt)
,x.getOrElse("iamount","0").toDouble
,orderamount
)
res
}
filterRecord.foreachRDD((x: RDD[result],time: Time) =>{
if(!x.isEmpty()) {
// 打印一下这一批batch的处理时间段以及累计的有效记录数(不含档次)
println("--"+new DateTime(time.milliseconds).toString("yyyy-MM-dd HH:mm:ss")+"--totalcounts:"+totalcounts.value+"-----")
x.foreachPartition{res =>
{
if(!res.isEmpty){
val connection = ConnectionPool.getConnection.getOrElse(null)
res.foreach {
r: result =>totalcounts.add(1L)
insertIntoMySQL(connection, sql, r)
}
ConnectionPool.closeConnection(connection)
}
}
}
}
})
ssc
}
// 主函数入口=================================================================
def main(args:Array[String]): Unit ={
val zkqurm = "10.10.10.177:2181,10.10.10.175:2181,10.10.10.179:2181"
val topic = scala.Predef.Map("t_fw_00015"->30)
val checkPointDir ="/user/root/sparkcheck"
val ssc = StreamingContext.getOrCreate(checkPointDir,
() => {
createContext(zkqurm, topic,checkPointDir)
})
ssc.start()
ssc.awaitTermination()
}
}
连接池部分,代码如下:
package com.homed.stream
/**
* Created by hadoop on 2016/12/17.
*/
import java.sql.Connection
import com.jolbox.bonecp.{BoneCP, BoneCPConfig}
import org.slf4j.LoggerFactory
object ConnectionPool {
val logger = LoggerFactory.getLogger(this.getClass)
private val connectionPool = {
try{
Class.forName("com.mysql.jdbc.Driver")
val config = new BoneCPConfig()
config.setJdbcUrl("jdbc:mysql://localhost:3306/test")
config.setUsername("etl")
config.setPassword("xxxxx")
config.setLazyInit(true)
config.setMinConnectionsPerPartition(3)
config.setMaxConnectionsPerPartition(5)
config.setPartitionCount(5)
config.setCloseConnectionWatch(true)
config.setLogStatementsEnabled(false)
Some(new BoneCP(config))
} catch {
case exception:Exception=>
logger.warn("Error in creation of connection pool"+exception.printStackTrace())
None
}
}
def getConnection:Option[Connection] ={
connectionPool match {
case Some(connPool) => Some(connPool.getConnection)
case None => None
}
}
def closeConnection(connection:Connection): Unit = {
if(!connection.isClosed) {
connection.close()
}
}
}
sparkStreaming统计各平台最近一分钟实时注册收入 时间段,平台,金额,订单数的更多相关文章
- Reaction 开源可自定义实时的电商平台
Reaction 开源可自定义实时的电商平台,支持以下特性 拖放商品 订单处理 支付 物流 税 折扣 Analytics(分析) 与许多第三方应用程序集成 graphql 开发api 简单demo 使 ...
- 实时竞价RTB广告平台_传漾科技_中国领先的智能数字营销引擎
实时竞价RTB广告平台_传漾科技_中国领先的智能数字营销引擎 Programmatic Framework™ 传漾程序化购买框架
- 第五章 大数据平台与技术 第12讲 大数据处理平台Spark
Spark支持多种的编程语言 对比scala和Java编程上节课的计数程序.相比之下,scala简洁明了. Hadoop的IO开销大导致了延迟高,也就是说任务和任务之间涉及到I/O操作.前一个任务完成 ...
- GPS部标监控平台的架构设计(八)-基于WCF的平台数据通信设计
总体来讲,GPS部标平台的软件开发是一个对网络通信和应用程序之间通信的技术应用密集型的开发工作,也是有一定设计技术含量的工作. 1.设计通信接口 在设计的时候,根据职责划分,拆分成不同的应用子系统,对 ...
- windows平台下基于QT和OpenCV搭建图像处理平台
在之前的博客中,已经分别比较详细地阐述了"windows平台下基于VS和OpenCV"以及"Linux平台下基于QT和OpenCV"搭建图像处理框架,并 ...
- 大数据平台Hive数据迁移至阿里云ODPS平台流程与问题记录
一.背景介绍 最近几天,接到公司的一个将当前大数据平台数据全部迁移到阿里云ODPS平台上的任务.而申请的这个ODPS平台是属于政务内网的,因考虑到安全问题当前的大数据平台与阿里云ODPS的网络是不通的 ...
- 微信公众平台测试帐号的注册与使用(自己的服务器<---->微信后台<---->测式公众号)
打开注册的网址:http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login 用手机微信扫描网页左边的二维码,然后在手机上确认即可: 至此 ...
- win7 64位平台编译的程序在XP 32位平台无法运行的解决方法
win7 64位平台编译的程序在XP 32位平台无法运行的解决方法 vs2010的开发环境,制作了一个DLL库.但DLL在XP 32位平台一直无法使用.解决方法如下: 右键项目,属性->配置属性 ...
- 淘宝平台进行数据的实时传输: TimeTunnel介绍
在班级工作中遇到似业务场景中的实时流传输数据的访问,所以,淘宝实时数据仓库这个人做了一些研究和了解. 本文介绍的业务场景和淘宝的设计TimeTunnel工具,从淘宝数据仓库团队沟通过程中的图像文字si ...
随机推荐
- iOS - The identity used to sign the executable is no longer valid
①.首先在xcode中的Build Settings中看有没有设置: ②.账号是不是多个人在用,个人开发者的账号只能绑定一台电脑,当另外一台电脑绑定了话,你的电脑就失效了.你确认下是不是这个原因造成的 ...
- 【工作中学习1】两个设计模式:Singleton(单例)和 Adapter(适配器)
好久没有写自己的学习小博客,罪过罪过..最近本菜鸟在项目中接触到经常用到的设计模式,首先是Singleton(单例),这个相信大家都会用到很多,所以自己用代码实现一下,有助于自己学习理解,如有不对,请 ...
- adobe air ane 中有的java class 打包 apk 后却没有了报NoClassDefFoundError ,ClassNotFoundException
apache flex sdk 手机项目 09-18 10:34:55.030: E/AndroidRuntime(19513): FATAL EXCEPTION: main 09-18 10:34: ...
- tomcat下部署项目的流程和遇到的问题笔记
简单部署流程: 1,解析域名关联到服务器ip 2,配置服务器jre运行环境 3,安装tomcat 4,项目打war包,放入tomcat根目录下webapps(tomcat默认加载的项目目录)目录下 5 ...
- 如何在js中获取到服务器端控件并给其赋值
如下所示:lbID为服务器端控件ID document.getElementById('<%=lbID.ClientID%>').value = "赋值";
- JavaScript 如何编写计算器
1.JavaScript制作计算器 <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...
- VM(xp系统下用虚拟机安装win8 提示 :units specified don't exist, SHSUCDX can't install)解决方法
改成IDE的模式
- 利用Excel导入数据到SAP C4C
假设要导入一个Account数据到C4C系统. 工作中心Data Workbench,工作中心视图Import,点download metadata: 会下载一个压缩包到本地. 进入文件夹Templa ...
- 2019.03.15 ZJOI2019模拟赛 解题报告
得分: \(20+45+15=80\)(三题暴力全写挂...) \(T1\):Lyk Love painting 首先,不难想到二分答案然后\(DP\)验证. 设当前需验证的答案为\(x\),则一个暴 ...
- IOError: [Errno 22] invalid mode ('rb') or filename: 'C
dataset = scipy.io.loadmat('F:\test_data.mat') 报错 IOError: [Errno ] invalid mode ('rb') or filename: ...