javaAPI操作Hbase
package chapter04; import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.client.TableDescriptorBuilder; public class ExampleForHbase {
//配置类
public static Configuration configuration ;
// 连接类
public static Connection connection ;
// 管理类
public static Admin admin; public static void main(String[] args) {
init();
//创建表
//createTable("Score",new String[]{"sname","course"});
// 删除表
//dropTable("Score");
// 查询所有的表
//listTable();
// 新增一条数据
//insertRow("Score", "98001", "sname", "", "张三");
// insertRow("Score","98001","course","math","80");
// 查询数据
getData("Score", "98001", "course", "math");
// 删除数据
deleteRow("Score", "98001", "sname", "");
getData("Score", "98001", "sname","");
close();
}
/**
* 初始化
*/
public static void init() {
configuration = HBaseConfiguration.create();
configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
try {
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 关闭
*/
public static void close() {
try {
if(admin!=null) {
admin.close();
}
if(null != connection) {
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 创建表
* @param tabName
* @param colFamily
*/
public static void createTable(String tabName,String[] colFamily) {
try {
TableName tableName = TableName.valueOf(tabName);
if(admin.tableExists(tableName)) {
System.out.println("Table is exist!");
}else {
TableDescriptorBuilder tableBuilder = TableDescriptorBuilder.newBuilder(tableName);
for(String str:colFamily) {
tableBuilder.setColumnFamily(ColumnFamilyDescriptorBuilder.of(str)) ;
}
admin.createTable(tableBuilder.build());
System.out.println("Table create successful!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 删除表
* @param tabName
*/
public static void dropTable(String tabName) {
try {
TableName tableName = TableName.valueOf(tabName);
admin.disableTable(tableName);
admin.deleteTable(tableName);
System.out.println("删除成功"+tabName);
close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 查询表列表
*/
public static void listTable() {
try {
TableName[] tbNames = admin.listTableNames();
for (TableName tableName : tbNames) {
System.out.println(tableName.getNameAsString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 新增一条记录
* @param tabName 表名
* @param rowKey 行键
* @param colFamily 列族
* @param column 列名
* @param value 单元值
*/
public static void insertRow(String tabName,String rowKey,String colFamily,String column,String value) {
try {
TableName tableName = TableName.valueOf(tabName);
Table table = connection.getTable(tableName);
Put put = new Put(rowKey.getBytes());
put.addColumn(colFamily.getBytes(), column.getBytes(), value.getBytes());
table.put(put);
table.close();
System.out.println("新增纪录成功!");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 删除行数据
* @param tabName 表名
* @param rowKey 行键
* @param colFamily 列族
* @param qualifier 列修饰符
*/
public static void deleteRow(String tabName,String rowKey,String colFamily,String qualifier) {
try {
TableName tableName = TableName.valueOf(tabName);
Table table = connection.getTable(tableName);
Delete delete = new Delete(rowKey.getBytes());
// 删除指定列族的所有数据
if(StringUtils.isNotEmpty(qualifier) && StringUtils.isNotEmpty(colFamily)) {
delete.addColumn(colFamily.getBytes(),qualifier.getBytes());
}else if(StringUtils.isNotEmpty(colFamily)) {
delete.addFamily(colFamily.getBytes());
}
table.delete(delete);
System.out.println("删除数据成功");
table.close();
} catch (Exception e) { }
}
/**
* 查询数据
* @param tabName
* @param rowKey
* @param colFamily
* @param qualifier
*/
public static void getData(String tabName,String rowKey,String colFamily,String qualifier) {
try {
TableName tableName = TableName.valueOf(tabName);
Table table = connection.getTable(tableName);
Get get = new Get(rowKey.getBytes());
if(StringUtils.isNotEmpty(qualifier) && StringUtils.isNotEmpty(colFamily)) {
get.addColumn(colFamily.getBytes(),qualifier.getBytes());
}else if(StringUtils.isNotEmpty(colFamily)) {
get.addFamily(colFamily.getBytes());
}
Result result = table.get(get); if(!result.isEmpty()) {
showCell(result);
}else {
System.out.println("数据已经不存在");
}
table.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 格式化输出
* @param result
*/
private static void showCell(Result result){
Cell[] cells = result.rawCells();
for(Cell cell:cells){
System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
System.out.println("Timetamp:"+cell.getTimestamp()+" ");
System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");
}
}
}
javaAPI操作Hbase的更多相关文章
- HBase JavaAPI操作示例
package testHBase; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBase ...
- Hbase深入学习(六) Java操作HBase
Hbase深入学习(六) ―― Java操作HBase 本文讲述如何用hbase shell命令和hbase java api对hbase服务器进行操作. 先看以下读取一行记录hbase是如何进行工作 ...
- HBase(六)HBase整合Hive,数据的备份与MR操作HBase
一.数据的备份与恢复 1. 备份 停止 HBase 服务后,使用 distcp 命令运行 MapReduce 任务进行备份,将数据备份到另一个地方,可以是同一个集群,也可以是专用的备份集群. 即,把数 ...
- 大数据技术之_11_HBase学习_02_HBase API 操作 + HBase 与 Hive 集成 + HBase 优化
第6章 HBase API 操作6.1 环境准备6.2 HBase API6.2.1 判断表是否存在6.2.2 抽取获取 Configuration.Connection.Admin 对象的方法以及关 ...
- 基于jython操作hbase
一.前言 关于jython介绍,直接上官网www.jython.org,可以得到详细资料,这里只介绍一下jython操作hbase的一些方法,本质上和用java操作hbase差不多,只不过语法换成了p ...
- PySpark操作HBase时设置scan参数
在用PySpark操作HBase时默认是scan操作,通常情况下我们希望加上rowkey指定范围,即只获取一部分数据参加运算.翻遍了spark的python相关文档,搜遍了google和stackov ...
- Java操作hbase总结
用过以后,总得写个总结,不然,就忘喽. 一.寻找操作的jar包. java操作hbase,首先要考虑到使用hbase的jar包. 因为咱装的是CDH5,比较方便,使用SecureCRT工具,远程连接到 ...
- spark 操作hbase
HBase经过七年发展,终于在今年2月底,发布了 1.0.0 版本.这个版本提供了一些让人激动的功能,并且,在不牺牲稳定性的前提下,引入了新的API.虽然 1.0.0 兼容旧版本的 API,不过还是应 ...
- HBase 6、用Phoenix Java api操作HBase
开发环境准备:eclipse3.5.jdk1.7.window8.hadoop2.2.0.hbase0.98.0.2.phoenix4.3.0 1.从集群拷贝以下文件:core-site.xml.hb ...
随机推荐
- Python的map方法的应用
Map方法,第一个参数要写一个匿名函数表达式,或者是一个函数引用,第二个第三个往后都是表达式用到的参数,参数一般是可迭代的 1.比如下面这个map方法,两个参数,第一个 lambda x: x*x是匿 ...
- Qt的诞生和本质
有没有发现,在这个主函数里面没有了消息循环了.消息循环到哪里去了呢? 我们知道,每个GUI应用程序都有消息循环,一般都需要将消息循环while那个函数写到main的最后位置.那既然如此,为了代码复用, ...
- mysqlbinlog-Note
binlog_format = mixedlog-bin = /data/mysql/mysql-binexpire_logs_days = 7 #binlog过期清理时间max_binlog_siz ...
- Pro Micro
选择这块Arduino板主要是因为它便宜(淘宝上20元左右搞定),引脚相对较多,体积小,而且其使用的处理器核心ATmega32U4(兼容Arduino Leonardo)可用于模拟HID设备,可以配合 ...
- (day69)axios、配置ElementUI、配置jQuery和Bootstrap、Django中的CORS问题
目录 一.Vue的ajax插件:axios 二.Django中的CORS跨域问题 (一)同源策略 (二)解决方式(cors模块) 三.Vue配置ElementUI 四.Vue配置jQuery和Boot ...
- vuetify,vux,Mint UI 等框架的选择
vuetify: https://vuetifyjs.com/zh-Hans/getting-started/quick-start NutUI:https://github.com/jdf2e/nu ...
- 第2次作业-titanic数据集练习
一.读入titanic.xlsx文件,按照教材示例步骤,完成数据清洗. titanic数据集包含11个特征,分别是: Survived:0代表死亡,1代表存活Pclass:乘客所持票类,有三种值(1, ...
- RAC数据库的ORA-27123: Unable To Attach To Shared Memory Segment Linux-x86_64 Error: 22: Invalid argument
RAC数据库的 ORA-27123: Unable To Attach To Shared Memory Segment Linux-x86_64 Error: 22: Invalid argumen ...
- JAVA CST时间 转换成Date
Mybatis中处理Oracle时间类型是个比较麻烦的问题,特别是需要用到时间做比较的,可参考以下代码与思路: 格式化CST时间 SimpleDateFormat sdf = new SimpleDa ...
- Concepts & Implementation of PLs
http://perugini.cps.udayton.edu/teaching/courses/Spring2015/cps352/index.html#lecturenotes Programmi ...