HBase入门(三)——HBase远程连接+HBase常用命令

IDSW集群中Hbase的访问

环境

内容
版本 hbase-1.4.9
hadoop版本 hadoop2.6.5
安装目录 /data/hbase-1.4.9
机器 10.111.25.140(主)
10.111.25.138
10.111.25.139
zookeeper 使用hbase自带的zk

访问

在10.111.25.140中
hbase shell
注:hbase shell中用“Ctrl+Backspace”进行删除

http://10.111.25.140:16010

远程连接

pom.xml

<!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-client -->
<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-client</artifactId>
    <version>1.4.9</version>
</dependency>
参数 内容
集群ip 10.111.25.140(主)
10.111.25.138
10.111.25.139
hbase.zookeeper.quorum idsw-dev-03(或10.111.25.140)
hbase.zookeeper.property.clientPort 2181(默认)

访问代码:

package Utils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
 
public class HbaseTest {
    private HBaseAdmin admin = null;
    // 定义配置对象HBaseConfiguration
    private static Configuration configuration;
    public HbaseTest() throws Exception {
        configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum","10.111.25.140");  //hbase 服务地址
        configuration.set("hbase.zookeeper.property.clientPort","2181"); //端口号
        admin = new HBaseAdmin(configuration);
    }
    // Hbase获取所有的表信息
    public List getAllTables() {
        List<String> tables = null;
        if (admin != null) {
            try {
                HTableDescriptor[] allTable = admin.listTables();
                if (allTable.length > 0)
                    tables = new ArrayList<String>();
                for (HTableDescriptor hTableDescriptor : allTable) {
                    tables.add(hTableDescriptor.getNameAsString());
                    System.out.println(hTableDescriptor.getNameAsString());
                }
            }catch (IOException e) {
                e.printStackTrace();
            }
        }
        return tables;
    }
    public static void main(String[] args) throws Exception {
        HbaseTest hbaseTest = new HbaseTest();
        hbaseTest.getAllTables();
    }
}

Hbase常用命令

名称 命令表达式
查看hbase状态 status
创建表 create '表名','列族名1','列族名2','列族名N'
查看所有表 list
描述表 describe '表名'
判断表存在 exists '表名'
判断是否禁用启用表 is_enabled '表名' 
is_disabled '表名'
添加记录 put '表名','rowkey','列族:列','值'
查看记录rowkey下的所有数据 get '表名','rowkey'
查看所有记录 scan '表名'
查看表中的记录总数 count '表名'
获取某个列族 get '表名','rowkey','列族:列'
获取某个列族的某个列 get '表名','rowkey','列族:列'
删除记录 delete '表名','行名','列族:列'
删除整行 deleteall '表名','rowkey'
删除一张表 先要屏蔽该表,才能对该表进行删除
第一步 disable '表名',第二步 drop '表名'
清空表 truncate '表名'
查看某个表某个列中所有数据 scan '表名',{COLUMNS=>'列族名:列名'}
更新记录 就是覆盖,hbase没有修改,都是追加

表操作的命令

  1. list
  2. create
  3. describe
  4. enable
  5. disable
  6. is_disabled
  7. is_enabled
  8. drop

表数据相关操作的命令

命令 说明 实例
scan 就相当于select scan 'test'
count 计数有几行(row)一共 count 'test'
put 添加 put 'test','row1','info:age','30'
get 取出某一列的数据 get 'test','0001','info:name'
delete 删除某列 delete 'test','0001','info:age'
truncate 格式化某个表
默认执行下述操作
disable 'test'
drop 'test'
create 'test'
truncate 'test'
alter 修改 alter 'xingoo:test_v',NAME=>'v',VERSIONS=>5
create_namespace 创建命名空间 create_namespace 'xingoo'

版本相关操作命令

修改版本数

alter 'xingoo:test_v',NAME=>'v',VERSIONS=>5  
describe 'xingoo:test_v'

写入多版本

put 'xingoo:test_v','1','v:c1','value1'
put 'xingoo:test_v','1','v:c1','value2'
put 'xingoo:test_v','1','v:c1','value3'
put 'xingoo:test_v','1','v:c1','value4'
put 'xingoo:test_v','1','v:c1','value5'
put 'xingoo:test_v','1','v:c1','value6'
put 'xingoo:test_v','1','v:c1','value7'

读取多版本

get 'xingoo:test_v','1',{COLUMN => 'v:c1',VERSIONS=>5}

另外,还可以把版本字段当做一个时间字段来进行范围查询,如:

get 't1', 'r1', {COLUMN => 'c1', TIMERANGE => [ts1, ts2], VERSIONS => 4}

猜你喜欢

转载自blog.csdn.net/lishuan182/article/details/89531407