package yqq.study;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
/**
* @Author yqq
* @Date 2021/11/15 15:27
* @Version 1.0
*/
public class HBaseDemo {
//声明表的管理类
private HBaseAdmin admin = null;
//声明表的数据的CRUD的类对象
private HTable table = null;
//表明名
private String tableName = "phone";
public void init() throws IOException {
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum","node2,node3,node4");
//实例化admin对象
admin = new HBaseAdmin(conf);
//实例化table对象
table = new HTable(conf,tableName.getBytes());
}
public void close() throws IOException {
if(table!=null)
table.close();
if(admin!=null)
admin.close();
}
/**
* 创建表
*/
public void createTable() throws IOException {
//定义表描述符对象
HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName));
//创建表时需要制定列族,所以定义列族描述符
HColumnDescriptor famliy = new HColumnDescriptor("cf".getBytes());
//将列族的描述符对象添加到表描述符中
desc.addFamily(famliy);
//判断表是否存在,如果存在,删除之前的表
if(admin.tableExists(tableName)){
//先禁用表
admin.disableTable(tableName);
//删除表
admin.deleteTable(tableName);
}
admin.createTable(desc);
}
}
hbase(main):002:0> list
TABLE
phone
test
test1
3 row(s) in 0.0370 seconds
=> ["phone", "test", "test1"]
public void insert() throws InterruptedIOException, RetriesExhaustedWithDetailsException {
//创建Put对象
Put put = new Put("rk111".getBytes());
put.add("cf".getBytes(),"name".getBytes(),"yqq".getBytes());
put.add("cf".getBytes(),"age".getBytes(),"20".getBytes());
put.add("cf".getBytes(),"sex".getBytes(),"man".getBytes());
//执行put插入操作
table.put(put);
}
hbase(main):003:0> scan "phone"
ROW COLUMN+CELL
rk111 column=cf:age, timestamp=1636971270134, value=20
rk111 column=cf:name, timestamp=1636971270134, value=yqq
rk111 column=cf:sex, timestamp=1636971270134, value=man
1 row(s) in 0.4770 seconds
/**
* 根据rowkey的值获取他所对应的的值
*/
public void get() throws IOException {
//创建Get对象
Get get = new Get("rk111".getBytes());
//添加列过滤,只查询需要的列,减少网络传输的量,减少磁盘的io次数
get.addColumn("cf".getBytes(),"name".getBytes());
get.addColumn("cf".getBytes(),"age".getBytes());
get.addColumn("cf".getBytes(),"sex".getBytes());
//调用查询方法 select * from phone
Result result = table.get(get);
//从result中获取数据
Cell nameCell = result.getColumnLatestCell("cf".getBytes(),"name".getBytes());
Cell ageCell = result.getColumnLatestCell("cf".getBytes(), "age".getBytes());
Cell sexCell = result.getColumnLatestCell("cf".getBytes(), "sex".getBytes());
System.out.print(Bytes.toString(CellUtil.cloneValue(nameCell))+"\t");
System.out.print(Bytes.toString(CellUtil.cloneValue(ageCell))+"\t");
System.out.print(Bytes.toString(CellUtil.cloneValue(sexCell)));
}
/**
* 查询多行数据
*/
public void scan() throws IOException {
Scan scan = new Scan();
//查询表中全部数据(全部行,全部列)
ResultScanner resultScanner = table.getScanner(scan);
for (Result result:resultScanner){
Cell nameCell = result.getColumnLatestCell("cf".getBytes(),"name".getBytes());
Cell ageCell = result.getColumnLatestCell("cf".getBytes(),"age".getBytes());
Cell sexCell = result.getColumnLatestCell("cf".getBytes(),"sex".getBytes());
System.out.print(Bytes.toString(CellUtil.cloneValue(nameCell))+"\t");
System.out.print(Bytes.toString(CellUtil.cloneValue(ageCell))+"\t");
System.out.println(Bytes.toString(CellUtil.cloneValue(sexCell))+"\t");
}
}
hbase(main):014:0> scan "phone"
ROW COLUMN+CELL
rk111 column=cf:age, timestamp=1636973404628, value=20
rk111 column=cf:name, timestamp=1636973404628, value=yqq
rk111 column=cf:sex, timestamp=1636973404628, value=man
rk112 column=cf:age, timestamp=1636973427738, value=20
rk112 column=cf:name, timestamp=1636973427738, value=wt
rk112 column=cf:sex, timestamp=1636973427738, value=man
rk113 column=cf:age, timestamp=1636973446974, value=20
rk113 column=cf:name, timestamp=1636973446974, value=lwl
rk113 column=cf:sex, timestamp=1636973446974, value=man
rk114 column=cf:age, timestamp=1636973469295, value=20
rk114 column=cf:name, timestamp=1636973469295, value=db
rk114 column=cf:sex, timestamp=1636973469295, value=man
4 row(s) in 0.0840 seconds
/**
* 查询指定rowkey查询范围[startRow,stopRow)
*/
public void scan() throws IOException {
Scan scan = new Scan();
//指定rowkey查询范围[startRow,stopRow)
scan.setStartRow("rk112".getBytes());
scan.setStopRow("rk114".getBytes());
//指定查询的列
scan.addColumn("cf".getBytes(),"name".getBytes());
scan.addColumn("cf".getBytes(),"age".getBytes());
scan.addColumn("cf".getBytes(),"sex".getBytes());
//查询表中全部数据(指定的rowkey的行,指定的列)
ResultScanner resultScanner = table.getScanner(scan);
for (Result result:resultScanner){
Cell nameCell = result.getColumnLatestCell("cf".getBytes(),"name".getBytes());
Cell ageCell = result.getColumnLatestCell("cf".getBytes(),"age".getBytes());
Cell sexCell = result.getColumnLatestCell("cf".getBytes(),"sex".getBytes());
System.out.print(Bytes.toString(CellUtil.cloneValue(nameCell))+"\t");
System.out.print(Bytes.toString(CellUtil.cloneValue(ageCell))+"\t");
System.out.println(Bytes.toString(CellUtil.cloneValue(sexCell))+"\t");
}
}
/**
* 为存在表添加列族
* @throws IOException
*/
public void addFamliy() throws IOException {
//获取tableName对象
TableName tableNameObj = TableName.valueOf(tableName);
//禁用表phone
admin.disableTable(tableNameObj);
//添加列族
//获取表上的描述符
HTableDescriptor tableDescriptor = admin.getTableDescriptor(tableNameObj);
//创建列族cf2描述符对象
HColumnDescriptor famliy = new HColumnDescriptor("cf2".getBytes());
//将famliy添加到tableDescriptor对象上
tableDescriptor.addFamily(famliy);
//执行列族的添加操作:表结构的修改
admin.modifyTable(tableName,tableDescriptor);
//将表从禁用状态改为可用状态
admin.enableTable(tableNameObj);
}
hbase(main):002:0> describe "phone"
Table phone is ENABLED
phone
COLUMN FAMILIES DESCRIPTION
{NAME => 'cf', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION =>
'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}
{NAME => 'cf2', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION =>
'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}
2 row(s) in 0.3080 seconds
/**
* 根据需求删除数据
*/
public void delete() throws IOException {
//创建Delete对象,指定rowkey
Delete delete = new Delete("rk112".getBytes());
//指定删除的列族
// delete.deleteFamily("cf".getBytes());
//指定删除rowkey对应一行数据中的某一列
delete.deleteColumn("cf2".getBytes(),"phone_num".getBytes());
//删除rowkey对应一行的的数据中列族cf1下所有数据
table.delete(delete);
}
hbase(main):010:0> scan 'phone'
ROW COLUMN+CELL
rk112 column=cf2:phone_num, timestamp=1637029441291, value=18224667889
rk113 column=cf:age, timestamp=1636973446974, value=20
rk113 column=cf:name, timestamp=1636973446974, value=lwl
rk113 column=cf:sex, timestamp=1636973446974, value=man
rk114 column=cf:age, timestamp=1636973469295, value=20
rk114 column=cf:name, timestamp=1636973469295, value=db
rk114 column=cf:sex, timestamp=1636973469295, value=man
3 row(s) in 0.0340 seconds
hbase(main):011:0> scan 'phone'
ROW COLUMN+CELL
rk113 column=cf:age, timestamp=1636973446974, value=20
rk113 column=cf:name, timestamp=1636973446974, value=lwl
rk113 column=cf:sex, timestamp=1636973446974, value=man
rk114 column=cf:age, timestamp=1636973469295, value=20
rk114 column=cf:name, timestamp=1636973469295, value=db
rk114 column=cf:sex, timestamp=1636973469295, value=man
2 row(s) in 0.0450 seconds