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"); } }

最新文章

  1. (转)DataMatrix编码2——伽罗华域运算
  2. 在线程中用 OracleBulkCopy 导至 CPU 百分百
  3. druid配置(转)
  4. C#反射—解决类的实例化问题
  5. Asp.Net Identity自定义user类的运用,ClaimsIdentity
  6. 3.类型、值和变量-JavaScript权威指南笔记
  7. 体系结构复习2——指令级并行(分支预測和VLIW)
  8. 如何插上U盘 自动复制内容
  9. CCNA网络工程师学习进程(7)路由器的路由配置
  10. frameset导航框架
  11. 优化のzencart URL &amp;zenid=.....
  12. 【JDK1.8】JDK1.8集合源码阅读——LinkedHashMap
  13. Mybatis动态查询语句
  14. git 设置和取消代理
  15. 《android开发艺术探索》读书笔记(五)--RemoteViews
  16. 堡垒机jumpserver测试记录--安装
  17. SQL SERVER-时间戳(timestamp)与时间格式(datetime)互相转换
  18. 【C/C++】求解线性方程组的雅克比迭代与高斯赛德尔迭代
  19. C++著名程序库的比较和学习经验
  20. C# mongodb中内嵌文档数组条件查询

热门文章

  1. 在设置了android:parentActivityName后,点击子Activity返回键,父Activity总会调用OnDestroy()的解决方式
  2. JS实现按下按键触发点击事件
  3. (最新)使用爬虫刷CSDN博客访问量——亲测有效
  4. 每天自动备份MySQL数据库的shell脚本
  5. CSS两列布局——左侧宽度固定,右侧宽度自适应的3种方法
  6. Spring中@Async用法详解及简单实例
  7. 我的前端规范——CSS篇
  8. 【38.02%】【codeforces 625B】War of the Corporations
  9. Android开发中的小技巧
  10. Linux下停Tomcat服务器,出现Connection refused错误解决办法