hbase 2.0.2 增删改查
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 增删改查的更多相关文章
- 使用iBATIS3.0完成增删改查
使用iBATIS3.0完成增删改查 iBATIS3.0和以前的版本有一些改变,不过学过以前版本的再学习3.0应该不是太难,3.0要求JDK1.5支持,因为其中增加了注解和泛型,这些都是JDK1.5才有 ...
- MVC3.0 EF增删改查的封装类
本人亲身使用EF CodeFirst,因为增删改查都是使用EF内置的一些方法,我想把它封装到一个类调用就行了.结合网上的资料和自己的整理,若有不对的地方望斧正,感激不尽.直接上代码吧.我就用新闻的增删 ...
- YII2.0 数据库增删改查
/*==================== dkhBaseModel 数据库增删改查方法 start ================================*/ //新增一条数据 publ ...
- hbase常用操纵操作——增删改查
查询某个资金账户的信息 get 'dmp:hbase_tags','资金账号' 创建表 create 'emp', 'personal data', 'professional data' 在HBas ...
- Hbase常用操作(增删改查)
Hbase常用操作(增删改查) [日期:2014-01-03] 来源:Linux社区 作者:net19880504 [字体:大 中 小] 运行Eclipse,创建一个新的Java工程“HBa ...
- 完成在本机远程连接HBase进行数据增删改查
1.进行hbase与本机远程连接测试连接 1.1 修改虚拟机文件hbase-site.xml(cd/usr/local/hbase/conf)文件,把localhost换成你的虚拟机主机名字 1.2修 ...
- MongoDB 3.0.6 安装 增删改查
下载 安装包MSI http://yunpan.cn/cmhHdTPkXZRM2 访问密码 9b6c 上边提供的是 MongoDB 3.0.6 64Bit 的安装包 安装 如果不想直接安装在C盘.. ...
- Yii2.0高级框架数据库增删改查的一些操作(转)
yii2.0框架是PHP开发的一个比较高效率的框架,集合了作者的大量心血,下面通过用户为例给大家详解yii2.0高级框架数据库增删改查的一些操作 --------------------------- ...
- primefaces4.0基本教程以及增删改查
最近试着用了用primefaces4.0,准备写一个基本的增删改查以及分页程序,但在写的过程中发现了很多问题,本想通过百度.谷歌解决,但无奈中文资料非常少,笔者在坑中不停的打滚,终于完成了一个有着基本 ...
随机推荐
- [Ramda] Simple log function for debugging Compose function / Using R.tap for logging
const log = function(x){ console.log(x); return x; } const get = R.curry(function(prop, obj){ return ...
- Android Material风格的应用(四)--FloatActionButton
添加 FloatActionButton和SnackBar Android Material风格的应用(一)--AppBar TabLayoutAndroid Material风格的应用(二)--Re ...
- amazeui学习笔记--css(HTML元素5)--表格Table
amazeui学习笔记--css(HTML元素5)--表格Table 一.总结 1.基本样式:am-table:直接模块名 <table class="am-table"& ...
- 每天自动备份MySQL数据库的shell脚本
经常备份数据库是一个好习惯,虽然数据库损坏或数据丢失的概率很低,但一旦发生这种事情,后悔是没用的.一般网站或应用的后台都有备份数据库的功能按钮,但需要去手工执行.我们需要一种安全的,每天自动备份的方法 ...
- Fiddler抓包工具详细介绍
本文转自:http://www.cnblogs.com/Chilam007/p/6985379.html 一.Fiddler与其他抓包工具的区别 1.Firebug虽然可以抓包,但是对于分析http请 ...
- Nginx 设置,设置已经解析的域名,在nginx中没有定义相应server时的默认访问
https://blog.csdn.net/m_nanle_xiaobudiu/article/details/80785027
- 【例题5-7 UVA - 136】Ugly Numbers
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 每个丑数x,都能生成3个丑数2x,3x,5x 则我们以1作为起点. 生成丑数. 每次取出set里面最小的那个数. 然后用它去生成其他 ...
- ios获取系统时间
//获取系统时间 NSDate * date=[NSDate date]; NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init]; ...
- swift项目第七天:构建访客界面以及监听按钮点击
一:访客界面效果如图 二:xib封装访客视图的view 1:业务逻辑分析:1:由于用户未登录时要显示访客视图,要先进行判断用户是否登录,未登录则显示访客视图,登录则显示正常的登陆界面,由于要在四个子控 ...
- 【42.38%】【BZOJ 3196】二逼平衡树
Time Limit: 10 Sec Memory Limit: 128 MB Submit: 1363 Solved: 579 [Submit][Status][Discuss] Descripti ...