hbase命令及相应java API

1 habase命令

名称

命令表达式

创建表

create '表名', '列族名1','列族名2','列族名N'

查看所有表

list

描述表

describe  ‘表名’

判断表存在

exists  '表名'

判断是否禁用启用表

is_enabled '表名'

is_disabled ‘表名’

添加记录     

put  ‘表名’, ‘rowKey’, ‘列族 : 列‘  ,  '值'

查看记录rowkey下的所有数据

get  '表名' , 'rowKey'

查看表中的记录总数

count  '表名'

获取某个列族

get '表名','rowkey','列族'

获取某个列族的某个列

get '表名','rowkey','列族:列’

删除记录

delete  ‘表名’ ,‘行名’ , ‘列族:列'

删除整行

deleteall '表名','rowkey'

删除一张表

先要屏蔽该表,才能对该表进行删除

第一步 disable ‘表名’ ,第二步  drop '表名'

清空表

truncate '表名'

查看所有记录

scan "表名" 

查看某个表某个列中所有数据

scan "表名" , {COLUMNS=>'列族名:列名'}

更新记录

就是重写一遍,进行覆盖,hbase没有修改,都是追加


HBaseConfiguration

包:org.apache.hadoop.hbase.HBaseConfiguration

作用:通过此类可以对HBase进行配置

用法实例:

Configuration config =HBaseConfiguration.create();

说明: HBaseConfiguration.create() 默认会从classpath 中查找hbase-site.xml 中的配置信息,初始化 Configuration。

 

使用方法:

static Configuration config = null;

static {

    config = HBaseConfiguration.create();

    config.set("hbase.zookeeper.quorum","slave1,slave2,slave3");

    config.set("hbase.zookeeper.property.clientPort","2181");

}

1.1.  表管理类

HBaseAdmin

包:org.apache.hadoop.hbase.client.HBaseAdmin

作用:提供接口关系HBase 数据库中的表信息

 

用法:

HBaseAdmin admin = new HBaseAdmin(config);

1.2.  表描述类

HTableDescriptor

包:org.apache.hadoop.hbase.HTableDescriptor

作用:HTableDescriptor 类包含了表的名字以及表的列族信息

         表的schema(设计)

用法:

HTableDescriptor htd =newHTableDescriptor(tablename);

htd.addFamily(newHColumnDescriptor(“myFamily”));

1.3.  列族的描述类

HColumnDescriptor

包:org.apache.hadoop.hbase.HColumnDescriptor

作用:HColumnDescriptor 维护列族的信息

 

用法:

htd.addFamily(newHColumnDescriptor(“myFamily”));

1.4.  创建表的操作

CreateTable(一般我们用shell创建表)

static Configuration config = null;

static {

    config = HBaseConfiguration.create();

    config.set("hbase.zookeeper.quorum","slave1,slave2,slave3");

    config.set("hbase.zookeeper.property.clientPort","2181");

}

 

HBaseAdmin admin = new HBaseAdmin(config);

HTableDescriptor desc = newHTableDescriptor(tableName);

HColumnDescriptor family1 = newHColumnDescriptor(“f1”);

HColumnDescriptor family2 = newHColumnDescriptor(“f2”);

desc.addFamily(family1);

desc.addFamily(family2);

admin.createTable(desc);

1.5.  删除表

HBaseAdmin admin = new HBaseAdmin(config);

admin.disableTable(tableName);

admin.deleteTable(tableName);

1.6.  创建一个表的类

HTable

包:org.apache.hadoop.hbase.client.HTable

作用:HTable 和 HBase 的表通信

用法:

// 普通获取表

 HTable table = newHTable(config,Bytes.toBytes(tablename));

// 通过连接池获取表

Connection connection =ConnectionFactory.createConnection(config);

HTableInterface table = connection.getTable(TableName.valueOf("user"));

1.7.  单条插入数据

Put

包:org.apache.hadoop.hbase.client.Put

作用:插入数据

用法:

Put put = new Put(row);

p.add(family,qualifier,value);

说明:向表 tablename 添加“family,qualifier,value”指定的值。

 

示例代码:

Connection connection =ConnectionFactory.createConnection(config);

HTableInterface table = connection.getTable(TableName.valueOf("user"));

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

put.add(Bytes.toBytes(family),Bytes.toBytes(qualifier),Bytes.toBytes(value));

table.put(put);

1.8.  批量插入

批量插入

List<Put> list = newArrayList<Put>();

Put put = new Put(Bytes.toBytes(rowKey));//获取put,用于插入

put.add(Bytes.toBytes(family),Bytes.toBytes(qualifier),Bytes.toBytes(value));//封装信息

list.add(put);

table.put(list);//添加记录

1.9.  删除数据

Delete

包:org.apache.hadoop.hbase.client.Delete

作用:删除给定rowkey的数据

用法:

Delete del= newDelete(Bytes.toBytes(rowKey));

table.delete(del);

代码实例

Connection connection =ConnectionFactory.createConnection(config);

HTableInterface table = connection.getTable(TableName.valueOf("user"));

Delete del= newDelete(Bytes.toBytes(rowKey));

table.delete(del);

1.10.    单条查询

Get

包:org.apache.hadoop.hbase.client.Get

作用:获取单个行的数据

用法:

HTable table = new HTable(config,Bytes.toBytes(tablename));

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

Result result = table.get(get);

说明:获取 tablename 表中 row 行的对应数据

 

代码示例:

Connection connection =ConnectionFactory.createConnection(config);

HTableInterface table = connection.getTable(TableName.valueOf("user"));

Get get = new Get(rowKey.getBytes());

Result row = table.get(get);

for (KeyValue kv : row.raw()) {

         System.out.print(newString(kv.getRow()) + " ");

         System.out.print(newString(kv.getFamily()) + ":");

         System.out.print(newString(kv.getQualifier()) + " = ");

         System.out.print(newString(kv.getValue()));

         System.out.print("timestamp = " + kv.getTimestamp() + "\n");

}

1.11.    批量查询

ResultScanner

包:org.apache.hadoop.hbase.client.ResultScanner

作用:获取值的接口

用法:

ResultScanner scanner = table.getScanner(scan);

For(Result rowResult : scanner){

       Bytes[] str = rowResult.getValue(family,column);

}

说明:循环获取行中列值。

 

代码示例:

Connection connection =ConnectionFactory.createConnection(config);

HTableInterface table = connection.getTable(TableName.valueOf("user"));

Scan scan = new Scan();

scan.setStartRow("a1".getBytes());

scan.setStopRow("a20".getBytes());

ResultScanner scanner =table.getScanner(scan);

for (Result row : scanner) {

         System.out.println("\nRowkey:" + new String(row.getRow()));

         for(KeyValue kv : row.raw()) {

              System.out.print(new String(kv.getRow()) +" ");

              System.out.print(newString(kv.getFamily()) + ":");

              System.out.print(newString(kv.getQualifier()) + " = ");

              System.out.print(newString(kv.getValue()));

              System.out.print(" timestamp = "+ kv.getTimestamp() + "\n");

         }

}



  

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值