hbase1.2.4 API改动

  1. 使用ConnectionFactory来连接数据库
  2. 表名使用特定的类TableName而不是字符串
  3. 弃用HAdmin使用Admin,通过Connection获取
  4. 启用HTable使用Table,通过Connection获取
  5. Put.add()方法过时,使用Put.addColumn()
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; import java.util.Map; import java.util.NavigableMap; /** * Created by hadoop on 16-11-24. */ public class HBaseTestCase { static Configuration cfg = HBaseConfiguration.create(); //创建一张表,通过Admin HTableDescriptor 来创建 public static void createTable(Connection connection,TableName tableName, String columnFamily)throws Exception{ Admin admin = connection.getAdmin(); if(admin.tableExists(tableName)){ System.out.println("table Exists!"); }else{ HTableDescriptor tableDesc = new HTableDescriptor(tableName); tableDesc.addFamily(new HColumnDescriptor(columnFamily)); admin.createTable(tableDesc); System.out.println("create table success!"); } } //添加一条数据,通过HTable Put 为已经存在的表来添加数据 public static void put(Connection connection,TableName tableName,String row,String columnFamily,String column,String data)throws Exception{ Table table = connection.getTable(tableName); Put p1 = new Put(Bytes.toBytes(row)); p1.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(column),Bytes.toBytes(data)); table.put(p1); } //根据row key获取表中的该行数据 public static void get(Connection connection,TableName tableName,String row)throws IOException { Table table = connection.getTable(tableName); Get g = new Get(Bytes.toBytes(row)); Result result = table.get(g); NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> navigableMap = result.getMap(); for(Map.Entry<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> entry:navigableMap.entrySet()){ System.out.print(Bytes.toString(entry.getKey())+"#"); NavigableMap<byte[], NavigableMap<Long, byte[]>> map =entry.getValue(); for(Map.Entry<byte[], NavigableMap<Long, byte[]>> en:map.entrySet()){ System.out.print(Bytes.toString(en.getKey())+"##"); NavigableMap<Long, byte[]> ma = en.getValue(); for(Map.Entry<Long, byte[]>e: ma.entrySet()){ System.out.print(e.getKey()+"###"); System.out.println(Bytes.toString(e.getValue())); } } } // System.out.println("Get:"+result); } //根据TableName获取整张表中的数据 public static void scan(Connection connection,TableName tableName)throws Exception{ Table table = connection.getTable(tableName); Scan s = new Scan(); ResultScanner rs = table.getScanner(s); //遍历打印表中的数据 for(Result r:rs){ NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> navigableMap = r.getMap(); for(Map.Entry<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> entry:navigableMap.entrySet()){ System.out.print(Bytes.toString(r.getRow())+":"); System.out.print(Bytes.toString(entry.getKey())+"#"); NavigableMap<byte[], NavigableMap<Long, byte[]>> map =entry.getValue(); for(Map.Entry<byte[], NavigableMap<Long, byte[]>> en:map.entrySet()){ System.out.print(Bytes.toString(en.getKey())+"##"); NavigableMap<Long, byte[]> ma = en.getValue(); for(Map.Entry<Long, byte[]>e: ma.entrySet()){ System.out.print(e.getKey()+"###"); System.out.println(Bytes.toString(e.getValue())); } } } System.out.println(); } } //删除表中的数据 public static boolean delete(Connection connection,TableName tableName) throws IOException{ Admin admin = connection.getAdmin(); if(admin.tableExists(tableName)){ try{ admin.disableTable(tableName); admin.deleteTable(tableName); }catch(Exception ex){ ex.printStackTrace(); return false; } } return true; } public static void main(String args[]){ TableName tableName = TableName.valueOf("hbase_tb"); String columnFamily = "cf"; try{ cfg.set("hbase.zookeeper.property.clientPort", "2181"); cfg.set("hbase.zookeeper.quorum", "fang-ubuntu,fei-ubuntu,kun-ubuntu"); Connection connection = ConnectionFactory.createConnection(cfg); HBaseTestCase.createTable(connection,tableName,columnFamily); HBaseTestCase.put(connection,tableName,"row1",columnFamily,"cl1","data"); HBaseTestCase.put(connection,tableName,"row2",columnFamily,"cl1","fang"); HBaseTestCase.put(connection,tableName,"row2",columnFamily,"cl2","fang"); HBaseTestCase.put(connection,tableName,"row2",columnFamily,"cl3","fang"); HBaseTestCase.put(connection,tableName,"row3",columnFamily,"cl4","fang"); HBaseTestCase.get(connection,tableName,"row1"); HBaseTestCase.scan(connection,tableName); if(true==HBaseTestCase.delete(connection,tableName)){ System.out.println("Delete table:"+tableName.toString()+" success!"); } }catch(Exception e){ e.printStackTrace(); } } } 

 

 

代码实例中使用到了HBase的基本操作。

转载于:https://www.cnblogs.com/hejianxin/p/7610737.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值