package cn.hbase.demo;

 import java.io.IOException;
import java.util.Iterator; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellScanner;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
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.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.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.regionserver.StripeStoreFileManager;
import org.apache.hadoop.hbase.util.Bytes; /**
* 增删改查 注意hbase中一行指的是一个列族,所以一行可能含有多条数据
*
* @author Tele
*
*/ public class CrudTable {
private static Connection conn;
private static Configuration conf;
private static Admin admin;
static {
try {
conf = HBaseConfiguration.create();
conn = ConnectionFactory.createConnection(conf);
admin = conn.getAdmin();
} catch (IOException e) {
e.printStackTrace();
} } public static boolean isExistTable(String tableName) throws IOException {
return admin.tableExists(TableName.valueOf(tableName));
} /**
* 创建表
*
* @param tableName
* @param columnFamily 列族,创建表时可以传递多个列族过来
* @throws IOException
*/
public static void createTable(String tableName, String... columnFamily) throws IOException { if (isExistTable(tableName)) {
System.out.println("已经存在表" + tableName);
return;
} else {
HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
for (String cf : columnFamily) {
tableDescriptor.addFamily(new HColumnDescriptor(cf));
}
admin.createTable(tableDescriptor);
System.out.println("成功创建了表" + tableName);
}
} /**
* 创建多版本的表
*
* @param tableName
* @param columnFamily 列族,创建表时可以传递多个列族过来
* @throws IOException
*/
public static void createTableMultiVersion(String tableName, String veriosn, String... columnFamily)
throws IOException { if (isExistTable(tableName)) {
System.out.println("已经存在表" + tableName);
return;
} else {
HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
for (String cf : columnFamily) {
tableDescriptor.addFamily(new HColumnDescriptor(cf).setVersions(1, 3));
}
admin.createTable(tableDescriptor);
System.out.println("成功创建了表" + tableName);
}
} /**
* 删除表之前必须先禁用表
*
* @param tableName
* @throws IOException
*/
public static void dropTable(String tableName) throws IOException {
if (isExistTable(tableName)) {
admin.disableTable(TableName.valueOf(tableName));
admin.deleteTable(TableName.valueOf(tableName));
System.out.println("表" + tableName + "删除成功");
} else {
System.out.println("表" + tableName + "不存在");
} } /**
* 添加一条数据
*
* @param tableName
* @param rowKey 行键
* @param cf 列族
* @param cn 列名
* @param value
* @throws IOException
*/
public static void addRow(String tableName, String rowKey, String cf, String cn, String value) throws IOException {
// 先判断表是否存在
if (isExistTable(tableName)) {
Table table = conn.getTable(TableName.valueOf(tableName));
Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn), Bytes.toBytes(value)); table.put(put);
System.out.println("成功插入一条数据");
table.close();
} else {
System.out.println("待插入的表不存在");
}
} /**
* 删除一条数据
*
* @param tableName
* @param rowKey
* @param cf
* @param cn
* @throws IOException
*/
public static void deleteRow(String tableName, String rowKey, String cf, String cn) throws IOException {
if (isExistTable(tableName)) {
Table table = conn.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(Bytes.toBytes(rowKey));
delete.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn));
table.delete(delete);
System.out.println("删除成功");
table.close();
} else {
System.out.println("表不存在");
} } /**
* 删除一行数据,即删除该行数据对应的一个列族的数据
*
* @param tableName
* @param rowKey
* @param cf
* @throws IOException
*/
public static void deleteMultiRow(String tableName, String rowKey, String cf) throws IOException {
if (isExistTable(tableName)) {
Table table = conn.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(Bytes.toBytes(rowKey));
delete.addFamily(Bytes.toBytes(cf));
table.delete(delete);
System.out.println("删除成功");
table.close();
} else {
System.out.println("表不存在");
}
} /**
* 查询指定列族.列名的数据,一个reuslt代表一个列的全部数据
*
* @param tableName
* @param rowKey
* @param cf
* @param cn
* @throws IOException
*/
public static void getRow(String tableName, String rowKey, String cf, String cn) throws IOException {
if (isExistTable(tableName)) {
Table table = conn.getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowKey));
get.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn));
Result result = table.get(get); // 遍历result
CellScanner cellScanner = result.cellScanner();
while (cellScanner.advance()) {
Cell cell = cellScanner.current();
System.out.println("行键:" + Bytes.toString(CellUtil.copyRow(cell)));
System.out.println("列族:" + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("列名:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
}
table.close();
} else {
System.out.println("表不存在");
}
} /**
* 查询指定列多个版本的数据
*
* @param tableName
* @param rowKey
* @param cf
* @param cn
* @throws IOException
*/
public static void getRowMultiVersion(String tableName, String rowKey, String cf, String cn) throws IOException {
if (isExistTable(tableName)) {
Table table = conn.getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowKey)); // get.setMaxVersions();
get.readAllVersions(); get.addColumn(Bytes.toBytes(cf), Bytes.toBytes(cn));
Result result = table.get(get); // 遍历result
CellScanner cellScanner = result.cellScanner();
while (cellScanner.advance()) {
Cell cell = cellScanner.current();
System.out.println("行键:" + Bytes.toString(CellUtil.copyRow(cell)));
System.out.println("列族:" + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("列名:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
}
table.close();
} else {
System.out.println("表不存在");
}
} /**
* 查询指定列族数据 一个reuslt代表一个行键的全部数据
*
* @param tableName
* @param rowKey
* @param cf
* @param cn
* @throws IOException
*/
public static void getMultiRows(String tableName, String rowKey, String cf) throws IOException {
if (isExistTable(tableName)) {
Table table = conn.getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowKey));
get.addFamily(Bytes.toBytes(cf));
Result result = table.get(get); // 遍历result
CellScanner cellScanner = result.cellScanner();
while (cellScanner.advance()) {
Cell cell = cellScanner.current();
System.out.println("行键:" + Bytes.toString(CellUtil.copyRow(cell)));
System.out.println("列族:" + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("列名:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
System.out.println();
}
table.close();
} else {
System.out.println("表不存在");
}
} /**
* 查询全部数据 一个reuslt代表一个列族的全部数据
*
* @param tableName
* @throws IOException
*/
public static void scanTable(String tableName) throws IOException {
if (isExistTable(tableName)) {
Table table = conn.getTable(TableName.valueOf(tableName)); Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
// 遍历scanner
Iterator<Result> iterator = scanner.iterator();
while (iterator.hasNext()) {
Result result = iterator.next();
// 遍历result
CellScanner cellScanner = result.cellScanner();
while (cellScanner.advance()) {
Cell cell = cellScanner.current();
System.out.println("行键:" + Bytes.toString(CellUtil.copyRow(cell)));
System.out.println("列族:" + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("列名:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
}
System.out.println();
} table.close();
} else {
System.out.println("表不存在");
} } /**
* 从指定行scan表
*
* @param tableName
* @param startRow
* @throws IOException
*/
public static void scanTableByRow(String tableName, String startRow) throws IOException {
if (isExistTable(tableName)) {
Table table = conn.getTable(TableName.valueOf(tableName)); Scan scan = new Scan();
// scan.setStartRow(Bytes.toBytes(startRow));
scan.withStartRow(Bytes.toBytes(startRow));
ResultScanner scanner = table.getScanner(scan);
// 遍历scanner
Iterator<Result> iterator = scanner.iterator();
while (iterator.hasNext()) {
Result result = iterator.next();
// 遍历result
CellScanner cellScanner = result.cellScanner();
while (cellScanner.advance()) {
Cell cell = cellScanner.current();
System.out.println("行键:" + Bytes.toString(CellUtil.copyRow(cell)));
System.out.println("列族:" + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("列名:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("值:" + Bytes.toString(CellUtil.cloneValue(cell)));
}
System.out.println();
} table.close();
} else {
System.out.println("表不存在");
} } public static void main(String[] args) throws IOException {
// createTable("yeye","info","hobby");
// dropTable("yeye");
// addRow("yeye","1","info","name","tele");
// deleteRow("yeye","1","info","name");
// deleteMultiRow("yeye","1","info"); // getRow("yeye","1","info","name");
// getMultiRows("yeye","1","info"); // scanTable("yeye"); // scanTableByRow("yeye","2"); // 创建多版本的表
// createTableMultiVersion("staff","3","info","sex"); /*
* addRow("staff","1","info","name","wyc");
* addRow("staff","1","info","name","baba");
* addRow("staff","1","info","name","yeye");
*/ // 查询多版本
getRowMultiVersion("staff", "1", "info", "name"); } }

hbase 2.0.2 增删改查的更多相关文章

  1. 使用iBATIS3.0完成增删改查

    使用iBATIS3.0完成增删改查 iBATIS3.0和以前的版本有一些改变,不过学过以前版本的再学习3.0应该不是太难,3.0要求JDK1.5支持,因为其中增加了注解和泛型,这些都是JDK1.5才有 ...

  2. MVC3.0 EF增删改查的封装类

    本人亲身使用EF CodeFirst,因为增删改查都是使用EF内置的一些方法,我想把它封装到一个类调用就行了.结合网上的资料和自己的整理,若有不对的地方望斧正,感激不尽.直接上代码吧.我就用新闻的增删 ...

  3. YII2.0 数据库增删改查

    /*==================== dkhBaseModel 数据库增删改查方法 start ================================*/ //新增一条数据 publ ...

  4. hbase常用操纵操作——增删改查

    查询某个资金账户的信息 get 'dmp:hbase_tags','资金账号' 创建表 create 'emp', 'personal data', 'professional data' 在HBas ...

  5. Hbase常用操作(增删改查)

    Hbase常用操作(增删改查) [日期:2014-01-03] 来源:Linux社区  作者:net19880504 [字体:大 中 小]     运行Eclipse,创建一个新的Java工程“HBa ...

  6. 完成在本机远程连接HBase进行数据增删改查

    1.进行hbase与本机远程连接测试连接 1.1 修改虚拟机文件hbase-site.xml(cd/usr/local/hbase/conf)文件,把localhost换成你的虚拟机主机名字 1.2修 ...

  7. MongoDB 3.0.6 安装 增删改查

    下载 安装包MSI http://yunpan.cn/cmhHdTPkXZRM2  访问密码 9b6c 上边提供的是 MongoDB 3.0.6 64Bit 的安装包 安装 如果不想直接安装在C盘.. ...

  8. Yii2.0高级框架数据库增删改查的一些操作(转)

    yii2.0框架是PHP开发的一个比较高效率的框架,集合了作者的大量心血,下面通过用户为例给大家详解yii2.0高级框架数据库增删改查的一些操作 --------------------------- ...

  9. primefaces4.0基本教程以及增删改查

    最近试着用了用primefaces4.0,准备写一个基本的增删改查以及分页程序,但在写的过程中发现了很多问题,本想通过百度.谷歌解决,但无奈中文资料非常少,笔者在坑中不停的打滚,终于完成了一个有着基本 ...

随机推荐

  1. Java 学习(18):Java 序列化& 网络编程& 发送邮件

    --Java 序列化 -- 网络编程 -- 发送邮件 Java 序列化 Java 提供了一种对象序列化的机制,该机制中,一个对象可以被表示为一个字节序列,该字节序列包括该对象的数据.有关对象的类型的信 ...

  2. thinkphp5最最最最简单的ajax实例

    thinkphp5最最最最简单的ajax实例 一.总结 一句话总结:页面端使用$.get()方法传递ajax请求,服务器端判断是不是ajax请求,是的话接受参数,进行逻辑处理之后向客户端返回值. 1. ...

  3. Launcher Activity在开机时重新启动两次解决的方法

    今天在看log的时候发现,Launcher activity会被onDestroy掉一次.然后再重新启动. 可能原因推測: 1.横竖屏切换 2.MCC MNC等Configuration改变引起的 M ...

  4. Apache多虚拟主机多版本PHP(5.3+5.6+N)共存运行配置全过程

    摘要: 为需要实现在同一台Linux服务器上面,同时运行多个不同版本的PHP程序,本文我们将使用FastCGI方式加载,并把过程详细记录下来方便大家参考. 常规的PHP配置方式有很多种,例如CGI.f ...

  5. RDA安装

    解压到/home/oracle下面     $ cp /home/oracle/rda $ perl rda.pl -cv   运行上面的命令,如果最后一行出现下面所示,说明没问题       No ...

  6. 31、CMOS摄像头说明

    ov7740(摄像头模块) 输入信号: 自然景观等的模拟信号输出信号: RGB.YUV格式的数字信号 1). 常用参数输入信号: 自然景观等的模拟信号输出信号: 输出格式为:RAW RGB.YUV输出 ...

  7. 可直接复制粘贴的boostrap图标库网址

    1:http://fontawesome.dashgame.com/ 2:http://www.kuiyu.net/art-34.html 3:http://www.bootcss.com/p/fon ...

  8. [Angular2 Form] Reactive Form, show error message for one field

    <form [formGroup]="reactiveForm" novalidate autocomplete="off"> <div cl ...

  9. DirectX 11游戏编程学习笔记之1: 开场白

    本文由哈利_蜘蛛侠原创,转载请注明出处.有问题欢迎联系2024958085@qq.com           这是我之前的博客系列"DirectX9.0c游戏开发手记之'龙书'第二版学习笔记 ...

  10. [Recompose] Add Lifecycle Hooks to a Functional Stateless Component using Recompose

    Learn how to use the 'lifecycle' higher-order component to conveniently use hooks without using a cl ...