Hbase shell操作
命名 |
描述 |
语法 |
help ‘命令名’ |
查看命令的使用描述 |
help ‘命令名’ |
whoami |
我是谁 |
whoami |
version |
返回hbase版本信息 |
version |
status |
返回hbase集群的状态信息 |
status |
table_help |
查看如何操作表 |
table_help |
create |
创建表 |
create ‘表名’, ‘列族名1’, ‘列族名2’, ‘列族名N’ |
alter |
修改列族 |
添加一个列族:alter ‘表名’, ‘列族名’删除列族:alter ‘表名’, {NAME=> ‘列族名’, METHOD=> ‘delete’} |
describe |
显示表相关的详细信息 |
describe ‘表名’ |
list |
列出hbase中存在的所有表 |
list |
exists |
测试表是否存在 |
exists ‘表名’ |
put |
添加或修改的表的值 |
put ‘表名’, ‘行键’, ‘列族名’, ‘列值’put ‘表名’, ‘行键’, ‘列族名:列名’, ‘列值’ |
scan |
通过对表的扫描来获取对用的值 |
scan ‘表名 扫描某个列族: scan ‘表名’, {COLUMN=>‘列族名’}扫描某个列族的某个列: scan ‘表名’, {COLUMN=>‘列族名:列名’}查询同一个列族的多个列: scan ‘表名’, {COLUMNS => [ ‘列族名1:列名1’, ‘列族名1:列名2’, …]} |
get |
获取行或单元(cell)的值 |
get ‘表名’, ‘行键’get ‘表名’, ‘行键’, ‘列族名’ |
count |
统计表中行的数量 |
count ‘表名’ |
incr |
增加指定表行或列的值 |
incr ‘表名’, ‘行键’, ‘列族:列名’, 步长值 |
get_counter |
获取计数器 |
get_counter ‘表名’, ‘行键’, ‘列族:列名’ |
delete |
删除指定对象的值(可以为表,行,列对应的值,另外也可以指定时间戳的值) |
删除列族的某个列: delete ‘表名’, ‘行键’, ‘列族名:列名’ |
deleteall |
删除指定行的所有元素值 |
deleteall ‘表名’, ‘行键’ |
truncate |
重新创建指定表 |
truncate ‘表名’ |
enable |
使表有效 |
enable ‘表名’ |
is_enabled |
是否启用 |
is_enabled ‘表名’ |
disable |
使表无效 |
disable ‘表名’ |
is_disabled |
是否无效 |
is_disabled ‘表名’ |
drop |
删除表 |
drop的表必须是disable的 disable ‘表名’ drop ‘表名’ |
shutdown |
关闭hbase集群(与exit不同) |
|
tools |
列出hbase所支持的工具 |
|
exit |
退出hbase shell |
|
Hbase api 操作
package cn.itcast.hbase.admin.api_test;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Test;
import java.io.IOException;
import java.util.List;
/**
* @author noor9
* @date 2020-10-18-22:49
*/
public class TableTest {
private static Connection connection;
private static Admin admin;
//获取连接
public static void getConn() throws IOException {
Configuration configuration = HBaseConfiguration.create();
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
}
//关闭连接
public static void closeConn(){
try{
if(admin != null){
admin.close();
}
if(null != connection){
connection.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
//创建表 fields 存放列簇
public static void createTable(String tableName , String[] fields) throws IOException {
getConn();
TableName TableName1 = TableName.valueOf(tableName);
if (admin.tableExists(TableName1))
{
System.out.println("表已经存在");
deleteTable(tableName);
TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName1);
for (String field : fields) {
ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(field));
ColumnFamilyDescriptor cfDes = columnFamilyDescriptorBuilder.build();
tableDescriptorBuilder.setColumnFamily(cfDes);
}
TableDescriptor tableDescriptor = tableDescriptorBuilder.build();
admin.createTable(tableDescriptor);
}
else {
TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName1);
for (String field : fields) {
ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(field));
ColumnFamilyDescriptor cfDes = columnFamilyDescriptorBuilder.build();
tableDescriptorBuilder.setColumnFamily(cfDes);
}
TableDescriptor tableDescriptor = tableDescriptorBuilder.build();
admin.createTable(tableDescriptor);
System.out.println("创建成功");
}
closeConn();
}
//添加数据
public static void addRecord(String tableName ,String row , String[] fields ,String[] values) throws IOException {
getConn();
TableName tableName1 = TableName.valueOf(tableName);
if(!admin.tableExists(tableName1))
{
System.err.println("the table "+tableName+" is not exist");
System.exit(1);
}
else{
Table table = connection.getTable(tableName1);
for (int i = 0 ; i!=fields.length ; i++)
{
Put put = new Put(Bytes.toBytes(row));
String[] cols = fields[i].split(":");
put.addColumn(Bytes.toBytes(cols[0]),Bytes.toBytes(cols[1]),Bytes.toBytes(values[i]));
table.put(put);
}
table.close();
}
closeConn();
}
//查找
public static void scanColumn(String tableName , String rowkey , String column) throws IOException {
getConn();
TableName tableName1 = TableName.valueOf(tableName);
Table table = connection.getTable(tableName1);
if(!column.contains(":")){
Get get = new Get(Bytes.toBytes(rowkey));
Result result = table.get(get);
// 列出所有单元格
List<Cell> cells = result.listCells();
// 打印rowkey
byte[] row = result.getRow();
System.out.println(Bytes.toString(row));
// 迭代单元格列表
for (Cell cell : cells) {
//获取列簇名称
String cf = Bytes.toString(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
//获取列的名称
String columnName = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
//获取值
String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
if(value=="")
System.out.println(cf+":"+columnName+"->"+null);
else
System.out.println(cf+":"+columnName+"->"+value);
}
}else{
Get get = new Get(Bytes.toBytes(rowkey));
String family= column.split(":")[0];
String qualifier = column.split(":")[1];
get.addColumn(Bytes.toBytes(family),Bytes.toBytes(qualifier));
Result result = table.get(get);
String resultValue =new String(result.getValue(Bytes.toBytes(family), Bytes.toBytes(qualifier)));
if(resultValue=="")
System.out.println(family+":"+qualifier+"->"+null);
else
System.out.println(family+":"+qualifier+"->"+resultValue.toString());
}
table.close();
closeConn();
}
//修改值
public static void modifyData(String tableName, String rowkey, String column , String value) throws IOException {
getConn();
TableName tableName1 = TableName.valueOf(tableName);
Table table = connection.getTable(tableName1);
Put put = new Put(Bytes.toBytes(rowkey));
String family = column.split(":")[0];
String qualifier = column.split(":")[1];
//修改
put.addColumn(Bytes.toBytes(family),Bytes.toBytes(qualifier),Bytes.toBytes(value));
table.put(put);
//比对
Get get = new Get(Bytes.toBytes(rowkey));
get.addColumn(Bytes.toBytes(family),Bytes.toBytes(qualifier));
Result result = table.get(get);
String resultValue =new String(result.getValue(Bytes.toBytes(family), Bytes.toBytes(qualifier)));
if(resultValue.equals(value))
System.out.println("修改成功");
table.close();
closeConn();
}
//删除表
public static void deleteTable(String tableName) throws IOException {
getConn();
TableName tableName1 = TableName.valueOf(tableName);
if(admin.tableExists(tableName1))
{
admin.disableTable(tableName1);
admin.deleteTable(tableName1);
}
System.out.println("删除表成功");
}
//删除行
public static void deleteRow(String tableName ,String row) throws IOException {
getConn();
if(!admin.tableExists(TableName.valueOf(tableName)))
{
System.err.println("the table "+tableName+" is not exist");
System.exit(1);
}
Table table = connection.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(Bytes.toBytes(row));//rowkey
table.delete(delete);
System.out.println("删除成功");
table.close();
closeConn();
}
//根据表名查询所有的rowkey
public static void getRowKey(String tableName) throws IOException {
getConn();
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
ResultScanner results = table.getScanner(scan);
for (Result result : results) {
byte[] row = result.getRow();
System.out.println("标记");
System.out.println(new String(row));
}
closeConn();
}
public static void main(String[] args) throws IOException {
String tableName = "Student";
String tableName2 = "Course";
String tableName3 = "SC";
//1.第一个
/*String[] fields = {"H1","H2","H3"};
createTable(tableName,fields);*/
//第二个
/*String row = "Zhangsan";
String[] fields ={"H1:S_No", "H1:S_Name", "H1:S_Sex","H1:S_Age"};
String[] values={"2015001","Zhangsan","male","23"};
addRecord(tableName,row,fields,values);*/
//第三个
// canColumn(tableName,"Zhangsan","H1:S_Age");
//第四个 modifyData(String tableName, String row, String column)
//modifyData(tableName , "Zhangsan" , "H1:S_Age","21");
//第五个
//deleteRow(tableName , "Zhangsan");
//getRowKey(tableName);
}
}