最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

DataFrame:通過SparkSql將scala類轉(zhuǎn)為DataFrame的方法

 更新時(shí)間:2019年01月29日 14:33:24   作者:silentwolfyh  
今天小編就為大家分享一篇DataFrame:通過SparkSql將scala類轉(zhuǎn)為DataFrame的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

如下所示:

import java.text.DecimalFormat
import com.alibaba.fastjson.JSON
import com.donews.data.AppConfig
import com.typesafe.config.ConfigFactory
import org.apache.spark.sql.types.{StructField, StructType}
import org.apache.spark.sql.{Row, SaveMode, DataFrame, SQLContext}
import org.apache.spark.{SparkConf, SparkContext}
import org.slf4j.LoggerFactory
 
/**
 * Created by silentwolf on 2016/6/3.
 */
 
case class UserTag(SUUID: String,
     MAN: Float,
     WOMAN: Float,
     AGE10_19: Float,
     AGE20_29: Float,
     AGE30_39: Float,
     AGE40_49: Float,
     AGE50_59: Float,
     GAME: Float,
     MOVIE: Float,
     MUSIC: Float,
     ART: Float,
     POLITICS_NEWS: Float,
     FINANCIAL: Float,
     EDUCATION_TRAINING: Float,
     HEALTH_CARE: Float,
     TRAVEL: Float,
     AUTOMOBILE: Float,
     HOUSE_PROPERTY: Float,
     CLOTHING_ACCESSORIES: Float,
     BEAUTY: Float,
     IT: Float,
     BABY_PRODUCT: Float,
     FOOD_SERVICE: Float,
     HOME_FURNISHING: Float,
     SPORTS: Float,
     OUTDOOR_ACTIVITIES: Float,
     MEDICINE: Float
     )
 
object UserTagTable {
 
 val LOG = LoggerFactory.getLogger(UserOverviewFirst.getClass)
 
 val REP_HOME = s"${AppConfig.HDFS_MASTER}/${AppConfig.HDFS_REP}"
 
 def main(args: Array[String]) {
 
 var startTime = System.currentTimeMillis()
 
 val conf: com.typesafe.config.Config = ConfigFactory.load()
 
 val sc = new SparkContext()
 
 val sqlContext = new SQLContext(sc)
 
 var df1: DataFrame = null
 
 if (args.length == 0) {
  println("請(qǐng)輸入: appkey , StartTime : 2016-04-10 ,StartEnd :2016-04-11")
 }
 else {
 
  var appkey = args(0)
 
  var lastdate = args(1)
 
  df1 = loadDataFrame(sqlContext, appkey, "2016-04-10", lastdate)
 
  df1.registerTempTable("suuidTable")
 
  sqlContext.udf.register("taginfo", (a: String) => userTagInfo(a))
  sqlContext.udf.register("intToString", (b: Long) => intToString(b))
  import sqlContext.implicits._
 
  //***重點(diǎn)***:將臨時(shí)表中的suuid和自定函數(shù)中Json數(shù)據(jù),放入U(xiǎn)serTag中。
 sqlContext.sql(" select distinct(suuid) AS suuid,taginfo(suuid) from suuidTable group by suuid").map { case Row(suuid: String, taginfo: String) =>
  val taginfoObj = JSON.parseObject(taginfo)
  UserTag(suuid.toString,
   taginfoObj.getFloat("man"),
   taginfoObj.getFloat("woman"),
   taginfoObj.getFloat("age10_19"),
   taginfoObj.getFloat("age20_29"),
   taginfoObj.getFloat("age30_39"),
   taginfoObj.getFloat("age40_49"),
   taginfoObj.getFloat("age50_59"),
   taginfoObj.getFloat("game"),
   taginfoObj.getFloat("movie"),
   taginfoObj.getFloat("music"),
   taginfoObj.getFloat("art"),
   taginfoObj.getFloat("politics_news"),
   taginfoObj.getFloat("financial"),
   taginfoObj.getFloat("education_training"),
   taginfoObj.getFloat("health_care"),
   taginfoObj.getFloat("travel"),
   taginfoObj.getFloat("automobile"),
   taginfoObj.getFloat("house_property"),
   taginfoObj.getFloat("clothing_accessories"),
   taginfoObj.getFloat("beauty"),
   taginfoObj.getFloat("IT"),
   taginfoObj.getFloat("baby_Product"),
   taginfoObj.getFloat("food_service"),
   taginfoObj.getFloat("home_furnishing"),
   taginfoObj.getFloat("sports"),
   taginfoObj.getFloat("outdoor_activities"),
   taginfoObj.getFloat("medicine")
  )}.toDF().registerTempTable("resultTable")
 
  val resultDF = sqlContext.sql(s"select '$appkey' AS APPKEY, '$lastdate' AS DATE,SUUID ,MAN,WOMAN,AGE10_19,AGE20_29,AGE30_39 ," +
  "AGE40_49 ,AGE50_59,GAME,MOVIE,MUSIC,ART,POLITICS_NEWS,FINANCIAL,EDUCATION_TRAINING,HEALTH_CARE,TRAVEL,AUTOMOBILE," +
  "HOUSE_PROPERTY,CLOTHING_ACCESSORIES,BEAUTY,IT,BABY_PRODUCT ,FOOD_SERVICE ,HOME_FURNISHING ,SPORTS ,OUTDOOR_ACTIVITIES ," +
  "MEDICINE from resultTable WHERE SUUID IS NOT NULL")
  resultDF.write.mode(SaveMode.Overwrite).options(
  Map("table" -> "USER_TAGS", "zkUrl" -> conf.getString("Hbase.url"))
  ).format("org.apache.phoenix.spark").save()
 
 }
 }
 
