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 ...
随机推荐
- HDU 5234 Happy birthday --- 三维01背包
HDU 5234 题目大意:给定n,m,k,以及n*m(n行m列)个数,k为背包容量,从(1,1)开始只能往下走或往右走,求到达(m,n)时能获得的最大价值 解题思路:dp[i][j][k]表示在位置 ...
- URAL 1004 Sightseeing Trip(最小环)
Sightseeing Trip Time limit: 0.5 secondMemory limit: 64 MB There is a travel agency in Adelton town ...
- hdu1058丑数(优先队列、暴力打表)
hdu1058 题意:当一个数只有2.3.5.7这四种质因数时(也可以一种都没有或只有其中几种),这个数就是丑数,输出第 n 个丑数是多少: 其实并没有发现hdu把这道题放在 dp 专题里的意图,我的 ...
- IE 下加载jQuery
转:http://www.iitshare.com/ie8-not-use-native-json.html 解决在IE8中无法使用原生JSON的问题 起因 在项目中要将页面上的js对象传给后台, ...
- click 绑定(一)无参数的click 事件绑定
目的 click绑定在DOM元素上添加事件句柄以便元素被点击的时候执行定义的JavaScript 函数.大部分是用在button,input和连接a上,但是可以在任意元素上使用. 例子 <d ...
- java多线程:并发包中ConcurrentHashMap和jdk的HashMap的对比
一:HashMap--->底层存储的是Entry<K,V>[]数组--->Entry<K,V>的结构是一个单向的链表static class Entry<K, ...
- caffe:编译时提示:unsupported GNU version! gcc versions later than 4.9 are not supported!
NVCC src/caffe/solvers/adam_solver.cuIn file included from /usr/local/cuda/include/cuda_runtime.h:76 ...
- RedHat Linux下利用sersync进行实时同步数据
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://linux5588.blog.51cto.com/65280/772054 拓扑图 ...
- LNMP-查看安装编译时参数
查看mysql编译参数: cat /usr/local/mysql/bin/mysqlbug | grep CONFIGURE_LINE 查看apache编译参数: cat $apachehome$/ ...
- vs2010 无法创建 *.edmx(Entity Frame Work) 文件的问题
当你安装了VS2010或者已经安装了EntityFramework41RC.exe之后发现依然在Add New Item时无法找到ADO.NET Entity Model,有可能是你创建的不是netf ...