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. VS2012载入DLL编译出现试图载入格式不对的程序; 以及执行出现Mixed mode assembly is built against version &#39;v2.0.50727&#39; of the

    VS2012载入DLL编译出现试图载入格式不对的程序:以及执行出现Mixed mode assembly is built against version 'v2.0.50727' of therun ...

  2. amazeui页面分析2

    amazeui页面分析2 一.总结 1.弄清结构:这些部分都是一块一块分好了的,掌握结构之后,想替换哪块就替换哪块,想不要哪块就不要哪块,非常简单的 2.一块一块:替换十分简单 3.弄清楚大块之后,然 ...

  3. maven 解决Cannot change version of project facet Dynamic web module to 2.5

    我们用Eclipse创建Maven结构的web项目的时候选择了Artifact Id为maven-artchetype-webapp,由于这个catalog比较老,用的servlet还是2.3的,而一 ...

  4. Android JavaMail介绍及发送一封简单邮件

    本文来自:高爽|Coder,原文地址:http://blog.csdn.net/ghsau/article/details/17839983,转载请注明.       JavaMail是SUN提供给开 ...

  5. Hadoop笔记(一)

    1.大数据的概述 大数据:巨量数据.海量数据,首先在数据的量上达到一定的规模,首先是人或者计算机在不合理时间内是不能够实现的数据量. 2.特点:数据量比较大,数据类型多样化.处理速度问题 3.大数据平 ...

  6. SoC总线专题

    SoC总线专题 TileLink ARM系列总线 APB AHB AXI CHI Wishbone

  7. finish() OnDestroy() system.exit()

    1 finish()方法:activity动作完成的时候, 或者Activity需要关闭的时候, 调用此方法. 2 当你调用此方法的时候,系统只是将最上面的Activity移出了栈,并没有及时的调用o ...

  8. [RxJS] Subject: an Observable and Observer hybrid

    This lesson teaches you how a Subject is simply a hybrid of Observable and Observer which can act as ...

  9. 6 、字符编码笔记:ASCII,Unicode和UTF-8

    1. ASCII码 我们知道,在计算机内部,所有的信息最终都表示为一个二进制的字符串.每一个二进制位(bit)有0和1两种状态,因此八个二进制位就可以组合出 256种状态,这被称为一个字节(byte) ...

  10. sublime课程3 emmet插件中的常用符号有哪些

    sublime课程3 emmet插件中的常用符号有哪些 一.总结 一句话总结:emmet插件中的符号和css选择器里面哪些符号的意思很像. 1.+是干嘛的? 组合 2.{}是干嘛的? 标签里面的inn ...