HBase java 的增删改查
. 目前来看好像是我的最全了,应该是大佬不屑于写这种的吧
. 其实本来想写很多方法的,但是写着写着就有点烦了,再加上本人也不全是闲人一个,然后就给停了,看着有点乱,但是办法都给了备注了,所以不是很影响阅读体验
. 如果写的有错误或者说有更好的,还望大佬不吝指教
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
public class HBaseFactory {
//创建配置对象
Configuration entries = HBaseConfiguration.create();
//获取hbase连接对象
Connection connection;
//2、获取操作对象
Admin admin;
HTableDescriptor hTableDescriptor;
HTableDescriptor hDescriptor1;
int sumber = 0, answer = 0, sum = 0;
String strName;
public static void main(String[] args) {
try {
System.out.println("程序正在运行。。。");
HBaseFactory hBaseFactory = new HBaseFactory();
hBaseFactory.connectionHBase();
hBaseFactory.deleteData("test:test001",
"1017",
"aaa",
"name");
// hBaseFactory.insertData("test:test001",
// "1017",
// "aaa",
// "name",
// "yongren");
// hBaseFactory.selectColimnFamily("test:test001", "aaa");
// hBaseFactory.deleteColimnFamily("test:test001","ccc");
// hBaseFactory.setOldColimnFamily("test:test001","abcd","ccc");
// hBaseFactory.addColimnFamily("test:test001","cccc");
// hBaseFactory.creatTable("test:test002","abc");
// hBaseFactory.deleteTable("test:test002");
// hBaseFactory.selectTable("test:test001","1001");
// hBaseFactory.offIO();
// hBaseFactory.selectTable("test:test001","1001");
// hBaseFactory.haveNamespace("test");
// hBaseFactory.createNamespace("demo");
} catch (Exception e) {
e.printStackTrace();
new HBaseFactory().isAnswer();
}
}
public void answer() {
answer = 1;
}
public void isAnswer() {
if (answer == 0) offIO();
else flushIO(strName);
}
/**
* 连接
*/
public void connectionHBase() {
if (sum == 0) {
//获取Hbase的连接
entries.set("hbase.zookeeper.quorum", "test01,test02,test03");
try {
connection = ConnectionFactory.createConnection(entries);
//获取操作对象
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
sum = 0;
}
public void connectionHBase(String values) {
//获取Hbase的连接
entries.set("hbase.zookeeper.quorum", values);
strName = values;
try {
connection = ConnectionFactory.createConnection(entries);
//获取操作对象
admin = connection.getAdmin();
sumber = 1;
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 关闭流
*/
public void offIO() {
try {
admin.close();
connection.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
/**
* 刷新流
* @param tableName 表名
*/
public void flushIO(String tableName) {
try {
admin.flush(TableName.valueOf(tableName));
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 是否有该命名空间
* @param name
* @return
*/
public boolean haveNamespace(String name) {
int sum = 0;
try {
NamespaceDescriptor[] namespaceDescriptors = admin.listNamespaceDescriptors();
for (NamespaceDescriptor namespaceDescriptor : namespaceDescriptors) {
String[] split = namespaceDescriptor.toString().split("'");
if (split[1].equals(name)) {
sum = 1;
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
return sum == 1;
}
/**
* 创建命名空间
* @param namespace 命名空间
*/
public void createNamespace(String namespace) {
if (haveNamespace(namespace)) {
System.out.println("已有该命名空间!\n创建失败!!");
} else
try {
admin.createNamespace(NamespaceDescriptor.create(namespace).build());
System.out.println("创建成功!!");
} catch (IOException e) {
e.printStackTrace();
}
offIO();
}
/**
* 删除命名空间
* @param namespace 命名空间
*/
public void deleteNamespace(String namespace) {
if (haveNamespace(namespace))
System.out.println("没有该命名空间!");
else
try {
admin.deleteNamespace(namespace);
System.out.println("删除成功!!");
} catch (IOException e) {
e.printStackTrace();
}
offIO();
}
/**
* 判断是否有该表
* @return 是否有该表
* @tableName 表名
*/
public boolean haveTbale(String tableName) {
//表描述对象,用于指定表名和列族
hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
String[] split = tableName.split(":");
if (haveNamespace(split[0])) {
TableName tableName1 = TableName.valueOf(tableName);
try {
if (admin.tableExists(tableName1)) {
this.strName = tableName;
if (admin.isTableDisabled(TableName.valueOf(tableName)))
admin.enableTable(TableName.valueOf(tableName));
return true;
}
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("没有该命名空间!!");
}
return false;
}
/**
* 创建表 tableName "命名空间:表名",列族名
*/
public void creatTable(String name, String familyName) {
if (haveTbale(name)) {
System.out.println("已经有该表了!\n创建失败!!");
} else {
//列族对象
HColumnDescriptor hd = new HColumnDescriptor(familyName);
//整合
hTableDescriptor.addFamily(hd);
//创建表
try {
admin.createTable(hTableDescriptor);
System.out.println("创建成功!!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 删除表
*
* @param tableName 表名
*/
public void deleteTable(String tableName) {
if (haveTbale(tableName)) {
try {
//解除表
admin.disableTable(TableName.valueOf(tableName));
//删除表
admin.deleteTable(TableName.valueOf(tableName));
System.out.println("删除成功!!");
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("没有该表!\n删除失败!!");
}
}
/**
* 根据表名查询数据
* @param tableName
* @return 该表数据
*/
public ResultScanner selectTable(String tableName){
if (haveTbale(tableName)){
try {
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
ResultScanner results = table.getScanner(scan);
// for (Result result : results) {
// Cell[] cells = result.rawCells();
// for (Cell cell : cells) {
// System.out.println("rowkey = " + Bytes.toString(CellUtil.cloneRow(cell)));
// System.out.println("family = " + Bytes.toString(CellUtil.cloneFamily(cell)));
// System.out.println("column = " + Bytes.toString(CellUtil.cloneQualifier(cell)));
// System.out.println("value = " + Bytes.toString(CellUtil.cloneValue(cell)));
// }
// }
return results;
} catch (IOException e) {
e.printStackTrace();
}
}else{
System.out.println("没有该表!!");
}
return null;
}
/**
* 是否有该rowKey
* @param tableName 表名
* @param rowKey rowKey
* @return 是否有该rowKey
*/
public boolean haveRowKey(String tableName,String rowKey){
ResultScanner results = selectTable(tableName);
try {
Table table = connection.getTable(TableName.valueOf(tableName));
for (Result result : table.getScanner(new Scan())) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
if (Bytes.toString(CellUtil.cloneRow(cell)).equals(rowKey))
return true;
}
}
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
/**
* 根据表名和rowkey查询数据
* @param tableName 表名
* @return Cell[] 表的内容
*/
public Cell[] selectTable(String tableName, String rowkey) {
if (haveTbale(tableName)) {
try {
TableName name = TableName.valueOf(tableName);
Table table = connection.getTable(name);
//字节编码转换
Get get = new Get(rowkey.getBytes());
//查询结果
Result result = table.get(get);
//判断是否为空
if (result.isEmpty()) {
System.out.println("该表为空!!");
} else {
//展示数据
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.println("rowkey = " + Bytes.toString(CellUtil.cloneRow(cell)));
System.out.println("family = " + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("column = " + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("value = " + Bytes.toString(CellUtil.cloneValue(cell)));
}
return cells;
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 确定是否有该列族
*/
public boolean haveColimnFamily(String name, String familyName) {
if (haveTbale(name)) {
try {
HTableDescriptor hDescriptor1 = admin.getTableDescriptor(TableName.valueOf(name));
HColumnDescriptor[] columnFamilies = hDescriptor1.getColumnFamilies();
for (HColumnDescriptor columnFamily : columnFamilies) {
String[] splits = columnFamily.toString().split("'");
if (familyName.equals(splits[1])) {
return true;
}
}
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("没有该表!!!");
}
return false;
}
/**
* 创建列族
* @param name 表名
* @param familyName 列族名
*/
public void createColimnFamily(String name, String familyName) {
if (haveColimnFamily(name, familyName)) {
System.out.println("已有该列!!");
} else {
TableName tablename1 = TableName.valueOf(name);
try {
admin.disableTable(tablename1);
hDescriptor1 = admin.getTableDescriptor(tablename1);
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(familyName);
hDescriptor1.addFamily(hColumnDescriptor);
admin.modifyTable(tablename1, hDescriptor1);
System.out.println("创建成功!!");
} catch (IOException e) {
e.printStackTrace();
try {
admin.enableTable(tablename1);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
/**
* 删除列族
* @param name 表名
* @param familyName 列族名
*/
public void deleteColimnFamily(String name, String familyName) {
if (haveColimnFamily(name, familyName)) {
try {
admin.deleteColumn(TableName.valueOf(name), familyName.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("没有该列族!!");
}
}
/**
* 修改列族(本质是将原列族复制到新列族,时间较长)
* @param tablename 表名
* @param oldCFName 旧列族名
* @param newCFName 新列族名
*/
public void setOldColimnFamily(String tablename, String oldCFName, String newCFName) {
if (haveColimnFamily(tablename, oldCFName)) {
if (haveColimnFamily(tablename, newCFName)) {
System.out.println("已有该列族!!");
return;
}
try {
//得到表的操作对象
HTableDescriptor hDescriptor1 = admin.getTableDescriptor(TableName.valueOf(tablename));
//得到表的详细信息
HColumnDescriptor[] columnFamilies = hDescriptor1.getColumnFamilies();
HColumnDescriptor existingColumn = null;
for (HColumnDescriptor columnFamily : columnFamilies) {
String[] splits = columnFamily.toString().split("'");
if (splits[1].equals(oldCFName)) {
//代替列族
existingColumn = new HColumnDescriptor(newCFName.getBytes(),
columnFamily.getMinVersions(),
columnFamily.getMaxVersions(),
columnFamily.getKeepDeletedCells(),
columnFamily.getCompression().toString(),
true,
columnFamily.getDataBlockEncoding().toString(),
columnFamily.isInMemory(),
columnFamily.isBlockCacheEnabled(),
columnFamily.getBlocksize(),
columnFamily.getTimeToLive(),
columnFamily.getBloomFilterType().toString(),
columnFamily.getScope());
admin.disableTable(TableName.valueOf(tablename));
hDescriptor1.addFamily(existingColumn);
admin.modifyTable(TableName.valueOf(tablename), hDescriptor1);
flushIO(tablename);
//复制该列族的数据
ResultScanner results = selectTable(tablename);
for (Result result : results) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
if (Bytes.toString(CellUtil.cloneRow(cell)).equals(oldCFName)) {
insertData(tablename,
Bytes.toString(CellUtil.cloneRow(cell)),
Bytes.toString(CellUtil.cloneFamily(cell)),
Bytes.toString(CellUtil.cloneQualifier(cell)),
Bytes.toString(CellUtil.cloneValue(cell)));
// System.out.println("rowkey = " + Bytes.toString(CellUtil.cloneRow(cell)));
// System.out.println("family = " + Bytes.toString(CellUtil.cloneFamily(cell)));
// System.out.println("column = " + Bytes.toString(CellUtil.cloneQualifier(cell)));
// System.out.println("value = " + Bytes.toString(CellUtil.cloneValue(cell)));
}
}
}
deleteColimnFamily(tablename, oldCFName);
}
}
if (existingColumn != null) {
System.out.println("修改成功!");
} else {
System.out.println("傻了吧你?都没有这个列族!");
}
} catch (IOException e) {
e.printStackTrace();
try {
if (admin.isTableDisabled(TableName.valueOf(tablename)))
admin.enableTable(TableName.valueOf(tablename));
flushIO(tablename);
} catch (IOException ex) {
ex.printStackTrace();
}
}
} else {
System.out.println("傻仔,都没有这个列族!");
}
}
/**
* 查看有该列族数据信息
* @param name 表名
* @param CFName 列族名
* @return 包含有该列族的信息数组
* 注意 :列族信息为空时,请谨慎使用
*/
public Cell[] selectColimnFamily(String name, String CFName) {
if (haveColimnFamily(name, CFName)) {
LinkedList<Object> list = new LinkedList<Object>();
Cell[] ces = new Cell[1];
ResultScanner results = selectTable(name);
for (Result result : results) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.println("rowkey -- > " + Bytes.toString(CellUtil.cloneRow(cell)) + "\t" +
"family -- > " + Bytes.toString(CellUtil.cloneFamily(cell)) + "\n" +
"column -- > " + Bytes.toString(CellUtil.cloneQualifier(cell)) + "\t" +
"value -- > " + Bytes.toString(CellUtil.cloneValue(cell)));
System.out.println("-------------------------------------");
if (CFName.equals(Bytes.toString(CellUtil.cloneFamily(cell)))) {
list.add(cell);
}
}
}
ces = new Cell[list.size()];
int num = 0;
for (Object o : list) {
ces[num++] = (Cell) o;
}
return ces;
} else {
System.out.println("没有该列族!!");
}
return null;
}
/**
* 判断是否有该列
* @param tableName 表名
* @param column 列名
* @return 是否有该列
*/
public boolean haveColumn(String tableName,String column){
ResultScanner results = selectTable(tableName);
try {
Table table = connection.getTable(TableName.valueOf(tableName));
for (Result result : table.getScanner(new Scan())) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
if (Bytes.toString(CellUtil.cloneQualifier(cell)).equals(column))
return true;
}
}
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
/**
* 添加数据
* @param tableName 表名
* @param rowkey rowKey
* @param family 列族
* @param column 列
* @param value 值
*/
public void insertData(String tableName,String rowkey,String family,String column,String value) {
if (haveTbale(tableName)){
if (haveRowKey(tableName,rowkey)){
System.out.println("该rowKey已存在!!");
}else {
Table table;
try {
table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(Bytes.toBytes(rowkey));
put.addColumn(//添加数据
Bytes.toBytes(family),
Bytes.toBytes(column),
Bytes.toBytes(value));
//用指定表对象添加数据
table.put(put);
System.out.println("数据增加成功.....");
} catch (IOException e) {
e.printStackTrace();
}
}
}else {
System.out.println("没有该表!!");
}
}
/**
* 删除数据
* @param tableName 表名
* @param rowKey rowKey
* @param family 列族
* @param column 列
* @throws IOException
*/
public void deleteData(String tableName,String rowKey,String family,String column) throws IOException {
if (haveTbale(tableName)){
if (haveRowKey(tableName,rowKey)){
Table table = connection.getTable(TableName.valueOf(tableName));
//1. 创建集合用于批量删除
// ArrayList<Delete> dels = new ArrayList<Delete>();
//2. 创建删除数据对象
Delete del = new Delete(Bytes.toBytes(rowKey));
del.addColumn(Bytes.toBytes(family),Bytes.toBytes(column));
//3. 添加到集合
// dels.add(del);
//4. 提交
table.delete(del);
System.out.println("删除完成0.0");
}else {
System.out.println("没有该rowKey!!");
}
}else {
System.out.println("没有该表!!");
}
}
}