hbase java api最新版本_HBase基本命令与新版本Java API

简介

有关HBase的安装可以参考hbase安装

我们可以通过hbase shell和hbase数据库进行交互,也可以通过Java-api和hbase数据库交互,这里我们使用的是hbase-client。

主要是介绍2.0重大重构之后的api的基本使用。

9f18bdf12c27dd80b8e85d2718a84340.png

命名空间

#列出所有命名空间

list_namespace

#新建命名空间

create_namespace 'namespaceName'

#删除命名空间

drop_namespace 'namespaceName'

#修改命名空间

表操作

create:创建表

exists:检查表是否存在

list:查看所有表

alter:修改表

delete:删除列

disable:禁用表

is_enabled:查看表是否禁用

desc\describe:查看表结构

drop:删除表

put:插入数据

count:统计表有多少行

get:获取数据

scan:扫描表或者列

#列出所有表

list

#列出指定命名空间下的所有表

list_namespace_tables 'namespaceName'

#新建一个以命名空间namespaceName的表tableName,列族为cf1。

create 'namespaceName:tableName', 'cf1'

create 'tablename','columnFamily1','columnFamily2'

create 'tablename',{NAME => 'columnFamily1',VERSIONS => 1, TTL => 214783647, BLOCKCACHE => false,IN_MEMORY=>false},{NAME=>'columnFamily2',VERSIONS=>1,TTL=>259200,BLOCKCACHE=>false,IN_MEMORY=>false}

disable 'tableName'

#删除表

drop 'tableName'

#查看表内容,查看前5行数据

scan 'namespaceName:tableName', {LIMIT=>5}

#插入 put tableName rowkey,"columnFamily:column","value"

put 'tableName', 'rowkey', 'columnFamilyName:columnName', 'value'

#查看 get rowKey

get 'tableName', 'rowkey'

删除表中的列簇,先disable表,修改后enable表,严格区分大小写

scan

查询user表中的所有信息

scan 'user'

查询user表中列族为info的信息

scan 'user', {COLUMNS => 'info'}

scan 'user', {COLUMNS => 'info', RAW => true, VERSIONS => 5}

查询user表中列族为info和data的信息

scan 'user', {COLUMNS => ['info', 'data']}

scan 'user', {COLUMNS => ['info:name', 'data:pic']}

查询user表中列族为info、列标示符为name的信息

scan 'user', {COLUMNS => 'info:name'}

查询user表中列族为info、列标示符为name的信息,并且版本最新的5个

scan 'user', {COLUMNS => 'info:name', VERSIONS => 5}

查询user表中列族为info和data且列标示符中含有a字符的信息

scan 'user', {COLUMNS => ['info', 'data'], FILTER => "(QualifierFilter(=,'substring:a'))"}

查询user表中row key为rk0001,cell的值为中国

scan 'user', 'rk0001', {FILTER => "ValueFilter(=,'binary:中国')"}

查询user表中row key为rk0001,列标示符中含有a的信息

scan 'user', 'rk0001', {FILTER => "(QualifierFilter(=,'substring:a'))"}

查询user表中row key以rk字符开头的

scan 'user',{FILTER=>"PrefixFilter('rk')"}

查询user表中列族为info,rk范围是[rk0001, rk0003)的数据

scan 'user', {COLUMNS => 'info', STARTROW => 'rk0001', ENDROW => 'rk0003'}

查询user表中指定范围的数据

scan 'user', {TIMERANGE => [1392368783980, 1392380169184]}

get命令也可以使用如上所示的scan命令方式

基本命令

help:查看命令帮助

status:查看hbase状态

version:查看hbase版本

快照

修改表名HBase没有rename命令,可以通过快照功能修改表名

//为表创建快照,这时还没有复制数据

clone_snapshot 'tableSnapshot', 'newTableName'

//根据某个快照而创建新表,此时复制数据

snapshot 'tableName', 'tableSnapshot'

//删除快照

delete_snapshot 'tableSnapshot'

