Hbase是我接触的新东西。项目组也准备使用它开发一个大的服务平台。我也趁机学习学习,先看看Hbase的简单操作方法吧:
虽然Hbase与传统的关系型数据库有很大的不同,但首先建张表还是必须的:
- /**
- * 定义几个常量
- */
- 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" );
- }
- }
表建好了就可以插入数据了:
- /**
- * 插入数据
- * @throws Exception
- */
- public static void insertData() throws Exception{
- //读取日志文件
- BufferedReader reader = new BufferedReader( new FileReader( "此处为读入的文件名" ));
- 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);
- }
- }