一、使用sbt引入hbase依赖包

"org.apache.hbase" % "hbase-server" % "2.1.0",
"org.apache.hbase" % "hbase-common" % "2.1.0",
"org.apache.hbase" % "hbase-client" % "2.1.0",
"org.apache.hbase" % "hbase-mapreduce" % "2.1.0",
"org.apache.hbase" % "hbase" % "2.1.0" ,

二、检查hbase中是否存在某表

import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.io.compress.Compression.Algorithm;
import org.apache.hadoop.conf.Configuration; import java.io.IOException; /**
* 检查表是否存在,存在就删掉重新建立
* @author gy
*/
public class TableTest { private static void creatOrOverwrite(Admin admin, HTableDescriptor table) throws IOException {
if (admin.tableExists(table.getTableName())) {
admin.disableTable(table.getTableName());
admin.deleteTable(table.getTableName());
}
admin.createTable(table);
} public static void createSchemaTables(Configuration config,String tablename,String colname,String ip) throws Exception {
config.set("hbase.zookeeper.quorum", ip);
try (Connection connection = ConnectionFactory.createConnection(config);
Admin admin = connection.getAdmin()) {
HTableDescriptor table = new HTableDescriptor(TableName.valueOf(tablename));
table.addFamily(new HColumnDescriptor(colname).setCompressionType(Algorithm.NONE));
System.out.println("Create table "+tablename);
creatOrOverwrite(admin, table);
System.out.println(" Done.");
} }
}

三、将dataframe写入hbase

import org.apache.hadoop.hbase.HBaseConfiguration
import org.apache.hadoop.hbase.client.Put
import org.apache.hadoop.hbase.io.ImmutableBytesWritable
import org.apache.hadoop.hbase.mapred.TableOutputFormat
import org.apache.hadoop.hbase.util.Bytes
import org.apache.hadoop.mapred.JobConf
import org.apache.spark.sql.DataFrame
import org.apache.spark.rdd.RDD object Write2Hbase {
def webAdd(da: DataFrame, colname: String): RDD[(ImmutableBytesWritable, Put)] = {
da.rdd.map(x => {
val row = x.getString(2) +"-"+(Long.MaxValue - x.getTimestamp(1).getTime)
var userid=""
if(!x.isNullAt(2)){
userid=x.getString(2)
}
var put = new Put(Bytes.toBytes(row))
put.addColumn(Bytes.toBytes(colname), Bytes.toBytes("hyid"), Bytes.toBytes(x.getInt(0)))
.addColumn(Bytes.toBytes(colname), Bytes.toBytes("time"), Bytes.toBytes(x.getTimestamp(1).toString)))
      .addColumn(Bytes.toBytes(colname), Bytes.toBytes("ip"), Bytes.toBytes(x.getString(10)))
      (new ImmutableBytesWritable, put)
})
}
def data2hbase(data: DataFrame, ip: String, tablename: String): Unit = {
var colname = "web"
val conf = HBaseConfiguration.create()
import TableTest.createSchemaTables
val jobConf = new JobConf(conf)
jobConf.set("hbase.zookeeper.quorum", ip)
jobConf.set("hbase.zookeeper.property.clientPort", "2181")
jobConf.set(TableOutputFormat.OUTPUT_TABLE, tablename)
jobConf.setOutputFormat(classOf[TableOutputFormat])
var saveData: RDD[(ImmutableBytesWritable, Put)] = webAdd(data, colname)
createSchemaTables(conf, tablename, colname, ip)
saveData.saveAsHadoopDataset(jobConf)
}
}

