KafkaSparkDemoMain

package kafka

import java.sql.DriverManager
import java.util.Properties

import DAO.{ScalaHbase, ScalaConn}
import kafka.kafkaToHbase.market_analysis
import kafka.serializer.StringDecoder
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.hbase.TableName
import org.apache.hadoop.hbase.client.{Table, ConnectionFactory, Connection}
import org.apache.kafka.clients.producer.{KafkaProducer, ProducerRecord}
import org.apache.spark.SparkConf
import org.apache.spark.streaming.dstream.{DStream, InputDStream}
import org.apache.spark.streaming.{Seconds, Minutes, Duration, StreamingContext}
import org.apache.spark.streaming.kafka.KafkaUtils

import org.apache.spark.SparkContext
import org.apache.spark.SparkContext._
import org.apache.spark.streaming.{Seconds, StreamingContext}
import org.apache.spark.streaming.kafka.KafkaUtils
/**
 * author 谭志坚
 * date   2007-03-16
 */
object KafkaSparkDemoMain {
  case class SP2PLOG(YMD: String, MOBILE: String, CHANNEL: String, REMARK: String, STATUS: String)
  def main(args: Array[String]): Unit = {

    val conf = new SparkConf().setAppName("kafkaDataTest").setMaster("local[2]")
    val sc = new SparkContext(conf)
    val ssc = new StreamingContext(sc, Seconds(10))
    ssc.checkpoint("D:\\BigData\\spark-warehouse")
    val topics = Set("SP2PLOG")
    val brokers = "192.168.100.110:9092,192.168.100.111:9092,192.168.100.112:9092"
    val kafkaParams = Map[String, String]("metadata.broker.list" -> brokers, "serializer.class" -> "kafka.serializer.StringEncoder")

    val kafkaStream = KafkaUtils.createDirectStream[String, String, StringDecoder, StringDecoder](ssc, kafkaParams, topics)



//    val lines = kafkaStream.map(x => x._2)
    val events = kafkaStream.flatMap(line => {
      Some(line._2.toString())
    })
    try {
//      println("events========"+events.toString)
//      println("events========"+events.map(_.split(",").toString))
      val tmpdf = events.map(_.split(",")).map(x => SP2PLOG(x(0), x(1), x(2), x(3), x(4)))
      tmpdf.foreachRDD { rdd =>
        val spark =  ScalaConn.spark
        import spark.implicits._
        val dataFrame = rdd.toDF()
        dataFrame.createOrReplaceTempView("SP2PLOG")
        val bak_sql = spark.sql("select YMD,MOBILE,CHANNEL,REMARK,STATUS from SP2PLOG")

        if (bak_sql.collect().length > 0) {
          try {
            bak_sql.collect().foreach { userRow => {
              val YMD: String = String.valueOf(userRow.getString(0))
              val MOBILE: String = String.valueOf(userRow.getString(1))
              val CHANNEL: String = String.valueOf(userRow.getString(2))
              val REMARK: String = String.valueOf(userRow.getString(3))
              val STATUS: String = String.valueOf(userRow.getString(4))
              println("MOBILE ==" + MOBILE + "===========REMARK=" + REMARK + "  STATUS=" + STATUS)

            }
            }
          } catch {
            case e: Exception => {
              println("完成数据保存到Hbase后:"+e.toString)
              e.printStackTrace
            }
          }

        }
      }
    }catch {
      case e: Exception =>
    }
    //                   将一行一行数据映射成对象,RT_SMS 获取申请手机号码注册,重置密码,绑定汇付
//    try {
//      //     将一行一行数据映射成对象,RT_SMS 获取申请手机号码注册,重置密码,绑定汇付
//      lines.filter(x => (!x.isEmpty
//        )).map(x => {
//        SP2PLOG_parse(x)
//      }).foreachRDD { rdd =>
//        val spark = ScalaConn.spark
//        import spark.implicits._
//        val dataFrame = rdd.toDF()
//        dataFrame.createOrReplaceTempView("RT_REG")
//        val RT_REG_sql = spark.sql("select YMD, MOBILE, CHANNEL,REMARK,STATUS from RT_REG")
//        println("RT_REG_sql.collect().length = " + RT_REG_sql.collect().length)
//        if (RT_REG_sql.collect().length > 0) {
//          try {
//            RT_REG_sql.collect().foreach { userRow => {
//              val YMD: String = String.valueOf(userRow.getString(0))
//              val MOBILE: String = String.valueOf(userRow.getString(1))
//              val CHANNEL: String = String.valueOf(userRow.getString(2))
//              val REMARK: String = String.valueOf(userRow.getString(3))
//              val STATUS: String = String.valueOf(userRow.getString(4))
//              //              println("MOBILE ==" + MOBILE + "===========REMARK=" + REMARK + "  STATUS=" + STATUS)
//              if (REMARK.contains("手机号注册") || STATUS.contains("注册成功")) {
//               println("再次获取展现: ================REMARK======"+REMARK+"+STATUS==========="+STATUS)
//              }
//              if (!YMD.isEmpty && !REMARK.isEmpty) {
//                println("再次获取展现: YMD========="+YMD+" =========================REMARK======"+REMARK)
//              }
//            }
//            }
//          } catch {
//            case e: Exception => {
//              println("完成数据保存到Hbase后:"+e.toString)
//              e.printStackTrace
//            }
//          }
//        }
//      }
//    } catch {
//      case e: Exception => {
//        println("完成数据分析和运行后:"+e.toString)
//        e.printStackTrace
//      }
//    }


    ssc.start()
    ssc.awaitTermination()
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值