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的更多相关文章

  1. HBase JavaAPI操作示例

    package testHBase; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBase ...

  2. Hbase深入学习(六) Java操作HBase

    Hbase深入学习(六) ―― Java操作HBase 本文讲述如何用hbase shell命令和hbase java api对hbase服务器进行操作. 先看以下读取一行记录hbase是如何进行工作 ...

  3. HBase(六)HBase整合Hive,数据的备份与MR操作HBase

    一.数据的备份与恢复 1. 备份 停止 HBase 服务后,使用 distcp 命令运行 MapReduce 任务进行备份,将数据备份到另一个地方,可以是同一个集群,也可以是专用的备份集群. 即,把数 ...

  4. 大数据技术之_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 对象的方法以及关 ...

  5. 基于jython操作hbase

    一.前言 关于jython介绍,直接上官网www.jython.org,可以得到详细资料,这里只介绍一下jython操作hbase的一些方法,本质上和用java操作hbase差不多,只不过语法换成了p ...

  6. PySpark操作HBase时设置scan参数

    在用PySpark操作HBase时默认是scan操作,通常情况下我们希望加上rowkey指定范围,即只获取一部分数据参加运算.翻遍了spark的python相关文档,搜遍了google和stackov ...

  7. Java操作hbase总结

    用过以后,总得写个总结,不然,就忘喽. 一.寻找操作的jar包. java操作hbase,首先要考虑到使用hbase的jar包. 因为咱装的是CDH5,比较方便,使用SecureCRT工具,远程连接到 ...

  8. spark 操作hbase

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

  9. 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 ...

随机推荐

  1. 你必须知道的Docker数据卷(Volume)

    本篇已加入<.NET Core on K8S学习实践系列文章索引>,可以点击查看更多容器化技术相关系列文章. 一.将Docker数据挂载到容器 在Docker中,要想实现数据的持久化(所谓 ...

  2. Spring Boot Redis 解析

    redis使用示例 本示例主要内容 使用lettuce操作redis redis字符串存储(RedisStringController.java) redis对象存储(RedisObjectContr ...

  3. c#时间戳相互转换

    /// <summary> /// 获取时间戳 /// </summary> /// <returns></returns> public static ...

  4. Java从入门到精通系统书籍,吐血整理的,只要1元

    思诚为修身之本,而明善又为思诚之本 我会在支付宝联系你 付完款后在支付宝账单详情页 点击联系收款方.我会回复您下载链接.

  5. PostgreSQL update set from 两表联合更新,注意与其它数据库更新语法有差别

    最近用PostgreSql数据库进行表关联更新时,发现与之前用的Sql Server 和My Sql语法有很大差别,稍微不注意,很容易出错. PostgreSql表更新时,两个表只允许一个表起别名,一 ...

  6. apache部分报错解决方法

    AH00558: 进入apache文件夹下的conf文件夹,打开httpd.conf文件,用ctrl+F找到ServerName,如下图 在下面加上一句: ServerName domain_name ...

  7. presentViewController底部弹框适配ipad

    //适配ipad if ([alert respondsToSelector:@selector(popoverPresentationController)]) { alert.popoverPre ...

  8. MVC、MVP与MVVM架构模式

    MVC(Model View Controller): View 层是界面,Model 层是业务逻辑,Controller 层用来调度 View 层和 Model 层, 将用户界面和业务逻辑合理的组织 ...

  9. Django出错提示TemplateDoesNotExist at /

    Issue: 打开login URL的时候报错如下: Action: 在setting.py中修改DIRS,模板文件目录 TEMPLATES = [ { 'BACKEND': 'django.temp ...

  10. How to restore and recover a database from an RMAN backup. (Doc ID 881395.1)

    APPLIES TO: Oracle Database - Enterprise Edition - Version 10.1.0.2 to 11.2.0.2 [Release 10.1 to 11. ...