立即注册 登录
About云-梭伦科技 返回首页

maizhu的个人空间 https://www.aboutyun.com/?8077 [收藏] [复制] [分享] [RSS]

日志

hbase学习_java操作

已有 1151 次阅读2014-11-2 21:19 |个人分类:hbase| java

翻译自HBase_in_action

2.1.3建立连接
 Configuration conf=new HBaseConfiguration.create();
         conf.set("hbase.zookeeper.quonum",serverip);     //serverip为在配置文件中配置的master的ip地址
 con.set("hbase.zookeeper.property.clientPort","2181"); //如果你在配置文件中更改过端口,通过这里进行设置,默认为2181端口
         HTableInterface usersTable=new HTable(conf,"users"); //这样就可以从hbase中获得users表,这个操作的前提是users表已经建立,不然会报错
2.1.4 连接的管理
      如果通过连接获取多张表,则可以通过HTablePool来进行管理
HTablePool pool=new HTablePool();
        HTableInterface usersTable=pool.getTable("users");
       ..........
       pool.close();
2.2 数据操作
2.2.1 添加数据
HTableInterface userstable=new HTable("users");
       Put p=new Put(Bytes.toByte("wang"));
       p.add(Bytes.toByte("info"),Bytes.toByte("email"),Bytes.toByte("wang@163.com"));
usertable.put(p);
       usertable.close();
2.2.2 修改数据
HTableInterface userstable=new HTable("users");
       Put p=new Put(Bytes.toByte("wang"));
       p.add(Bytes.toByte("info"),Bytes.toByte("email"),Bytes.toByte("zhangzhangzhangzhang@163.com"));
usertable.put(p);
       usertable.close();
2.2.3 读取数据
获得一条记录
  1.    HTable table = new HTable(conf, tableName);
  2.                 Get get = new Get(Bytes.toBytes(row));
  3.                 Result result = table.get(get);
  4.                 // 输出结果
  5.                 for (KeyValue rowKV : result.raw()) {
  6.                         System.out.print("Row Name: " + new String(rowKV.getRow()) + " ");
  7.                         System.out.print("Timestamp: " + rowKV.getTimestamp() + " ");
  8.                         System.out.print("column Family: " + new String(rowKV.getFamily()) + " ");
  9.                         System.out.print("Row Name:  " + new String(rowKV.getQualifier()) + " ");
  10.                         System.out.println("Value: " + new String(rowKV.getValue()) + " ");
  11.                 }
  12. 获得多条记录
    1.        public static void getAllRows(String tableName) throws Exception {
    2.                 HTable table = new HTable(conf, tableName);
    3.                 Scan scan = new Scan();
    4.                 ResultScanner results = table.getScanner(scan);
    5.                 // 输出结果
    6.                 for (Result result : results) {
    7.                         for (KeyValue rowKV : result.raw()) {
    8.                                 System.out.print("Row Name: " + new String(rowKV.getRow()) + " ");
    9.                                 System.out.print("Timestamp: " + rowKV.getTimestamp() + " ");
    10.                                 System.out.print("column Family: " + new String(rowKV.getFamily()) + " ");
    11.                                 System.out
    12.                                                 .print("Row Name:  " + new String(rowKV.getQualifier()) + " ");
    13.                                 System.out.println("Value: " + new String(rowKV.getValue()) + " ");
    14.                         }
    15.                 }
    16.         }

路过

雷人

握手

鲜花

鸡蛋

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 立即注册

关闭

推荐上一条 /2 下一条