Hbase的命令行客户端操作(shell操作)
- 描述表:describe “t_user”
- 判断表是否存在:exists “t_user”
- 向表中添加数据:put “t_user”,”rk001”,”basic_info:name”,”huihui”
put “t_user”,”rk001”,”family:father”,”bingbing” - 更新记录:与put相同,追加不是覆盖
- 查看表中数据:get “t_user”,”rk001”
- 计算总条数: count “t_user”
- 获取某个列族:get “t_user”,”rk001”,”base_info”
- 获取某个列族中的列: get “t_user”,”rk001”,”base_info”,”name”
- 删除记录:delete “t_user”,”rk001”,”base_info”,”name”
- 删除整行:delete “t_user”,”rk001”
- 删除表:先将表设置为不可用,disable “t_user”
再删除,drop “t_user” - 清空表:truncate “t_user”
- 查看所有记录:scan “t_user”
Hbase的Java客户端操作(JavaAPI)
public class TableAdmin {
Connection connection = null;
Admin admin = null;
Table table = null;
@Before
public void info() throws Exception {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "hadoop01:2181,hadoop02:2181,hadoop03:2181");
connection = ConnectionFactory.createConnection(conf);
admin = connection.getAdmin();
}
/**
*
* 建表
*/
@Test
public void TestCreate() throws Exception {
HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf("t_user"));
HColumnDescriptor hColumnDescriptor1 = new HColumnDescriptor("base_info");
hColumnDescriptor1.setMaxVersions(3);
HColumnDescriptor hColumnDescriptor2 = new HColumnDescriptor("family");
hTableDescriptor.addFamily(hColumnDescriptor1);
hTableDescriptor.addFamily(hColumnDescriptor2);
admin.createTable(hTableDescriptor);
}
/**
* 删除表
* @throws Exception
*/
@Test
public void TestDelete() throws Exception {
admin.disableTable(TableName.valueOf("t_user"));
admin.deleteTable(TableName.valueOf("t_user"));
}
/**
* 向表中添加数据
* @throws Exception
*/
@Test
public void TestPutData() throws Exception {
table = connection.getTable(TableName.valueOf("t_user"));
Put put = new Put(Bytes.toBytes("007rk"));
put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("name"), Bytes.toBytes("huishu"));
put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("age"), Bytes.toBytes("11"));
Put put2 = new Put(Bytes.toBytes("008rk"));
put2.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("name"), Bytes.toBytes("shu"));
put2.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("age"), Bytes.toBytes("20"));
List<Put> list = new ArrayList<>();
list.add(put);
list.add(put2);
table.put(list);
}
/**
* 删除表中数据
* @throws Exception
* @throws Exception
*/
@Test
public void TestDeleteData() throws Exception {
table = connection.getTable(TableName.valueOf("t_user"));
Delete delete = new Delete(Bytes.toBytes("rk002"));
table.delete(delete);
}
/**
* 查询单行数据
* @throws Exception
* @throws Exception
*/
@Test
public void TestSelect() throws Exception {
table = connection.getTable(TableName.valueOf("t_user"));
Get get = new Get(Bytes.toBytes("rk002"));
Result result = table.get(get);
byte[] value = result.getValue(Bytes.toBytes("family"), Bytes.toBytes("mather"));
System.out.println(Bytes.toString(value));
byte[] value2 = result.getValue(Bytes.toBytes("family"), Bytes.toBytes("father"));
System.out.println(new String(value2));
}
/**
* 多行查询
* @throws Exception
* @throws Exception
*/
@Test
public void TestSelects() throws Exception {
table = connection.getTable(TableName.valueOf("t_user"));
Scan scan = new Scan();
scan.setStartRow(Bytes.toBytes("rk003"));
scan.setStopRow(Bytes.toBytes("rk005"));
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
System.out.print(new String(result.getRow()) + " ");
byte[] value = result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("name"));
System.out.println(Bytes.toString(value));
byte[] value2 = result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("age"));
System.out.println(new String(value2));
System.out.println("-----------------------------");
}
}
/**
* 列值过滤器
* @throws Exception
* @throws Exception
*/
@Test
public void SingleColumnValueFilter() throws Exception {
table = connection.getTable(TableName.valueOf("t_user"));
Scan scan = new Scan();
SingleColumnValueFilter f = new SingleColumnValueFilter(Bytes.toBytes("base_info"), Bytes.toBytes("name"), CompareOp.EQUAL, Bytes.toBytes("shu"));
scan.setFilter(f);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
System.out.print(new String(result.getRow()) + " ");
byte[] value = result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("name"));
System.out.println(Bytes.toString(value));
byte[] value2 = result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("age"));
System.out.println(new String(value2));
System.out.println("-----------------------------");
}
}
/**
* 列名前缀过滤器
* @throws Exception
* @throws Exception
*/
@Test
public void ColumnPrefixFilter1() throws Exception {
table = connection.getTable(TableName.valueOf("t_user"));
Scan scan = new Scan();
ColumnPrefixFilter f = new ColumnPrefixFilter(Bytes.toBytes("name"));
scan.setFilter(f);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
System.out.print(new String(result.getRow()) + " ");
byte[] value = result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("name"));
System.out.println(Bytes.toString(value));
System.out.println("-----------------------------");
}
}
/**
* rowKey过滤器
* @throws Exception
* @throws Exception
*/
@Test
public void row() throws Exception {
table = connection.getTable(TableName.valueOf("t_user"));
Scan scan = new Scan();
RowFilter f = new RowFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator("^rk"));
scan.setFilter(f);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
System.out.print(new String(result.getRow()) + " ");
byte[] value = result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("name"));
System.out.println(Bytes.toString(value));
byte[] value2 = result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("age"));
System.out.println(new String(value2));
System.out.println("-----------------------------");
}
}
/**
* FilterList过滤器列表
* @throws Exception
* @throws Exception
*/
@Test
public void FilterByList() throws Exception {
table = connection.getTable(TableName.valueOf("t_user"));
Scan scan = new Scan();
FilterList list = new FilterList(Operator.MUST_PASS_ALL);
SingleColumnValueFilter f = new SingleColumnValueFilter(Bytes.toBytes("base_info"), Bytes.toBytes("name"), CompareOp.EQUAL, Bytes.toBytes("shu"));
RowFilter f1 = new RowFilter(CompareFilter.CompareOp.EQUAL, new RegexStringComparator("^rk"));
list.addFilter(f);
list.addFilter(f1);
scan.setFilter(list);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
System.out.print(new String(result.getRow()) + " ");
byte[] value = result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("name"));
System.out.println(Bytes.toString(value));
byte[] value2 = result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("age"));
System.out.println(new String(value2));
System.out.println("-----------------------------");
}
}
@After
public void close() throws Exception {
table.close();
admin.close();
connection.close();
}
}