样例数据:

__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统计各平台最近一分钟实时注册收入 时间段,平台,金额,订单数的更多相关文章

  1. Reaction 开源可自定义实时的电商平台

    Reaction 开源可自定义实时的电商平台,支持以下特性 拖放商品 订单处理 支付 物流 税 折扣 Analytics(分析) 与许多第三方应用程序集成 graphql 开发api 简单demo 使 ...

  2. 实时竞价RTB广告平台_传漾科技_中国领先的智能数字营销引擎

    实时竞价RTB广告平台_传漾科技_中国领先的智能数字营销引擎 Programmatic Framework™ 传漾程序化购买框架

  3. 第五章 大数据平台与技术 第12讲 大数据处理平台Spark

    Spark支持多种的编程语言 对比scala和Java编程上节课的计数程序.相比之下,scala简洁明了. Hadoop的IO开销大导致了延迟高,也就是说任务和任务之间涉及到I/O操作.前一个任务完成 ...

  4. GPS部标监控平台的架构设计(八)-基于WCF的平台数据通信设计

    总体来讲,GPS部标平台的软件开发是一个对网络通信和应用程序之间通信的技术应用密集型的开发工作,也是有一定设计技术含量的工作. 1.设计通信接口 在设计的时候,根据职责划分,拆分成不同的应用子系统,对 ...

  5. windows平台下基于QT和OpenCV搭建图像处理平台

        在之前的博客中,已经分别比较详细地阐述了"windows平台下基于VS和OpenCV"以及"Linux平台下基于QT和OpenCV"搭建图像处理框架,并 ...

  6. 大数据平台Hive数据迁移至阿里云ODPS平台流程与问题记录

    一.背景介绍 最近几天,接到公司的一个将当前大数据平台数据全部迁移到阿里云ODPS平台上的任务.而申请的这个ODPS平台是属于政务内网的,因考虑到安全问题当前的大数据平台与阿里云ODPS的网络是不通的 ...

  7. 微信公众平台测试帐号的注册与使用(自己的服务器<---->微信后台<---->测式公众号)

    打开注册的网址:http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login 用手机微信扫描网页左边的二维码,然后在手机上确认即可: 至此 ...

  8. win7 64位平台编译的程序在XP 32位平台无法运行的解决方法

    win7 64位平台编译的程序在XP 32位平台无法运行的解决方法 vs2010的开发环境,制作了一个DLL库.但DLL在XP 32位平台一直无法使用.解决方法如下: 右键项目,属性->配置属性 ...

  9. 淘宝平台进行数据的实时传输: TimeTunnel介绍

    在班级工作中遇到似业务场景中的实时流传输数据的访问,所以,淘宝实时数据仓库这个人做了一些研究和了解. 本文介绍的业务场景和淘宝的设计TimeTunnel工具,从淘宝数据仓库团队沟通过程中的图像文字si ...

随机推荐

  1. 轻松解决 Eclipse Indigo 3.7 中文字体偏小,完美 Consolas 微软雅黑混合字体!(转)

    在 Windows 7 下初始后化,发现界面变化不大,但中文字体却面目全非,小得根本看不见,而且也看起来很不爽.其实这是 Eclipse 的默认字体换了,以前的一直是 Courier New ,这次e ...

  2. 浅谈SQL Server中的事务日志(二)----事务日志在修改数据时的角色

    简介 每一个SQL Server的数据库都会按照其修改数据(insert,update,delete)的顺序将对应的日志记录到日志文件.SQL Server使用了Write-Ahead logging ...

  3. Java—多态

    多态——对象的多种形态(继承是多态实现的基础) 引用多态:父类的引用可以指向本类的对象:父类的引用可以指向子类的对象 方法多态:创建本类对象时,调用的方法为本类方法:创建子类对象时,调用的方法为子类重 ...

  4. Java字体优化

    需求背景 最近在做的项目显示的字体感觉太丑,于是乎想着DIY改进一下. 查阅资料,总觉得别人写的都不咋地,于是决心写一篇略微完善点的关于项目字体优化方面的文章. 当然,这篇文章不会教你如何使用True ...

  5. nginx配置https服务器

    方法一 1.创建证书 #cd /usr/local/nginx/conf #openssl genrsa -des3 -out server.key 1024 #openssl req -new -k ...

  6. CentOS6.8上安装epel

    实验环境: [root@bogon zhi]# uname -a Linux bogon -.el6.i686 # SMP Fri Nov :: UTC i686 i686 i386 GNU/Linu ...

  7. Android(java)学习笔记71:Tab标签的使用

    1. 案例1---TabProject (1)首先是main.xml文件: <?xml version="1.0" encoding="utf-8"?&g ...

  8. 字符串处理,Poj(2121)

    题目链接:http://poj.org/problem?id=2121 差一点就WA哭了,主要是自己傻逼了. 思路: 遇到hundred,sum*100; 但是遇到thouthend,million, ...

  9. 【转】Android开发学习总结(一)——搭建最新版本的Android开发环境

    最近由于工作中要负责开发一款Android的App,之前都是做JavaWeb的开发,Android开发虽然有所了解,但是一直没有搭建开发环境去学习,Android的更新速度比较快了,Android1. ...

  10. Spring boot 自动配置自定义配置文件

    示例如下: 1.   新建 Maven 项目 properties 2.   pom.xml <project xmlns="http://maven.apache.org/POM/4 ...