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. programming+windows+MFC

    1)CMyApp declares no data members 2)CWinApp::InitInstance run after application build but before the ...

  2. 洛谷 P1599 结算日

    洛谷 P1599 结算日 题目描述 “不放债不借债”,贝西多么希望自己可以遵循这个忠告.她已经和她的N(1 <= N <= 100,000)个朋友有了债务关系,或者借债了,或者放债了.她的 ...

  3. 洛谷 P2118 比例简化

    P2118 比例简化 题目描述 在社交媒体上,经常会看到针对某一个观点同意与否的民意调查以及结果.例如,对某一观点表示支持的有1498 人,反对的有 902人,那么赞同与反对的比例可以简单的记为149 ...

  4. 5.容器管理【Docker每天5分钟】

    原文:5.容器管理[Docker每天5分钟] Docker给PaaS世界带来的“降维打击”,其实是提供了一种非常便利的打包机制.该机制打包了应用运行所需要的整个操作系统,从而保证了本地环境和云端环境的 ...

  5. netty epoll调用示例

    1.服务器端 import io.netty.bootstrap.ServerBootstrap; import io.netty.buffer.PooledByteBufAllocator; imp ...

  6. linux安装anaconda

    打开网址:https://repo.continuum.io/archive/ 下载对应版本: 然后把下载的文件放到linux系统上 在终端执行: bash Anaconda3-5.1.0-Linux ...

  7. IOS计算两点之间的距离

    //广州经纬度 CLLocationCoordinate2D guangZhouLocation; guangZhouLocation.latitude = 23.20; guangZhouLocat ...

  8. [Nuxt] Update Vuex State with Mutations and MapMutations in Vue.js

    You commit changes to state in Vuex using defined mutations. You can easily access these state mutat ...

  9. UVA 11732 - strcmp() Anyone? 字典树

    传送门:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  10. minizlib

    ZLIB开源库采用的是DEFLATE压缩算法,已经不支持加密功能,实际上功能还存在于代码中,采用MINIZIP可以支持对ZIP文件的加解密. ZLIB目前最新的是1.2.7,MINIZIP最新的版本是 ...