设为首页 加入收藏

TOP

HBase基本API(java)操作(增删改查)(二)
2015-11-21 01:45:31 来源: 作者: 【 】 浏览:3
Tags:HBase 基本 API java 操作 删改
new HTable(conf, tableName); Get get = new Get(Bytes.toBytes(row)); Result result = table.get(get); // 输出结果,raw方法返回所有keyvalue数组 for (KeyValue rowKV : result.raw()) { System.out.print("行名:" + new String(rowKV.getRow()) + " "); System.out.print("时间戳:" + rowKV.getTimestamp() + " "); System.out.print("列族名:" + new String(rowKV.getFamily()) + " "); System.out.print("列名:" + new String(rowKV.getQualifier()) + " "); System.out.println("值:" + new String(rowKV.getValue())); } } // 获取所有数据 public static void getAllRows(String tableName) throws Exception { HTable table = new HTable(conf, tableName); Scan scan = new Scan(); ResultScanner results = table.getScanner(scan); // 输出结果 for (Result result : results) { for (KeyValue rowKV : result.raw()) { System.out.print("行名:" + new String(rowKV.getRow()) + " "); System.out.print("时间戳:" + rowKV.getTimestamp() + " "); System.out.print("列族名:" + new String(rowKV.getFamily()) + " "); System.out .print("列名:" + new String(rowKV.getQualifier()) + " "); System.out.println("值:" + new String(rowKV.getValue())); } } } // 主函数 public static void main(String[] args) { try { String tableName = "student"; // 第一步:创建数据库表:“student” String[] columnFamilys = { "info", "course" }; HBaseJavaAPI.createTable(tableName, columnFamilys); // 第二步:向数据表的添加数据 // 添加第一行数据 if (isExist(tableName)) { HBaseJavaAPI.addRow(tableName, "zpc", "info", "age", "20"); HBaseJavaAPI.addRow(tableName, "zpc", "info", "sex", "boy"); HBaseJavaAPI.addRow(tableName, "zpc", "course", "china", "97"); HBaseJavaAPI.addRow(tableName, "zpc", "course", "math", "128"); HBaseJavaAPI.addRow(tableName, "zpc", "course", "english", "85"); // 添加第二行数据 HBaseJavaAPI.addRow(tableName, "henjun", "info", "age", "19"); HBaseJavaAPI.addRow(tableName, "henjun", "info", "sex", "boy"); HBaseJavaAPI.addRow(tableName, "henjun", "course", "china","90"); HBaseJavaAPI.addRow(tableName, "henjun", "course", "math","120"); HBaseJavaAPI.addRow(tableName, "henjun", "course", "english","90"); // 添加第三行数据 HBaseJavaAPI.addRow(tableName, "niaopeng", "info", "age", "18"); HBaseJavaAPI.addRow(tableName, "niaopeng", "info", "sex","girl"); HBaseJavaAPI.addRow(tableName, "niaopeng", "course", "china","100"); HBaseJavaAPI.addRow(tableName, "niaopeng", "course", "math","100"); HBaseJavaAPI.addRow(tableName, "niaopeng", "course", "english","99"); // 第三步:获取一条数据 System.out.println("**************获取一条(zpc)数据*************"); HBaseJavaAPI.getRow(tableName, "zpc"); // 第四步:获取所有数据 System.out.println("**************获取所有数据***************"); HBaseJavaAPI.getAllRows(tableName); // 第五步:删除一条数据 System.out.println("************删除一条(zpc)数据************"); HBaseJavaAPI.delRow(tableName, "zpc"); HBaseJavaAPI.getAllRows(tableName); // 第六步:删除多条数据 System.out.println("**************删除多条数据***************"); String rows[] = new String[] { "qingqing","xiaoxue" }; HBaseJavaAPI.delMultiRows(tableName, rows); HBaseJavaAPI.getAllRows(tableName); // 第七步:删除数据库 System.out.println("***************删除数据库表*****
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇插入语句,@@IDENTITY,@@rowcount 下一篇有关SCN有小结

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: