spark读取hbase形成RDD,存入hive或者spark_sql分析
object SaprkReadHbase {
var total:Int = 0
def main(args: Array[String]) {
val spark = SparkSession
.builder()
.master("local[2]")
.appName("Spark Read Hbase ")
.enableHiveSupport() //如果要读取hive的表,就必须使用这个
.getOrCreate()
val sc= spark.sparkContext
//zookeeper信息设置,存储着hbase的元信息
val conf = HBaseConfiguration.create()
conf.set("hbase.zookeeper.quorum","hadoop01,hadoop02,hadoop03")
conf.set("hbase.zookeeper.property.clientPort", "")
conf.set(TableInputFormat.INPUT_TABLE, "event_logs_20190218")
//读取数据并转化成rdd
val hBaseRDD: RDD[(ImmutableBytesWritable, Result)] = sc.newAPIHadoopRDD(conf, classOf[TableInputFormat],
classOf[org.apache.hadoop.hbase.io.ImmutableBytesWritable], //定义输入格式
classOf[org.apache.hadoop.hbase.client.Result]) //定义输出
val count = hBaseRDD.count()
println("\n\n\n:" + count)
import spark.implicits._
val logRDD: RDD[EventLog] = hBaseRDD.map{case (_,result) =>{
//获取行键v
val rowKey = Bytes.toString(result.getRow)
val api_v=Bytes.toString(result.getValue(Bytes.toBytes("info"),Bytes.toBytes("api_v")))
val app_id=Bytes.toString(result.getValue(Bytes.toBytes("info"),Bytes.toBytes("app_id")))
val c_time=Bytes.toString(result.getValue(Bytes.toBytes("info"),Bytes.toBytes("c_time")))
val ch_id=Bytes.toString(result.getValue(Bytes.toBytes("info"),Bytes.toBytes("ch_id")))
val city=Bytes.toString(result.getValue(Bytes.toBytes("info"),Bytes.toBytes("city")))
val province=Bytes.toString(result.getValue(Bytes.toBytes("info"),Bytes.toBytes("province")))
val country=Bytes.toString(result.getValue(Bytes.toBytes("info"),Bytes.toBytes("country")))
val en=Bytes.toString(result.getValue(Bytes.toBytes("info"),Bytes.toBytes("en")))
val ip=Bytes.toString(result.getValue(Bytes.toBytes("info"),Bytes.toBytes("ip")))
val net_t=Bytes.toString(result.getValue(Bytes.toBytes("info"),Bytes.toBytes("net_t")))
val pl=Bytes.toString(result.getValue(Bytes.toBytes("info"),Bytes.toBytes("pl")))
val s_time=Bytes.toString(result.getValue(Bytes.toBytes("info"),Bytes.toBytes("s_time")))
val user_id=Bytes.toString(result.getValue(Bytes.toBytes("info"),Bytes.toBytes("user_id")))
val uuid=Bytes.toString(result.getValue(Bytes.toBytes("info"),Bytes.toBytes("uuid")))
val ver=Bytes.toString(result.getValue(Bytes.toBytes("info"),Bytes.toBytes("ver")))
//样例类进行schemal信息构建。元组与样例类的字段值据说不能超过22个,一般structureType构建(row,schemal)
new EventLog(rowKey,api_v,app_id,c_time,ch_id,city,province,country,en,ip,net_t,pl,s_time,user_id,uuid,ver)
}
}
//可以转为dataframe、dataset存入hive作为宽表 或者直接进行sparkcore分析
val logds= logRDD.toDS()
logds.createTempView("event_logs")
val sq= spark.sql("select * from event_logs limit 1")
println(sq.explain())
sq.show()
sc.stop()
spark.stop()
}
}
//write hbase
/**
* @created by imp ON 2018/2/19
*/
class SparkWriteHbase {
def main(args: Array[String]): Unit = {
val sparkConf = new SparkConf().setAppName(this.getClass.getName).setMaster("local")
val sc = new SparkContext(sparkConf)
sc.setLogLevel("ERROR")
val conf = HBaseConfiguration.create()
conf.set("hbase.zookeeper.quorum", "hadoop01,hadoop02,hadoop03")
conf.set("hbase.zookeeper.property.clientPort", "2181")
conf.set(TableOutputFormat.OUTPUT_TABLE, "test")
val job = new Job(conf)
job.setOutputKeyClass(classOf[ImmutableBytesWritable])
job.setOutputValueClass(classOf[Result])
job.setOutputFormatClass(classOf[TableOutputFormat[ImmutableBytesWritable]]) var arrResult: Array[String] = new Array[String](1)
arrResult(0) = "1, 3000000000";
//arrResult(0) = "1,100,11" val resultRDD = sc.makeRDD(arrResult)
val saveRDD = resultRDD.map(_.split(',')).map { arr => {
val put = new Put(Bytes.toBytes(arr(0)))
put.add(Bytes.toBytes("info"), Bytes.toBytes("total"), Bytes.toBytes(arr(1)))
(new ImmutableBytesWritable, put)
}
}
println("getConfiguration")
var c = job.getConfiguration()
println("save")
saveRDD.saveAsNewAPIHadoopDataset(c) sc.stop()
// spark.stop()
} }
spark读取hbase形成RDD,存入hive或者spark_sql分析的更多相关文章
- Spark读取Hbase中的数据
大家可能都知道很熟悉Spark的两种常见的数据读取方式(存放到RDD中):(1).调用parallelize函数直接从集合中获取数据,并存入RDD中:Java版本如下: JavaRDD<Inte ...
- Spark 读取HBase和SolrCloud数据
Spark1.6.2读取SolrCloud 5.5.1 //httpmime-4.4.1.jar // solr-solrj-5.5.1.jar //spark-solr-2.2.2-20161007 ...
- Spark 读取HBase数据
Spark1.6.2 读取 HBase 1.2.3 //hbase-common-1.2.3.jar //hbase-protocol-1.2.3.jar //hbase-server-1.2.3.j ...
- spark读取hbase(NewHadoopAPI 例子)
package cn.piesat.controller import java.text.{DecimalFormat, SimpleDateFormat}import java.utilimpor ...
- Spark读取HBase
背景:公司有些业务需求是存储在HBase上的,总是有业务人员找我要各种数据,所以想直接用Spark( shell) 加载到RDD进行计算 摘要: 1.相关环境 2.代码例子 内容 1.相关环境 Spa ...
- spark读取hbase数据
def main(args: Array[String]): Unit = { val hConf = HBaseConfiguration.create(); hConf.set("hba ...
- Spark读取Hbase的数据
val conf = HBaseConfiguration.create() conf.addResource(new Path("/opt/cloudera/parcels/CDH-5.4 ...
- Spark整合HBase,Hive
背景: 场景需求1:使用spark直接读取HBASE表 场景需求2:使用spark直接读取HIVE表 场景需求3:使用spark读取HBASE在Hive的外表 摘要: 1.背景 2.提交脚本 内容 场 ...
- spark大批量读取Hbase时出现java.lang.OutOfMemoryError: unable to create new native thread
这个问题我去网上搜索了一下,发现了很多的解决方案都是增加的nproc数量,即用户最大线程数的数量,但我修改了并没有解决问题,最终是通过修改hadoop集群的最大线程数解决问题的. 并且网络上的回答多数 ...
随机推荐
- 安全易用的云许可-VirboxLM许可管理平台
Virbox LM是深思推出的基于云许可管理的开放平台,旨在为开发者提供低成本.高强度.操作便捷的一站式软件保护方案. Virbox LM包括用户许可管理工具.加壳工具.API帮助工具.开发商管理工具 ...
- Java中Collections类的排序sort函数两种用法
java中的Colletions类主要实现列表List的排序功能.根据函数参数的传递,具体的排序可以分为 : 1. 自然排序(natural ordering). 函数原型:sort(List< ...
- PHP 注册错误和异常处理机制
注册错误和异常处理机制有三个PHP函数需要学习 1. register_shutdown_function('Bootstrap\Library\Frame::fatalError'); 2. set ...
- (转)EOS中账户、钱包和密钥的关系
EOS对于账户的设计与ETH有很大的不同,引入了Account账户, Wallet钱包, 钱包密码, Key公私钥, Permission权限等众多概念,刚入门的时候感觉一头雾水.本文希望通过对这些概 ...
- Java 基础 引用数据类型 ArrayList集合
引用数据类型(类) 分类 提到引用数据类型(类),其实我们对它并不陌生,如使用过的Scanner类.Random类. 我们可以把类的类型为两种: 第一种,Java为我们提供好的类,如Scanner类, ...
- windows下使用sed和tee命令
最近需要在winowds slave上设置构建app和sdk,至于如何在windows slave上构建c/c++代码生成sdk(dll,lib之类)和apk(exe文件),请参考我的另外一篇博客,即 ...
- Java Selenium - 元素操作 (二)
一篇概括了常用的元素定位方法,但是找到元素还是不够的,模拟鼠标的操作,完成各个功能点的自动操作才是关键. 下面是常见的页面元素操作会涉及到的方法,不是很全,比较复杂的后面单独拿出来做案例. 一, 输入 ...
- Java Selenium - 浏览器操作
浏览器主要操作方法来自接口 org.openqa.selenium.WebDriver , 实现于org.openqa.selenium.remote.RemoteWebDriver这个类,然后不同浏 ...
- CentOS6.5安装RHBase
1.安装依赖包 yum install automake libtool flex bison pkgconfig gcc-c++ boost-devel libevent-devel lib-dev ...
- python while for else
python的循环挺有意思 while和for体中可以带上else项 while中的else表示循环条件不成立时,去执行一次,也就是退出循环前去做一次 for中的else表示固定循环正常完成后,去执行 ...