使用java操作HBase

前提:

HBase版本为 hbase0.94.18.tar.gz

使用HBase的Java api,对HBase进行操作。

 

1、连接hbase

1
2
3
4
5
6
7
8
//设置配置信息
public Configuration getConfig() {
     Configuration conf = HBaseConfiguration.create();
     conf.set( "hbase.master" "192.168.206.21:60000" );
     conf.set( "hbase.zookeeper.quorum" "192.168.206.22,192.168.206.23,192.168.206.24" );
     conf.set( "hbase.zookeeper.property.clientport" "2181" );
     return conf;
}

2、在hbase中创建表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// 创建表
public boolean createTable(Configuration conf, String tableName) {
 
     boolean result = false;
     HBaseAdmin hBaseAdmin = null;
     try {
         hBaseAdmin =  new HBaseAdmin(conf);
         if (hBaseAdmin.tableExists(tableName)) {
             hBaseAdmin.disableTable(tableName);
             hBaseAdmin.deleteTable(tableName);
         }
         HTableDescriptor hTableDescriptor =  new HTableDescriptor(tableName);
         hTableDescriptor.addFamily( new HColumnDescriptor( "columnFamily1" ));
         hBaseAdmin.createTable(hTableDescriptor);
         result = true;
     catch (MasterNotRunningException e) {
         e.printStackTrace();
         result = false;
     catch (ZooKeeperConnectionException e) {
         e.printStackTrace();
         result = false;
     catch (IOException e) {
         e.printStackTrace();
         result = false;
     finally {
         try {
             hBaseAdmin.close();
         catch (IOException e) {
             e.printStackTrace();
         }
     }
     return result;
}

3、往HBase插入记录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// 插入一条记录
public boolean putRowIntoTable(Configuration conf,String tableName){
     boolean result = false;
     HTable hTable = null;
     try {
         hTable =  new HTable(conf, tableName);
         Put put =  new Put( "rowkey001" .getBytes());
         put.add( "columnFamily1" .getBytes(), "column1" .getBytes(), "value1" .getBytes());
         put.add( "columnFamily1" .getBytes(), "column2" .getBytes(), "value2" .getBytes());
         hTable.put(put);
         result = true;
     catch (IOException e) {
         e.printStackTrace();
         result = false;
     } finally {
         try {
             hTable.close();
         catch (IOException e) {
             e.printStackTrace();
         }
     }
     return result;
}

4、删除记录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// 删除一条记录
public boolean deleteRow(Configuration conf,String hTableName,String rowKey){
     boolean result = false;
     HTable hTable  = null;
     try {
         hTable =  new HTable(conf,hTableName);
         hTable. delete ( new Delete (rowKey.getBytes()));
         result = true;
     catch (IOException e) {
         e.printStackTrace();
         result = false;
     } finally {
         try {
             hTable.close();
         catch (IOException e) {
             e.printStackTrace();
         }
     }
     return result;
}

5、删除表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//删除表
public boolean dropTable(Configuration conf, String tableName){
     boolean result = false;
     HBaseAdmin hBaseAdmin = null;
     try {
         hBaseAdmin =  new HBaseAdmin(conf);
         if (hBaseAdmin.tableExists(tableName)){
             hBaseAdmin.disableTable(tableName);
             hBaseAdmin.deleteTable(tableName);
         } else {
             System.err.println(tableName +  " is not exist!" );
         }
         result = true;
     catch (IOException e) {
         e.printStackTrace();
         result = false;
     } finally {
         try {
             hBaseAdmin.close();
         catch (IOException e) {
             e.printStackTrace();
         }
     }
     return result;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值