分享

简单使用hbase

 石头狗 2009-03-15

简单使用hbase

关键字: hbase

我们来简单了解如何操作hbase,hbase与我们常用的数据库最大的差别就是列存储和无数据类型,所有数据都以string类型存储,再有如果hbase table里有5个字段,但实际只有4个字段有值,那么为null的那个字段是不占用空间的,这点比较好,可以跟我们常用的数据库比较下
首先还是创建一个表,暂不使用mapreduce:

Java代码 复制代码
  1. /**  
  2.  * 定义几个常量  
  3.  */  
  4. public static HBaseConfiguration conf = new HBaseConfiguration();   
  5. static HTable table = null;   
  6. /**  
  7.  * 创建hbase table  
  8.  * @param table  
  9.  * @throws IOException  
  10.  */  
  11. public static void creatTable(String tablename) throws IOException {   
  12.     HBaseAdmin admin = new HBaseAdmin(conf);   
  13.     if (!admin.tableExists(new Text(tablename))) {   
  14.         HTableDescriptor tableDesc = new HTableDescriptor(tablename);   
  15.         tableDesc.addFamily(new HColumnDescriptor("ip:"));   
  16.         tableDesc.addFamily(new HColumnDescriptor("time:"));   
  17.         tableDesc.addFamily(new HColumnDescriptor("type:"));   
  18.         tableDesc.addFamily(new HColumnDescriptor("cookie:"));   
  19.         //注意这个C列,下面我会简单以此列来说明列存储   
  20.         tableDesc.addFamily(new HColumnDescriptor("c:"));   
  21.         admin.createTable(tableDesc);   
  22.         System.out.println("table create ok!!!");   
  23.     } else {   
  24.         System.out.println("table Already exists");   
  25.     }   
  26. }  

 将hadoop/hbase都启动再运行,在hql中使用"desc tablename"就可以看出这个表有5个字段,下面我们再来往这个表里录入点数据,上面说过值为空的字段是不占用空间的,这里还要注意点,经过我的测试,如果发现这个字段无值,就不要往hbase里面写null值,当然你要真往这个字段存null值是不会有任何问题的,但你查询这个有null值的字段时,会有点毛病,当然这个我也不知道怎么描述,有兴趣的可以试试看,所以我下面有判断,再者,hbase table里面的每一行数据集都必须有一个唯一row关键字,这个row你可以随便定义,方便准确找到你需要的数据

Java代码 复制代码
  1. /**  
  2.  * 录入数据  
  3.  * @throws Exception  
  4.  */  
  5. public static void insertData() throws Exception{   
  6.     //读取日志文件   
  7.     BufferedReader reader = new BufferedReader(new FileReader("log file name"));   
  8.     if(table==null)   
  9.         table = new HTable(conf, new Text(tablename));   
  10.     String line;   
  11.     while((line = reader.readLine()) != null){   
  12.         //这里我就不说了,先前有说明   
  13.         LogAccess log = new LogAccess(line);   
  14.         //这里我使用time+cookie为row关键字,确保不重复,如果cookie记录有重复,将区别对待,这里暂不多做说明   
  15.         String row = createRow(log.getTime(),log.getCookie());   
  16.            
  17.         long lockid = table.startUpdate(new Text(row));   
  18.         if(!log.getIp().equals("") && log.getIp()!=null)   
  19.             table.put(lockid, new Text("ip:"), log.getIp().getBytes());   
  20.         if(!log.getTime().equals("") && log.getTime()!=null)   
  21.             table.put(lockid, new Text("time:"), log.getTime().getBytes());   
  22.         if(!log.getType().equals("") && log.getType()!=null)   
  23.             table.put(lockid, new Text("type:"), log.getType().getBytes());   
  24.         if(!log.getCookie().equals("") && log.getCookie()!=null)   
  25.             table.put(lockid, new Text("cookie:"), log.getCookie().getBytes());   
  26.         //这里要注意,我是往c列中写入了5个字段,你可以想象,我在c列中存入了一个map   
  27.         if(!log.getRegmark().equals("") && log.getRegmark()!=null)   
  28.             table.put(lockid, new Text("c:_regmark"), log.getRegmark().getBytes());   
  29.         if(!log.getRegmark2().equals("") && log.getRegmark2()!=null)   
  30.             table.put(lockid, new Text("c:_regmark2"), log.getRegmark2().getBytes());   
  31.         if(!log.getSendshow().equals("") && log.getSendshow()!=null)   
  32.             table.put(lockid, new Text("c:_sendshow"), log.getSendshow().getBytes());   
  33.         if(!log.getCurrenturl().equals("") && log.getCurrenturl()!=null)   
  34.             table.put(lockid, new Text("c:_currenturl"), log.getCurrenturl().getBytes());   
  35.         if(!log.getAgent().equals("") && log.getAgent()!=null)   
  36.             table.put(lockid, new Text("c:_agent"), log.getAgent().getBytes());   
  37.         //存入数据   
  38.         table.commit(lockid);   
  39.     }   
  40. }  

 

O了,测试下吧

评论
blank 2008-10-16   回复
http://solomons./blog/205736
上面这篇文章详细讲解了hadoop/hbase的安装以及启动方式,有兴趣看看吧
xianglei 2008-10-15   回复
hbase怎么启动?

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多