Spark读写Hbase中的数据
def main(args: Array[String]) {
val sparkConf = new SparkConf().setMaster("local").setAppName("cocapp").set("spark.kryo.registrator", classOf[HBaseConfiguration].getName)
.set("spark.executor.memory", "4g")
val sc: SparkContext = new SparkContext(sparkConf)
val sqlContext = new HiveContext(sc)
val mySQLUrl = "jdbc:mysql://localhost:3306/yangsy?user=root&password=yangsiyi"
val rows = sqlContext.jdbc(mySQLUrl, "person")
val tableName = "spark"
val columnFamily = "cf" //rows.first().getString(1)
val configuration = HBaseConfiguration.create();
configuration.set(TableInputFormat.INPUT_TABLE, "test");
val admin = new HBaseAdmin(configuration)
val hBaseRDD = sc.newAPIHadoopRDD(configuration, classOf[TableInputFormat],
classOf[org.apache.hadoop.hbase.io.ImmutableBytesWritable],
classOf[org.apache.hadoop.hbase.client.Result])
hBaseRDD.count()
def toHbase(rows: DataFrame,tableName : String,columnFamily: String) {
val configuration = HBaseConfiguration.create();
val admin = new HBaseAdmin(configuration)
if (admin.tableExists(tableName)) {
print("table Exists")
admin.disableTable(tableName);
admin.deleteTable(tableName);
}
configuration.addResource("hbase-site.xml")
val tableDesc = new HTableDescriptor(tableName)
tableDesc.addFamily(new HColumnDescriptor(columnFamily))
admin.createTable(tableDesc)
rows.foreachPartition { row =>
val table = new HTable(configuration, tableName) row.foreach { a =>
val put = new Put(Bytes.toBytes("row1"))
put.add(Bytes.toBytes(columnFamily), Bytes.toBytes("coulumn1"), Bytes.toBytes(a.getString(0)))
table.put(put)
println("insert into success")
}
}
然而并没有什么乱用,发现一个问题,就是说,在RDD取值与写入HBASE的时候,引入外部变量无法序列化。。。。。。网上很多说法是说extends Serializable ,可是尝试无效。Count()是可以获取到,但是如果我要在configuration中set列,然后进行查询就会报错了。暂时各种办法尝试无果,还在想办法,也不明原因。
Spark读写Hbase中的数据的更多相关文章
- IDEA中Spark往Hbase中写数据
import org.apache.hadoop.hbase.HBaseConfiguration import org.apache.hadoop.hbase.io.ImmutableBytesWr ...
- IDEA中Spark读Hbase中的数据
import org.apache.hadoop.hbase.HBaseConfiguration import org.apache.hadoop.hbase.io.ImmutableBytesWr ...
- Spark读取Hbase中的数据
大家可能都知道很熟悉Spark的两种常见的数据读取方式(存放到RDD中):(1).调用parallelize函数直接从集合中获取数据,并存入RDD中:Java版本如下: JavaRDD<Inte ...
- 用Spark向HBase中插入数据
java代码如下: package db.insert; import java.util.Iterator; import java.util.StringTokenizer; import org ...
- 使用Hive或Impala执行SQL语句,对存储在HBase中的数据操作
CSSDesk body { background-color: #2574b0; } /*! zybuluo */ article,aside,details,figcaption,figure,f ...
- Spark读写HBase
Spark读写HBase示例 1.HBase shell查看表结构 hbase(main)::> desc 'SDAS_Person' Table SDAS_Person is ENABLED ...
- 使用spark将内存中的数据写入到hive表中
使用spark将内存中的数据写入到hive表中 hive-site.xml <?xml version="1.0" encoding="UTF-8" st ...
- Spark读写Hbase的二种方式对比
作者:Syn良子 出处:http://www.cnblogs.com/cssdongl 转载请注明出处 一.传统方式 这种方式就是常用的TableInputFormat和TableOutputForm ...
- spark读写hbase性能对比
一.spark写入hbase hbase client以put方式封装数据,并支持逐条或批量插入.spark中内置saveAsHadoopDataset和saveAsNewAPIHadoopDatas ...
随机推荐
- URAL 1208 Legendary Teams Contest(DFS)
Legendary Teams Contest Time limit: 1.0 secondMemory limit: 64 MB Nothing makes as old as years. A l ...
- PWM控制led渐变
PWM,中文释义:脉冲宽度调制.它是利用微处理器的数字输出来对模拟电路进行控制的一种非常有效的技术. PWM 是一种对模拟信号电平进行数字编码的方法.通过高分辨率计数器的使用,方波的占空比被调制用来对 ...
- SoftmaxLayer and SoftmaxwithLossLayer 代码解读
SoftmaxLayer and SoftmaxwithLossLayer 代码解读 Wang Xiao 先来看看 SoftmaxWithLoss 在prototext文件中的定义: layer { ...
- java枚举实例
实例一: public enum OrderOption {ASC,DESC; } 实例二(带参数构造函数): public enum OrderOption { ASC("ASC" ...
- http://www.allthingsdistributed.com
http://www.allthingsdistributed.com159-6289-2518
- line-hieght与vertical-align的区别与联系
7.3 line-height 行高指的是文本行的基线间的距离,但是文本之间的空白距离不仅仅是行高决定的, 同时也受字号的影响. 7.3.1 语 法 line-height属性的具体定义列表如下: 语 ...
- Android开源项目第一篇——个性化控件(View)篇
本文为那些不错的Android开源项目第一篇——个性化控件(View)篇,主要介绍Android上那些不错个性化的View,包括ListView.ActionBar.Menu.ViewPager.Ga ...
- Andorid面试问题整理
Acitivty的四中启动模式与特点. standard:默认的启动模式 singleTop:适合那种接受通知启动的页面,比如新闻客户端之类的,可能会给你推送好几次 ,但是每次都是打开同一张页面调用o ...
- Linux查看端口使用状态、关闭端口方法
前提:首先你必须知道,端口不是独立存在的,它是依附于进程的.某个进程开启,那么它对应的端口就开启了,进程关闭,则该端口也就关闭了.下次若某个进程再次开启,则相应的端口也再次开启.而不要纯粹的理解为关闭 ...
- ThreadContext
//#define UseThreadContext using System; using System.Collections.Generic; using System.Linq; using ...