Hbase简单操作
来源:互联网 发布:专科解题软件 编辑:程序博客网 时间:2024/05/22 08:25
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);
- }
- }
- Hbase简单操作
- HBase简单API操作
- hbase简单操作
- Hbase简单操作
- HBase 简单数据操作
- Hbase简单操作(2)
- hive/hbase的简单操作
- hbase数据模型与简单操作
- Hbase一些简单DAO操作
- Hbase 简单操作Java示例
- Hbase单机安装及使用hbase shell进行简单操作
- python 通过thrift 简单操作hbase
- MapReduce实验:HBase安装以及简单操作
- python 通过thrift 简单操作hbase
- HBase学习笔记-API简单操作
- hbase做简单操作练习示例
- hbase 增删改查 api 简单操作
- Java 连接hbase 进行简单操作
- STL 简介,标准模板库
- 我现在做外包的
- 高效率测试之巧用策略模式
- STL 简介,标准模板库
- 关于CE6的提问解答
- Hbase简单操作
- C99标准的新特性
- 何必羡慕Safari 5?火狐浏览器也有“阅读模式”
- 在路上
- 测试人员的激励
- vlan加入域问题
- C关键字解析
- Linux pthread_create 如何设置 线程的detach 状态
- ubuntu下gvim启动出现gtk warning Invalid input string