Java API

org.apache.hbase

hbase-client

2.2.0

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.hbase.Cell;

import org.apache.hadoop.hbase.HBaseConfiguration;

import org.apache.hadoop.hbase.NamespaceDescriptor;

import org.apache.hadoop.hbase.TableName;

import org.apache.hadoop.hbase.client.*;

import org.apache.hadoop.hbase.util.Bytes;

import org.junit.Before;

import org.junit.Test;

import java.io.IOException;

import java.util.LinkedList;

import java.util.List;

import java.util.Random;

public class HBaseTest {

private static final String ZOOKEEPER_QUORUM = "127.0.0.1:2181";

public static final String TABLE_NAME = "test_tb";

private Connection connection;

private Configuration configuration;

@Before

public void setUp() throws IOException {

configuration = HBaseConfiguration.create();

configuration.set("hbase.zookeeper.quorum", ZOOKEEPER_QUORUM);

connection = ConnectionFactory.createConnection(configuration);

}

@Test

public void createTable() throws Exception {

Admin admin = connection.getAdmin();

TableName tableName = TableName.valueOf(TABLE_NAME);

TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tableName);

ColumnFamilyDescriptor baseInfo = ColumnFamilyDescriptorBuilder.of("base_info");

ColumnFamilyDescriptor extInfo = ColumnFamilyDescriptorBuilder.of("ext_info");

tableDescriptorBuilder.setColumnFamily(baseInfo);

tableDescriptorBuilder.setColumnFamily(extInfo);

TableDescriptor tableDescriptor = tableDescriptorBuilder.build();

admin.createTable(tableDescriptor);

}

@Test

public void dealTable() throws IOException {

Admin admin = connection.getAdmin();

String tableNameString = "test_user";

TableName tableName = TableName.valueOf(tableNameString);

if(admin.tableExists(tableName)){

admin.disableTable(tableName);

admin.deleteTable(tableName);

}

}

@Test

public void descTable() throws IOException {

TableName tableName = TableName.valueOf(TABLE_NAME);

Table table = connection.getTable(tableName);

TableDescriptor tableDescriptor = table.getDescriptor();

ColumnFamilyDescriptor[] columnFamilies = tableDescriptor.getColumnFamilies();

for(ColumnFamilyDescriptor cfd : columnFamilies){

System.out.println(Bytes.toString(cfd.getName()));

}

}

@Test

public void put() throws Exception {

TableName tableName = TableName.valueOf(TABLE_NAME);

Table table = connection.getTable(tableName);

Put put=new Put(Bytes.toBytes("user_info_1"));

// put.addColumn(Bytes.toBytes(familyName), Bytes.toBytes(columnName), Bytes.toBytes(value));

put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("name"), Bytes.toBytes("tim"));

put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("tel"), Bytes.toBytes("123"));

table.put(put);

}

@Test

public void puts() throws Exception {

TableName tableName = TableName.valueOf(TABLE_NAME);

Table table = connection.getTable(tableName);

LinkedList puts = new LinkedList<>();

puts.add(getPut("user_info_2"));

puts.add(getPut("user_info_3"));

puts.add(getPut("user_info_4"));

table.put(puts);

}

private static Put getPut(String rowKey){

Put put=new Put(Bytes.toBytes(rowKey));

Random random = new Random();

String name = String.valueOf(random.nextInt(1000000));

put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("name"), Bytes.toBytes(name));

put.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("tel"), Bytes.toBytes(name));

return put;

}

@Test

public void get() throws Exception {

TableName tableName = TableName.valueOf(TABLE_NAME);

Table table = connection.getTable(tableName);

Get get =new Get(Bytes.toBytes("user_info_1"));

get.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("name"));

Result result=table.get(get);

List cells = result.listCells();

for(Cell cell: cells){

System.out.println(Bytes.toString(cell.getFamilyArray()));

System.out.println(Bytes.toString(cell.getQualifierArray()));

System.out.println(Bytes.toString(cell.getValueArray()));

System.out.println(cell.getTimestamp());

}

}