当spark遇见hbase的更多相关文章

  1. MapReduce和Spark写入Hbase多表总结

    作者:Syn良子 出处:http://www.cnblogs.com/cssdongl 转载请注明出处 大家都知道用mapreduce或者spark写入已知的hbase中的表时,直接在mapreduc ...

  2. spark 操作hbase

    HBase经过七年发展,终于在今年2月底,发布了 1.0.0 版本.这个版本提供了一些让人激动的功能,并且,在不牺牲稳定性的前提下,引入了新的API.虽然 1.0.0 兼容旧版本的 API,不过还是应 ...

  3. Spark操作hbase

    于Spark它是一个计算框架,于Spark环境,不仅支持单个文件操作,HDFS档,同时也可以使用Spark对Hbase操作. 从企业的数据源HBase取出.这涉及阅读hbase数据,在本文中尽快为了尽 ...

  4. 大数据学习系列之九---- Hive整合Spark和HBase以及相关测试

    前言 在之前的大数据学习系列之七 ----- Hadoop+Spark+Zookeeper+HBase+Hive集群搭建 中介绍了集群的环境搭建,但是在使用hive进行数据查询的时候会非常的慢,因为h ...

  5. Spark读Hbase优化 --手动划分region提高并行数

    一. Hbase的region 我们先简单介绍下Hbase的架构和Hbase的region: 从物理集群的角度看,Hbase集群中,由一个Hmaster管理多个HRegionServer,其中每个HR ...

  6. spark读写hbase性能对比

    一.spark写入hbase hbase client以put方式封装数据,并支持逐条或批量插入.spark中内置saveAsHadoopDataset和saveAsNewAPIHadoopDatas ...

  7. Spark读写HBase

    Spark读写HBase示例 1.HBase shell查看表结构 hbase(main)::> desc 'SDAS_Person' Table SDAS_Person is ENABLED ...

  8. Spark读HBase写MySQL

    1 Spark读HBase Spark读HBase黑名单数据,过滤出当日新增userid,并与mysql黑名单表内userid去重后,写入mysql. def main(args: Array[Str ...

  9. Spark整合HBase,Hive

    背景: 场景需求1:使用spark直接读取HBASE表 场景需求2:使用spark直接读取HIVE表 场景需求3:使用spark读取HBASE在Hive的外表 摘要: 1.背景 2.提交脚本 内容 场 ...

随机推荐

  1. <每日一题>题目28:简单的python练习题(51-60)

    #51.一行代码实现1-100的和 sum(range(1,101)) #52.如何在一个函数内部修改全局变量 ''' 利用global ''' #53.字典如何删除和合并2个字典 ''' del d ...

  2. C# 判断当前请求是GET、还是POST ?

    方法一: HttpContext.Current.Request.RequestType == "POST"   //当前请求为:POST 方法二: if(Request.Serv ...

  3. 1、Zookeeper的功能以及工作原理

    1.ZooKeeper是什么? ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,它是集群的管理者,监视着集群中各个节点的状态根据节点提交 ...

  4. js格式化数字为金额

    /** * * @param num * @param precision * @param separator * @returns {*} *=========================== ...

  5. boost 日期时间计算

    示例代码如下: #include <boost/date_time/gregorian/gregorian.hpp> #include <boost/date_time/posix_ ...

  6. winDbg + VMware + window 双机联调环境搭建

    这里简单的介绍一下内核开发双机联调的搭建环境,尽管网上有很多类似的文章,但看了很多总是不太舒服,觉得不太明白,所以自己实践一下总结一篇.下面就拿我的环境简单介绍,希望别人可以看懂. 准备工具:装虚拟机 ...

  7. 原生微信小程序的生命周期

    小程序的生命周期函数:onLaunch:function(){当启动小程序时触发小程序只会启动1次,一般为初次打开时一般只会触发一次},onShow:function(){当小程序从后台切入到前台时触 ...

  8. NSIS使用WinVer.nsh头文件判断操作系统版本

    NSIS使用WinVer.nsh头文件判断操作系统版本,首先请下载最新的WinVer.nsh: http://nsis.sourceforge.net/Include/WinVer.nsh(下载后置于 ...

  9. JDK、Eclipse、Tomcat、Maven、IDEA 常见问题

    windows操作分成了32位和64位的系统,不同的系统安装的软件也不一样. 查询电脑操作系统是多少位? J D K 01. 下载安装 02. 目录解释 03. 配置环境变量 (JDK安装成功后进行配 ...

  10. MySQL数据库 字段操作 多表关系(更新中...)

    外键 (foreign key) ## 外键 ```mysql # 作者(author):id,name,sex,age,mobile, detail_id # 作者详情(author_detail) ...