 def intToString(suuid: Long): String = {
 suuid.toString()
 }
 
 def userTagInfo(num1: String): String = {
 
 var de = new DecimalFormat("0.00")
 var mannum = de.format(math.random).toFloat
 var man = mannum
 var woman = de.format(1 - mannum).toFloat
 
 var age10_19num = de.format(math.random * 0.2).toFloat
 var age20_29num = de.format(math.random * 0.2).toFloat
 var age30_39num = de.format(math.random * 0.2).toFloat
 var age40_49num = de.format(math.random * 0.2).toFloat
 
 var age10_19 = age10_19num
 var age20_29 = age20_29num
 var age30_39 = age30_39num
 var age40_49 = age40_49num
 var age50_59 = de.format(1 - age10_19num - age20_29num - age30_39num - age40_49num).toFloat
 
 var game = de.format(math.random * 1).toFloat
 var movie = de.format(math.random * 1).toFloat
 var music = de.format(math.random * 1).toFloat
 var art = de.format(math.random * 1).toFloat
 var politics_news = de.format(math.random * 1).toFloat
 
 var financial = de.format(math.random * 1).toFloat
 var education_training = de.format(math.random * 1).toFloat
 var health_care = de.format(math.random * 1).toFloat
 var travel = de.format(math.random * 1).toFloat
 var automobile = de.format(math.random * 1).toFloat
 
 var house_property = de.format(math.random * 1).toFloat
 var clothing_accessories = de.format(math.random * 1).toFloat
 var beauty = de.format(math.random * 1).toFloat
 var IT = de.format(math.random * 1).toFloat
 var baby_Product = de.format(math.random * 1).toFloat
 
 var food_service = de.format(math.random * 1).toFloat
 var home_furnishing = de.format(math.random * 1).toFloat
 var sports = de.format(math.random * 1).toFloat
 var outdoor_activities = de.format(math.random * 1).toFloat
 var medicine = de.format(math.random * 1).toFloat
 
 "{" + "\"man\"" + ":" + man + "," + "\"woman\"" + ":" + woman + "," + "\"age10_19\"" + ":" + age10_19 + "," + "\"age20_29\"" + ":" + age20_29 + "," +
  "\"age30_39\"" + ":" + age30_39 + "," + "\"age40_49\"" + ":" + age40_49 + "," + "\"age50_59\"" + ":" + age50_59 + "," + "\"game\"" + ":" + game + "," +
  "\"movie\"" + ":" + movie + "," + "\"music\"" + ":" + music + "," + "\"art\"" + ":" + art + "," + "\"politics_news\"" + ":" + politics_news + "," +
  "\"financial\"" + ":" + financial + "," + "\"education_training\"" + ":" + education_training + "," + "\"health_care\"" + ":" + health_care + "," +
  "\"travel\"" + ":" + travel + "," + "\"automobile\"" + ":" + automobile + "," + "\"house_property\"" + ":" + house_property + "," + "\"clothing_accessories\"" + ":" + clothing_accessories + "," +
  "\"beauty\"" + ":" + beauty + "," + "\"IT\"" + ":" + IT + "," + "\"baby_Product\"" + ":" + baby_Product + "," + "\"food_service\"" + ":" + food_service + "," +
  "\"home_furnishing\"" + ":" + home_furnishing + "," + "\"sports\"" + ":" + sports + "," + "\"outdoor_activities\"" + ":" + outdoor_activities + "," + "\"medicine\"" + ":" + medicine +
  "}";
 
 }
 
