public class HbaseTest {
//注意要配host,我第一次没配出现了RetriesExhaustedException: Can't get the locations
public static Configuration conf;
//获取配置信息
static {
conf = HBaseConfiguration.create();
}
//1.查看表是否存在
public static boolean isExist(String tablename) throws IOException {
//获得连接
Connection connect = ConnectionFactory.createConnection(conf);
//管理表
HBaseAdmin admin = (HBaseAdmin) connect.getAdmin();
return admin.tableExists(TableName.valueOf(tablename));
}
//2.创建表
public static boolean create_hbasetable(String tablename,String... columnFamilly) throws IOException {
//先判断表是否存在
if(isExist(tablename)){
return false;
}
//获取连接
Connection connect = ConnectionFactory.createConnection(conf);
//管理表
HBaseAdmin admin = (HBaseAdmin) connect.getAdmin();
//获取表描述器来创建列族
HTableDescriptor des = new HTableDescriptor(TableName.valueOf(tablename));
//添加列族
for (String familly: columnFamilly) {
des.addFamily(new HColumnDescriptor(familly));
}
//创建表
admin.createTable(des);
return true;
}
//3.删除表
public static void delete_hbasetable(String tablename) throws IOException {
Connection connect = ConnectionFactory.createConnection(conf);
HBaseAdmin admin = (HBaseAdmin) connect.getAdmin();
if(isExist(tablename)){
admin.disableTable(TableName.valueOf(tablename));
admin.deleteTable(TableName.valueOf(tablename));
}else {
System.out.println("表不存在,删除失败");
}
}
//4.添加数据put rowkey
public static void addrow(String tablename,String rowkey,String lie,String columns,String familly) throws IOException {
Connection connect = ConnectionFactory.createConnection(conf);
//获取表对象
Table table = connect.getTable(TableName.valueOf(tablename));
//1.用put的方式加入数据
Put put = new Put(Bytes.toBytes(rowkey));
//2.加入数据
put.addColumn(Bytes.toBytes(lie),Bytes.toBytes(columns),Bytes.toBytes(familly));
table.put(put);
}
//5.删除表中一行数据
public static void delete_row(String tablename,String rowkey) throws IOException {
Connection connect = ConnectionFactory.createConnection(conf);
Table t = connect.getTable(TableName.valueOf(tablename));
Delete d = new Delete(Bytes.toBytes(rowkey));
t.delete(d);
}
//6.删除多行数据
public static void deletes_row(String tablename,String... rowkey) throws IOException {
Connection connect = ConnectionFactory.createConnection(conf);
Table t = connect.getTable(TableName.valueOf(tablename));
List<Delete> list = new ArrayList<Delete>();
for (String rowk:rowkey){
Delete d = new Delete(Bytes.toBytes(rowk));
list.add(d);
}
t.delete(list);
}
//7.scan 表
public static void scan_hbasetable(String tablename) throws IOException {
Connection connect = ConnectionFactory.createConnection(conf);
Table t = connect.getTable(TableName.valueOf(tablename));
Scan s = new Scan();
ResultScanner sc = t.getScanner(s);
for (Result a : sc){
Cell[] cells = a.rawCells();
for (Cell c : cells){
System.out.println("行键为"+Bytes.toString(CellUtil.cloneRow(c)));
System.out.println("列为"+Bytes.toString(CellUtil.cloneFamily(c)));
System.out.println("值为"+Bytes.toString(CellUtil.cloneValue(c)));
}
}
}
//8.scan 从rowkey开始往后
public static void scan_hbaserowtables(String tablename , String rowkey) throws IOException {
Connection connect = ConnectionFactory.createConnection(conf);
Table table = connect.getTable(TableName.valueOf(tablename));
Scan scan = new Scan(Bytes.toBytes(rowkey));
//scan 'user',{STARTROW =>'101',STOPROW => '101'} 是可以进行限制的,我这里只给了一个参数,那就是从这里开始往后遍历
ResultScanner rs = table.getScanner(scan);
for (Result s : rs){
Cell [] cell= s.rawCells();
for (Cell c : cell){
System.out.println("行键为"+Bytes.toString(CellUtil.cloneRow(c)));
System.out.println("列为"+Bytes.toString(CellUtil.cloneFamily(c)));
System.out.println("值为"+Bytes.toString(CellUtil.cloneValue(c)));
}
}
}
//8.获取指定数据
public static void scan_hbasegetrowtables(String tablename, String rowkey) throws IOException {
Connection connect = ConnectionFactory.createConnection(conf);
Table table =connect.getTable(TableName.valueOf(tablename));
Get get = new Get(Bytes.toBytes(rowkey));
Result rs = table.get(get);
Cell [] cell = rs.rawCells();
for (Cell c : cell){
System.out.println("行键为"+Bytes.toString(CellUtil.cloneRow(c)));
System.out.println("列为"+Bytes.toString(CellUtil.cloneFamily(c)));
System.out.println("值为"+Bytes.toString(CellUtil.cloneValue(c)));
}
}
public static void main(String[] args) throws IOException {
scan_hbasegetrowtables("a","2");
// scan_hbaserowtables("a","1");
// scan_hbasetable("a");
// deletes_row("a","1","2");
// delete_row("a","1");
// addrow("a","1","lie1","age","90");
// System.out.println(isExist("b"));
// System.out.println(create_hbasetable("b","info1","info2"));
// delete_hbasetable("b");
}
}
Hbase API
最新推荐文章于 2021-11-14 20:33:00 发布