HBase基础
基本操作
- 创建表
hbase:008:0> create 'student', 'info'
- 插入数据
hbase:012:0> put 'student','1001','info:sex','male'
Took 0.0165 seconds
hbase:013:0> put 'student', '1001', 'info:age', '18'
Took 0.0152 seconds
hbase:014:0> put 'student', '1002', 'info:name', 'zhangsan'
Took 0.0053 seconds
hbase:015:0> put 'student', '1002', 'info:sex', 'female'
Took 0.0102 seconds
hbase:016:0> put 'student', '1002', 'info:age', '20'
Took 0.0107 seconds
- 扫描查看表数据
hbase:018:0> scan 'student'
ROW COLUMN+CELL
1001 column=info:age, timestamp=2021-08-02T16:54:33.093, value=18
1001 column=info:sex, timestamp=2021-08-02T16:53:54.575, value=male
1002 column=info:age, timestamp=2021-08-02T16:55:22.819, value=20
1002 column=info:name, timestamp=2021-08-02T16:54:53.701, value=zhangsan
1002 column=info:sex, timestamp=2021-08-02T16:55:10.711, value=female
2 row(s)
Took 0.0169 seconds
hbase:021:0> scan 'student', {STARTROW => '1001', STOPROW => '1001'}
ROW COLUMN+CELL
1001 column=info:age, timestamp=2021-08-02T16:54:33.093, value=18
1001 column=info:sex, timestamp=2021-08-02T16:53:54.575, value=male
1 row(s)
Took 0.0265 seconds
hbase:023:0> scan 'student', {STARTROW => '1002'}
ROW COLUMN+CELL
1002 column=info:age, timestamp=2021-08-02T16:55:22.819, value=20
1002 column=info:name, timestamp=2021-08-02T16:54:53.701, value=zhangsan
1002 column=info:sex, timestamp=2021-08-02T16:55:10.711, value=female
1 row(s)
Took 0.0151 seconds
- 查看表结构
hbase:024:0> desc 'student'
Table student is ENABLED
student
COLUMN FAMILIES DESCRIPTION
{NAME => 'info', BLOOMFILTER => 'ROW', IN_MEMORY => 'false', VERSIONS => '1', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', COMPRESSION =>
'NONE', TTL => 'FOREVER', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}
1 row(s)
Quota is disabled
Took 0.0935 seconds
- 更新数据
hbase:026:0> put 'student', '1001', 'info:name', 'lisi'
Took 0.0123 seconds
hbase:027:0> put 'student', '1001', 'info:age', '20'
Took 0.0172 seconds
- 查看“指定行”或“指定列族:列”的数据
hbase:030:0> get 'student', '1001'
COLUMN CELL
info:age timestamp=2021-08-02T17:02:06.801, value=20
info:name timestamp=2021-08-02T17:01:46.259, value=lisi
info:sex timestamp=2021-08-02T16:53:54.575, value=male
1 row(s)
Took 0.0241 seconds
hbase:031:0> get 'student', '1001', 'info:name'
COLUMN CELL
info:name timestamp=2021-08-02T17:01:46.259, value=lisi
1 row(s)
Took 0.0259 seconds
- 统计表数据行数
hbase:033:0> count 'student'
2 row(s)
Took 0.0343 seconds
=> 2
- 删除数据
hbase:034:0> deleteall 'student', '1001'
Took 0.0192 seconds
hbase:035:0> delete 'student', '1002', 'info:sex'
Took 0.0117 seconds
- 清空表数据
hbase:038:0> disable 'student'
Took 1.1732 seconds
hbase:039:0> truncate 'student'
Truncating 'student' table (it may take a while):
Truncating table...
Took 2.1732 seconds
- 删除表
hbase:041:0> disable 'student'
Took 0.3492 seconds
hbase:042:0> drop 'student'
Took 0.1452 seconds
- 变更表信息
hbase:022:0> alter 'student',{NAME=>'info',VERSIONS=>3}
hbase:022:0> get 'student','1001',{COLUMN=>'info:name',VERSIONS=>3}
API操作
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.List;
public class Test01 {
public static void main(String[] args) throws IOException {
}
public static Configuration conf;
static {
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "192.168.200.101");
conf.set("hbase.zookeeper.property.clientPort", "2181");
}
public static boolean isTableExist(String tableName) throws IOException {
Connection connection = ConnectionFactory.createConnection(conf);
Admin admin = connection.getAdmin();
return admin.tableExists(TableName.valueOf(tableName));
}
public static void createTable(String tableName, String... columnFamily) throws IOException {
Admin admin = ConnectionFactory.createConnection(conf).getAdmin();
if (admin.tableExists(TableName.valueOf(tableName))) {
System.out.println(tableName + "已经存在!");
}else {
HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
for (String cf : columnFamily) {
tableDescriptor.addFamily(new HColumnDescriptor(cf));
}
admin.createTable(tableDescriptor);
System.out.println(tableName + "创建成功!");
}
}
public static void dropTable(String tableName) throws IOException {
Admin admin = ConnectionFactory.createConnection(conf).getAdmin();
if (admin.tableExists(TableName.valueOf(tableName))) {
admin.disableTable(TableName.valueOf(tableName));
admin.deleteTable(TableName.valueOf(tableName));
System.out.println(tableName + "已经删除!");
}else {
System.out.println(tableName + "不存在!!");
}
}
public static void addRowData(String tableName, String rowKey, String columnFamily, String column, String value) throws IOException{
HTable hTable = new HTable(conf, tableName);
Put put = new Put(Bytes.toBytes(rowKey));
put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
hTable.put(put);
hTable.close();
System.out.println("插入数据成功");
}
public static void deleteMultiRow(String tableName, String... rows) throws IOException{
HTable hTable = new HTable(conf, tableName);
List<Delete> deleteList = new ArrayList<Delete>();
for(String row : rows){
Delete delete = new Delete(Bytes.toBytes(row));
deleteList.add(delete);
}
hTable.delete(deleteList);
hTable.close();
}
public static void getAllRows(String tableName) throws IOException{
HTable hTable = new HTable(conf, tableName);
Scan scan = new Scan();
ResultScanner resultScanner = hTable.getScanner(scan);
for(Result result : resultScanner){
Cell[] cells = result.rawCells();
for(Cell cell : cells){
System.out.println(" 行 键 :" + Bytes.toString(CellUtil.cloneRow(cell)));
System.out.println(" 列 族 " + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println(" 列 :" + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println(" 值 :" + Bytes.toString(CellUtil.cloneValue(cell)));
}
}
}
public static void getRow(String tableName, String rowKey) throws IOException{
HTable table = new HTable(conf, tableName);
Get get = new Get(Bytes.toBytes(rowKey));
Result result = table.get(get);
for(Cell cell : result.rawCells()){
System.out.println(" 行 键 :" + Bytes.toString(result.getRow()));
System.out.println(" 列 族 " + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println(" 列 :" + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println(" 值 :" + Bytes.toString(CellUtil.cloneValue(cell)));
System.out.println("时间戳:" + cell.getTimestamp());
}
}
public static void getRowQualifier(String tableName, String rowKey, String family, String qualifier) throws IOException{
HTable table = new HTable(conf, tableName);
Get get = new Get(Bytes.toBytes(rowKey));
get.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
Result result = table.get(get);
for(Cell cell : result.rawCells()){
System.out.println(" 行 键 :" + Bytes.toString(result.getRow()));
System.out.println(" 列 族 " + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println(" 列 :" + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println(" 值 :" + Bytes.toString(CellUtil.cloneValue(cell)));
}
}
}
|