 def loadDataFrame(ctx: SQLContext, appkey: String, startDay: String, endDay: String): DataFrame = {
 val path = s"$REP_HOME/appstatistic"
 ctx.read.parquet(path)
  .filter(s"timestamp is not null and appkey='$appkey' and day>='$startDay' and day<='$endDay'")
 }
 
 
}

以上這篇DataFrame:通過SparkSql將scala類轉(zhuǎn)為DataFrame的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python獲取當(dāng)前函數(shù)名稱方法實(shí)例分享

    Python獲取當(dāng)前函數(shù)名稱方法實(shí)例分享

    這篇文章主要介紹了Python獲取當(dāng)前函數(shù)名稱方法實(shí)例分享,具有一定借鑒價(jià)值
    2018-01-01
  • Python pygame繪制文字制作滾動(dòng)文字過程解析

    Python pygame繪制文字制作滾動(dòng)文字過程解析

    這篇文章主要介紹了Python pygame繪制文字制作滾動(dòng)文字過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • Python中encode和encoding的區(qū)別小結(jié)

    Python中encode和encoding的區(qū)別小結(jié)

    Python是一種非常流行的高級(jí)編程語言,它提供了許多內(nèi)置函數(shù)和庫來方便地處理文本數(shù)據(jù),其中,encode和encoding是處理文本編碼的重要概念,本文就來介紹一下Python中encode和encoding的區(qū)別小結(jié),感興趣的可以了解一下
    2023-11-11
  • python3如何獲取子線程中函數(shù)返回值

    python3如何獲取子線程中函數(shù)返回值

    這篇文章主要介紹了python3如何獲取子線程中函數(shù)返回值問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • python中私有函數(shù)調(diào)用方法解密

    python中私有函數(shù)調(diào)用方法解密

    這篇文章主要介紹了python中私有函數(shù)調(diào)用方法,較為詳細(xì)的分析了Python私有函數(shù)的原理與調(diào)用技巧,需要的朋友可以參考下
    2016-04-04
  • Scrapy爬蟲Response子類在應(yīng)用中的問題解析

    Scrapy爬蟲Response子類在應(yīng)用中的問題解析

    這篇文章主要為大家介紹了Scrapy爬蟲Response它的子類(TextResponse、HtmlResponse、XmlResponse)在應(yīng)用問題解析
    2023-05-05
  • 關(guān)于python環(huán)境變量如何配置

    關(guān)于python環(huán)境變量如何配置

    這篇文章主要介紹了關(guān)于python環(huán)境變量如何配置,當(dāng)我們?cè)谧约弘娔X上同時(shí)安裝了python2.x和python3.x版本的解釋器的時(shí)候,就需要對(duì)環(huán)境變量的配置進(jìn)行一定的修改,需要的朋友可以參考下
    2023-04-04
  • 在Python編程過程中用單元測(cè)試法調(diào)試代碼的介紹

    在Python編程過程中用單元測(cè)試法調(diào)試代碼的介紹

    這篇文章主要介紹了在Python編程過程中用單元測(cè)試法調(diào)試代碼的介紹,包括使用斷言等,有助于debug時(shí)的效率提升,需要的朋友可以參考下
    2015-04-04
  • Python csv文件的讀寫操作實(shí)例詳解

    Python csv文件的讀寫操作實(shí)例詳解

    這篇文章主要介紹了Python csv文件的讀寫操作實(shí)例詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • python中torch.load中的map_location參數(shù)使用

    python中torch.load中的map_location參數(shù)使用

    在PyTorch中,torch.load()函數(shù)是用于加載保存模型或張量數(shù)據(jù)的重要工具,map_location參數(shù)為我們提供了極大的靈活性,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-03-03

最新評(píng)論

临洮县| 辉南县| 宜黄县| 金平| 黎城县| 宁都县| 绥滨县| 周口市| 孟州市| 泰安市| 正阳县| 东乌珠穆沁旗| 同德县| 和田县| 喜德县| 怀集县| 遂溪县| 乐陵市| 奈曼旗| 平凉市| 清远市| 金寨县| 通河县| 中西区| 广南县| 分宜县| 沿河| 武汉市| 肃北| 滁州市| 木里| 舒兰市| 大姚县| 丰城市| 吴桥县| 石城县| 喜德县| 盐池县| 河间市| 荣昌县| 文山县|