@Test

public void gets() throws IOException {

TableName tableName = TableName.valueOf(TABLE_NAME);

Table table = connection.getTable(tableName);

LinkedList gets = new LinkedList<>();

gets.add(getGet("user_info_1"));

gets.add(getGet("user_info_2"));

gets.add(getGet("user_info_3"));

gets.add(getGet("user_info_4"));

Result[] results = table.get(gets);

for(Result result : results){

System.out.println(new String(result.getRow()));

System.out.println(new String(result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("name"))));

System.out.println(new String(result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("tel"))));

}

}

private static Get getGet(String rowKey){

Get get =new Get(Bytes.toBytes(rowKey));

get.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("name"));

get.addColumn(Bytes.toBytes("base_info"), Bytes.toBytes("tel"));

return get;

}

@Test

public void scan() throws IOException {

TableName tableName = TableName.valueOf(TABLE_NAME);

Table table = connection.getTable(tableName);

Scan s = new Scan();

ResultScanner resultScanner = table.getScanner(s);

for(Result result : resultScanner){

byte[] row = result.getRow();

System.out.println(new String(row));

System.out.println(new String(result.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("name"))));

}

}

@Test

public void deleteRowKey() throws Exception {

TableName tableName = TableName.valueOf(TABLE_NAME);

Table table = connection.getTable(tableName);

Delete de =new Delete(Bytes.toBytes("rowKey"));

table.delete(de);

}

@Test

public void deleteColumn() throws Exception {

TableName tableName = TableName.valueOf(TABLE_NAME);

Table table = connection.getTable(tableName);

Delete de =new Delete(Bytes.toBytes("rowKey"));

de.addColumn(Bytes.toBytes(""), Bytes.toBytes(""));

table.delete(de);

}

@Test

public void disableTable() throws Exception {

TableName tableName = TableName.valueOf(TABLE_NAME);

Admin admin = connection.getAdmin();

admin.disableTable(tableName);

}

@Test

public void listNamespace() throws Exception {

Admin admin = connection.getAdmin();

NamespaceDescriptor[] namespaceDescriptors = admin.listNamespaceDescriptors();

for(NamespaceDescriptor namespaceDescriptor : namespaceDescriptors){

System.out.println(namespaceDescriptor.getName());

}

}

@Test

public void listTables() throws IOException {

Admin admin = connection.getAdmin();

List tableDescriptors = admin.listTableDescriptors();

for(TableDescriptor tableDescriptor : tableDescriptors){

System.out.println(tableDescriptor.getTableName().getNameAsString());

}

}

}

重点就是构建连接:

Configuration configuration = HBaseConfiguration.create();

configuration.set("hbase.zookeeper.quorum", "127.0.0.1:2181");

Connection connection = ConnectionFactory.createConnection(configuration);

相关的操作基本和Admin有关,通过Connection获取Admin:

Admin admin = connection.getAdmin();

不再使用字符串表明,而是使用TableName抽象:

TableName tableName = TableName.valueOf("tableName");

表描述构建:

TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tableName);

列簇构建:

ColumnFamilyDescriptor baseInfo = ColumnFamilyDescriptorBuilder.of("base_info");

下面的createTable基本把表相关的抽象都使用到了:

@Test

public void createTable() throws Exception {

Admin admin = connection.getAdmin();

TableName tableName = TableName.valueOf(TABLE_NAME);

TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tableName);

ColumnFamilyDescriptor baseInfo = ColumnFamilyDescriptorBuilder.of("base_info");

ColumnFamilyDescriptor extInfo = ColumnFamilyDescriptorBuilder.of("ext_info");

tableDescriptorBuilder.setColumnFamily(baseInfo);

tableDescriptorBuilder.setColumnFamily(extInfo);

TableDescriptor tableDescriptor = tableDescriptorBuilder.build();

admin.createTable(tableDescriptor);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值