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集群的最大线程数解决问题的. 并且网络上的回答多数 ...
随机推荐
- (转)Docker镜像构建上下文(Context)
镜像构建上下文(Context) Docker在构建镜像时,如果注意,会看到 docker build 命令最后有一个 ... 表示当前目录,而 Dockerfile 就在当前目录,因此不少初学者以为 ...
- 【Java】-NO.13.Algorithm.1.Java Algorithm.1.001-【Java 常用算法手册 】-
1.0.0 Summary Tittle:[Java]-NO.13.Algorithm.1.Java Algorithm.1.001-[Java 常用算法手册 ]- Style:Java Series ...
- 阿里云香港主机自动换IP
为什么要写这个脚本原因你懂的,现在都是直接封IP pip3 install aliyun-python-sdk-alidns aliyun-python-sdk-domain aliyun-pytho ...
- PLSQL的SQL%NOTFOUND的使用场景
SELECT * INTO v_ticketStorageRow FROM BDM_TICKET_STORAGE WHERE p_startTicketNo >= START_NO_ AND p ...
- 《全栈性能Jmeter》-3JMeter体系结构
- 查看CPU信息
基础知识 示例: 开发机器是1个物理CPU,4核8线程,Intel(R) Core(TM) i7-3770 CPU @ 3.40GHz 基础 i3:2核模拟4线程,无睿频 i5:4核模拟4线程,有睿频 ...
- iOS UI基础-15.0 UIWebView
WebView介绍 知识点: 代码创建一个UIWebView OC调用html的js js页面调用OC 相关代码实现 代码创建一个UIWebView // 1.webView UIWebView *w ...
- 编写带有下列声明的例程:第一个例程是个驱动程序,它调用第二个例程并显示String str中的字符的所有排列。例如,str是"abc", 那么输出的串则是abc,acb,bac,bca,cab,cba,第二个例程使用递归。
全排列在笔试面试中很热门,因为它难度适中,既可以考察递归实现,又能进一步考察非递归的实现,便于区分出考生的水平.所以在百度和迅雷的校园招聘以及程序员和软件设计师的考试中都考到了,因此本文对全排列作下总 ...
- python爬虫-基础入门-爬取整个网站《1》
python爬虫-基础入门-爬取整个网站<1> 描述: 使用环境:python2.7.15 ,开发工具:pycharm,现爬取一个网站页面(http://www.baidu.com)所有数 ...
- PHP乘法表
<?php //一行*$a="*";echo ("$a <br>"); //一行50个*for($j=1;$j<=50;$j++){ e ...