package statistics

import common.util.timeUtil
import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.sql.SQLContext
import org.apache.spark.sql.functions.{col, count, split}

class costMonth {
  def main(args: Array[String]): Unit = {

    val conf = new SparkConf()
    //      .setAppName("finance test")
    //      .setMaster("local")
    val sc = new SparkContext(conf)
    //    sc.setLogLevel("WARN")
    val sqlContext = new SQLContext(sc)

    val df = sqlContext.load(
      "org.apache.phoenix.spark"
      , Map("table" -> "ASSET_NORMAL"
        , "zkUrl" -> "node3,node4,node5:2181")
    )
    df.registerTempTable("asset_normal")
    def costingWithin(originalValue: Float, years: Int): Double =  (originalValue*0.95)/(years*365)
    sqlContext.udf.register("costingWithin", costingWithin _)

    def costingBeyond(originalValue: Float): Double = originalValue*0.05/365
    sqlContext.udf.register("costingBeyond", costingBeyond _)

    def expire(acceptanceDate: String, years: Int): Boolean = timeUtil.dateStrAddYears2TimeStamp(acceptanceDate, timeUtil.SECOND_TIME_FORMAT, years) > System.currentTimeMillis()
    sqlContext.udf.register("expire", expire _)

    def monthSpace(stDate: String, endDate: String): Int = timeUtil.getMonthSpace(stDate, endDate)
    sqlContext.udf.register("monthSpace", monthSpace _)

    val costDay = sqlContext
      .sql(
        "select ID, ASSET_ID, ASSET_NAME, ACCEPTANCE_DATE, FIRST_DEPARTMENT_ID, SECOND_DEPARTMENT_ID, case when expire(ACCEPTANCE_DATE, DEPRECIABLE_LIVES_NAME) then costingWithin(ORIGINAL_VALUE, DEPRECIABLE_LIVES_NAME) else costingBeyond(ORIGINAL_VALUE) end as ACTUAL_COST, current_timestamp() as GENERATION_TIME from asset_normal "
      )

    //    df.show(false)
    costDay.write
      .format("org.apache.phoenix.spark")
      .mode("overwrite")
      .option("table", "ASSET_FINANCIAL_DETAIL")
      .option("zkUrl", "node3,node4,node5:2181")
      .save()

  }
}
ln -s /etc/hbase/conf.cloudera.hbase/hbase-site.xml /etc/spark/conf.cloudera.spark_on_yarn/hbase-site.xml
spark-shell --conf "spark.executor.extraClassPath=/opt/cloudera/parcels/CDH-5.14.2-1.cdh5.14.2.p0.3/lib/spark/lib/cf/phoenix-spark-4.14.0-cdh5.14.2.jar:/opt/cloudera/parcels/CDH-5.14.2-1.cdh5.14.2.p0.3/lib/spark/lib/cf/phoenix-client-4.14.1-HBase-1.4.jar" --conf "spark.driver.extraClassPath=/opt/cloudera/parcels/CDH-5.14.2-1.cdh5.14.2.p0.3/lib/spark/lib/cf/phoenix-spark-4.14.0-cdh5.14.2.jar:/opt/cloudera/parcels/CDH-5.14.2-1.cdh5.14.2.p0.3/lib/spark/lib/cf/phoenix-client-4.14.1-HBase-1.4.jar"
val df = sqlContext.load("org.apache.phoenix.spark",Map("table"->"ASSET_NORMAL","zkUrl"->"node3,node4,node5:2181"))

参考:

https://blog.csdn.net/dingyuanpu/article/details/52623655

https://www.cnblogs.com/feiyudemeng/p/9254046.html

http://dequn.github.io/2016/11/08/phoenix-spark-setting/

https://community.hortonworks.com/questions/212315/spark2-phoenix-plugin-with-zeppelin.html

https://community.hortonworks.com/content/supportkb/150591/how-to-connect-hbase-and-phoenix-tables-in-secure.html

https://community.hortonworks.com/articles/179762/how-to-connect-to-phoenix-tables-using-spark2.html

https://mvnrepository.com/artifact/org.apache.phoenix/phoenix-spark/4.14.0-cdh5.14.2

https://mvnrepository.com/artifact/org.apache.phoenix/phoenix-client/4.14.1-HBase-1.4

https://blogs.apache.org/phoenix/entry/spark_integration_in_apache_phoenix

http://phoenix.apache.org/phoenix_spark.html#

https://www.cnblogs.com/skyEva/p/5859742.html

