简单使用hbase关键字: hbase我们来简单了解如何操作hbase,hbase与我们常用的数据库最大的差别就是列存储和无数据类型,所有数据都以string类型存储,再有如果hbase table里有5个字段,但实际只有4个字段有值,那么为null的那个字段是不占用空间的,这点比较好,可以跟我们常用的数据库比较下
/** * 定义几个常量 */ public static HBaseConfiguration conf = new HBaseConfiguration(); static HTable table = null; /** * 创建hbase table * @param table * @throws IOException */ public static void creatTable(String tablename) throws IOException { HBaseAdmin admin = new HBaseAdmin(conf); if (!admin.tableExists(new Text(tablename))) { HTableDescriptor tableDesc = new HTableDescriptor(tablename); tableDesc.addFamily(new HColumnDescriptor("ip:")); tableDesc.addFamily(new HColumnDescriptor("time:")); tableDesc.addFamily(new HColumnDescriptor("type:")); tableDesc.addFamily(new HColumnDescriptor("cookie:")); //注意这个C列,下面我会简单以此列来说明列存储 tableDesc.addFamily(new HColumnDescriptor("c:")); admin.createTable(tableDesc); System.out.println("table create ok!!!"); } else { System.out.println("table Already exists"); } } 将hadoop/hbase都启动再运行,在hql中使用"desc tablename"就可以看出这个表有5个字段,下面我们再来往这个表里录入点数据,上面说过值为空的字段是不占用空间的,这里还要注意点,经过我的测试,如果发现这个字段无值,就不要往hbase里面写null值,当然你要真往这个字段存null值是不会有任何问题的,但你查询这个有null值的字段时,会有点毛病,当然这个我也不知道怎么描述,有兴趣的可以试试看,所以我下面有判断,再者,hbase table里面的每一行数据集都必须有一个唯一row关键字,这个row你可以随便定义,方便准确找到你需要的数据
/** * 录入数据 * @throws Exception */ public static void insertData() throws Exception{ //读取日志文件 BufferedReader reader = new BufferedReader(new FileReader("log file name")); if(table==null) table = new HTable(conf, new Text(tablename)); String line; while((line = reader.readLine()) != null){ //这里我就不说了,先前有说明 LogAccess log = new LogAccess(line); //这里我使用time+cookie为row关键字,确保不重复,如果cookie记录有重复,将区别对待,这里暂不多做说明 String row = createRow(log.getTime(),log.getCookie()); long lockid = table.startUpdate(new Text(row)); if(!log.getIp().equals("") && log.getIp()!=null) table.put(lockid, new Text("ip:"), log.getIp().getBytes()); if(!log.getTime().equals("") && log.getTime()!=null) table.put(lockid, new Text("time:"), log.getTime().getBytes()); if(!log.getType().equals("") && log.getType()!=null) table.put(lockid, new Text("type:"), log.getType().getBytes()); if(!log.getCookie().equals("") && log.getCookie()!=null) table.put(lockid, new Text("cookie:"), log.getCookie().getBytes()); //这里要注意,我是往c列中写入了5个字段,你可以想象,我在c列中存入了一个map if(!log.getRegmark().equals("") && log.getRegmark()!=null) table.put(lockid, new Text("c:_regmark"), log.getRegmark().getBytes()); if(!log.getRegmark2().equals("") && log.getRegmark2()!=null) table.put(lockid, new Text("c:_regmark2"), log.getRegmark2().getBytes()); if(!log.getSendshow().equals("") && log.getSendshow()!=null) table.put(lockid, new Text("c:_sendshow"), log.getSendshow().getBytes()); if(!log.getCurrenturl().equals("") && log.getCurrenturl()!=null) table.put(lockid, new Text("c:_currenturl"), log.getCurrenturl().getBytes()); if(!log.getAgent().equals("") && log.getAgent()!=null) table.put(lockid, new Text("c:_agent"), log.getAgent().getBytes()); //存入数据 table.commit(lockid); } }
O了,测试下吧 |
|
来自: 石头狗 > 《bigtable》
评论
上面这篇文章详细讲解了hadoop/hbase的安装以及启动方式,有兴趣看看吧