spark同时支持sparksql和sparkStreaming场景

package com.zgw.spark.recommander

import java.util.Properties

import com.zgw.spark.utils.ConfigUtil
import org.apache.spark.SparkConf
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.{DataFrame, SaveMode, SparkSession}
import org.apache.spark.streaming.{Seconds, StreamingContext}

/**
  *
  * 根据用户的视频浏览历史,进行相关的推荐
  * http://lxw1234.com/archives/2015/04/136.htm
  *
  * 实时计算结果
  *
  *
  */
object RecommendVideoRt {
  def main(args: Array[String]): Unit = {

    val sparkConf: SparkConf = new SparkConf().setMaster("local[*]").setAppName("RecommendVideoRt").set("spark.testing.memory", "2147480000")
    //sparkSession,不能直接new,私有的对象,具体看源码

    //分析环境对象以及采集周期
    val ssc = new StreamingContext(sparkConf,Seconds(180))


//    ssc.addStreamingListener()
//    ssc.socketStream()
   val aa: RDD[Long] =  ssc.sparkContext.range(0,100,2)

    aa.foreach(println)

    val spark: SparkSession = SparkSession.builder.config(sparkConf).getOrCreate()


    val driver = "com.mysql.jdbc.Driver"
    val url = ConfigUtil.getString("jdbc.url")
    val userName =  ConfigUtil.getString("jdbc.username")
    val passWd =  ConfigUtil.getString("jdbc.password")

    val readConnProperties4 = new Properties()
    readConnProperties4.put("driver", "com.mysql.jdbc.Driver")
    readConnProperties4.put("user", userName)
    readConnProperties4.put("password", passWd)

    val userVideoHistory: DataFrame = spark.read.jdbc(
//    val userVideoHistory: DataFrame = sqlcontext.read.jdbc(
      url
      ,"(select * from user_video_history ) t"
      ,readConnProperties4
    )
//    saveAsFileByPartition(dataFrame, "hdfs://gtdata-test01:8020/testw/history_data","|",SaveMode.Overwrite)

    userVideoHistory.show(100)
    userVideoHistory.cache()
    userVideoHistory.createTempView("history_data")
 }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值