技术开发 频道

基于Java的HBase客户端编程

  (5)删除一行记录

1 /**
2  * 删除一行记录
3  *
@param tablename
4  *
@param rowkey
5  *
@throws IOException
6  
*/
7 public void deleteRow(String tablename, String rowkey) throws IOException {
8     HTable table = new HTable(conf, tablename);
9     List list = new ArrayList();
10     Delete d1 = new Delete(rowkey.getBytes());
11     list.add(d1);
12     table.delete(list);
13     System.out.println("删除行成功!");
14 }

  (6)查找一行记录

1 /**
2  * 查找一行记录
3  * @param tablename
4  * @param rowkey
5  */
6 public static void selectRow(String tablename, String rowKey)
7         throws IOException {
8     HTable table = new HTable(conf, tablename);
9     Get g = new Get(rowKey.getBytes());
10     Result rs = table.get(g);
11     for (KeyValue kv : rs.raw()) {
12         System.out.print(new String(kv.getRow()) + "  ");
13         System.out.print(new String(kv.getFamily()) + ":");
14         System.out.print(new String(kv.getQualifier()) + "  ");
15         System.out.print(kv.getTimestamp() + "  ");
16         System.out.println(new String(kv.getValue()));
17     }
18 }

  (7)查询表中所有行

1 /**
2  * 查询表中所有行
3  *
@param tablename
4  
*/
5 public void scaner(String tablename) {
6     try {
7         HTable table = new HTable(conf, tablename);
8         Scan s = new Scan();
9         ResultScanner rs = table.getScanner(s);
10         for (Result r : rs) {
11             KeyValue[] kv = r.raw();
12             for (int i = 0; i < kv.length; i++) {
13                 System.out.print(new String(kv[i].getRow()) + "  ");
14                 System.out.print(new String(kv[i].getFamily()) + ":");
15                 System.out.print(new String(kv[i].getQualifier()) + "  ");
16                 System.out.print(kv[i].getTimestamp() + "  ");
17                 System.out.println(new String(kv[i].getValue()));
18             }
19         }
20     } catch (IOException e) {
21         e.printStackTrace();
22     }
23 }
0
相关文章