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. admin配置与Mysql数据库连接

    admin配置管理数据库的框架:web版的数据库管理页面初始化数据库: python manage.py makemigrations python manage.py migrate启动项目:(创建 ...

  2. docker工具之基本命令

    docker工具之基本命令 1.docker服务的启动.停止.重启 systemctl start docker #启动docker服务 systemctl daemon-reload #守护进程重启 ...

  3. python访问Apollo获取配置

    操作系统 : CentOS7.3.1611_x64 Python 版本 : 3.6.8 Apollo源码地址: https://github.com/ctripcorp/apollo 访问Apollo ...

  4. GitLab CI/CD持续集成设置

    GitLab CI/CD持续设置 官方文档地址(https://docs.gitlab.com/ee/ci/README.html) GitLab CI.CD功能非常完善,只需要简单几步,就可以完成项 ...

  5. Java replace() 方法 替换字符 也可用于 js 中

    JAVA中: public class Test { public static void main(String args[]) { String Str = new String("he ...

  6. ABP之Logging

    服务器端 ABP使用Castle Windsor的日志设施.它可以使用不同的日志库:Log4Net.NLog.Serilog等等.Castle为所有日志程序库提供了一个公共接口,这样,我们可以独立的使 ...

  7. Java日期处理组件joda-time

    版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/175 Java日期处理组件joda-time 平常在开发过 ...

  8. retrying failed action with response code: 403

    0x00 Event [2019-09-24T19:22:31,655][INFO ][logstash.outputs.elasticsearch] retrying failed action w ...

  9. 38-docker managed volume

    docker managed volume 与 bind mount 在使用上的最大区别是不需要指定 mount 源,指明 mount point 就行了.还是以 httpd 容器为例: 我们通过 - ...

  10. Lniux系统-Ubantu安装搜狗输入法

    1.在官网下载搜狗输入法的安装包-https://pinyin.sogou.com/linux/?r=pinyin 2.终端打开,进行解压安装--sudo dpkg -i sogoupinyin_2. ...