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 ...
随机推荐
- leetcode 120 Triangle ----- java
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- JavaWeb学习记录(二十二)——模式字符串与占位符
一.Java代码案例 @Test public void test10(){ int planet=7; String event="a disturban ...
- php常用配置(php.ini)
查看php配置文件的位置 # /usr/local/php/bin/php -i | head php配置文件中的注释是用;号 1.disable_functions(php要禁用的函数) phpin ...
- 固定虚拟机的IP
- CodeForces 558A
Description Amr lives in Lala Land. Lala Land is a very beautiful country that is located on a coord ...
- HTML5里autofocus属性
转载:http://www.webhek.com/html5-autofocus/ HTML5给我们带来了一大堆神奇的东西.以前需要用JavaScript和Flash完成的任务,例如表单校验,INPU ...
- java的nio之:java的nio系列教程之selector
一:Java NIO的selector的概述===>Selector(选择器)是Java NIO中能够检测一到多个NIO通道,并能够知晓通道是否为诸如读写事件做好准备的组件.这样,一个单独的线程 ...
- js的数组操作 splice
原文:点击打开链接 1.作用:从指定位置删除部分元素并增加新的元素 1.1.该方法返回值是被删除的元素组成的数组 1.2.splice是直接 ...
- oracle_dblink配置
说明:一下是未配置本地服务的dblink. 前提: 授权> grant create public database link,create database link to myAcco ...
- 【深入Cocos2d-x】使用MVC架构搭建游戏Four
喜欢Four这个项目,就赶快在GitHub上Star这个项目吧! 喜欢我的文章,来微博关注我吧:王选易在学C艹 点我下载 项目起源 项目Logo: 下面是该游戏的项目地址,各位想参考源代码的同学可以到 ...