Spark教程——(6)Spark-shell基于Phoenix访问HBase数据的更多相关文章

  1. 基于Phoenix对HBase建索引

    参考: Phoenix与HBase集成进行数据分析 HBase查询速度慢原因排查 操作1,执行查询,如下: : jdbc:phoenix:node3::/hbase> SELECT * FROM ...

  2. Phoenix映射HBase数据表

    1. 说明 安装好phoenix后对于HBase中已经存在的数据表不会自动进行映射,所以想要再phoenix中操作HBase已有数据表就需要手动进行配置. 2. 创建HBase表 > creat ...

  3. [oBIX包使用教程] 使用 Python 通过 oBIX 协议访问 Niagara 数据

    oBIX 全称是 Open Building Information Exchange,它是基于 RESTful Web Service 的接口的标准,用于构建控制系统.oBIX是在专为楼宇自动化设计 ...

  4. 浅谈Phoenix在HBase中的应用

    一.前言 业务使用HBase已经有一段时间了,期间也反馈了很多问题,其中反馈最多的是HBase是否支持SQL查询和二级索引,由于HBase在这两块上目前暂不支持,导致业务在使用时无法更好的利用现有的经 ...

  5. Pyspark访问Hbase

    作者:Syn良子 出处:http://www.cnblogs.com/cssdongl/p/7347167.html 转载请注明出处 记录自己最近抽空折腾虚拟机环境时用spark2.0的pyspark ...

  6. spark教程(二)-shell操作

    spark 支持 shell 操作 shell 主要用于调试,所以简单介绍用法即可 支持多种语言的 shell 包括 scala shell.python shell.R shell.SQL shel ...

  7. Spark教程——(10)Spark SQL读取Phoenix数据本地执行计算

    添加配置文件 phoenixConnectMode.scala : package statistics.benefits import org.apache.hadoop.conf.Configur ...

  8. spark教程(一)-集群搭建

    spark 简介 建议先阅读我的博客 大数据基础架构 spark 一个通用的计算引擎,专门为大规模数据处理而设计,与 mapreduce 类似,不同的是,mapreduce 把中间结果 写入 hdfs ...

  9. 大数据(3):基于sogou.500w.utf8数据Hbase和Spark实践

    1. HBase安装部署操作 a) 解压HBase安装包tar –zxvf hbase-0.98.0-hadoop2-bin.tar.gzb) 修改环境变量 hbase-env.shexport JA ...

随机推荐

  1. idea新建maven project工程

    1.new project: 2.新建在main 下新建 java directory 并mark as  source root,这里我已经makr过java目录所以以showfor做演示: 3.新 ...

  2. lsof 查看打开了一个文件的有哪些进程 统计那个进程打开的文件最多

    lsof | grep /lib64/libc-2.12.so | wc   == 查看打开了一个文件的有哪些进程 lsof | awk '{print $2,$1}' | sort | uniq - ...

  3. Wireshark 查看指定进程的网络包

    Wireshark 查看指定进程的网络包 打开任务管理器,右键筛选列,选中PID(进程标识符): 找到该进程对应的PID,如1200: 在cmd中执行netstat -ano|findstr 1200 ...

  4. POJ-1087 A Plug for UNIX (网络流)

    思路 电器数1 ~ 100,附带100种接口,注意题目:You notice that some of the devices use plugs for which there is no rece ...

  5. html和css的重难点知识

    目录 html总难点总结: 1. 块级标签与内联标签的区别 1.1 块级标签: 1.2 内联标签: 2. 选择器 2.1 定义 2.2 选择器的分类 2.1 选择器的分类 3. css中margin, ...

  6. JQuery选择器&过滤器

    JQuery对象: JQuery对象的本质上是DOM数组,它对DOM元素进行了封装 JQuery对象和JavaScript对象可以互转(\$()/$obj()[i]),但是JQuery对象和Javas ...

  7. vs2017 vs2019配置sqlite3连接引擎(驱动)指南(二)vs2019续集

    在写完上一篇博客后,一觉醒来,又又又又不行了,介绍一个终极大招,如果你的fuck vs又提示无法打开sqlite3.h的问题 环境win10  vs2019 debug x86 实在没心情写文字了,直 ...

  8. POJ3662 Telephone Lines (dijkstra+二分)

    Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncoop ...

  9. k sum 问题系列

    转自:http://tech-wonderland.net/blog/summary-of-ksum-problems.html (中文旧版)前言: 做过leetcode的人都知道, 里面有2sum, ...

  10. 与英特尔分道扬镳,苹果的5G业务掉队了吗?

    5G概念已经大热,越来越多的厂商推出相关产品,中国骄傲之华为不仅在5G通信标准制定方面参与感非常强,也先于竞争对手推出5G智能终端,连同三星/Vivo等也纷纷推出5G终端,而作为智能手机市场绝对的利润 ...