Hbase常用操作CRUD(增删改查)
参考:
http://blog.csdn.net/net19880504/article/details/17733161
运行Eclipse,创建一个新的Java工程“HBaseClient”,右键项目根目录,选择 “Properties”->“Java Build Path”->“Library”->“Add External JARs”,将Hbase解压后根目录下的hbase-0.94.1-security.jar、hbase-0.94.1-security-tests.jar和lib子目录下所有jar 包添加到本工程的Classpath下。
HBase提供了java api来对HBase进行一系列的管理涉及到对表的管理、数据的操作等。常用的API操作有:
1、 对表的创建、删除、显示以及修改等,可以用HBaseAdmin,一旦创建了表,那么可以通过HTable的实例来访问表,每次可以往表里增加数据。
2、 插入数据
创建一个Put对象,在这个Put对象里可以指定要给哪个列增加数据,以及当前的时间戳等值,然后通过调用HTable.put(Put)来提交操作,子猴在这里提请注意的是:在创建Put对象的时候,你必须指定一个行(Row)值,在构造Put对象的时候作为参数传入。
3、 获取数据
要获取数据,使用Get对象,Get对象同Put对象一样有好几个构造函数,通常在构造的时候传入行值,表示取第几行的数据,通过HTable.get(Get)来调用。
4、 浏览每一行
通过Scan可以对表中的行进行浏览,得到每一行的信息,比如列名,时间戳等,Scan相当于一个游标,通过next()来浏览下一个,通过调用HTable.getScanner(Scan)来返回一个ResultScanner对象。HTable.get(Get)和HTable.getScanner(Scan)都是返回一个Result。Result是一个
KeyValue的链表。
5、 删除
使用Delete来删除记录,通过调用HTable.delete(Delete)来执行删除操作。(注:删除这里有些特别,也就是删除并不是马上将数据从表中删除。)
6、 锁
新增、获取、删除在操作过程中会对所操作的行加一个锁,而浏览却不会。
7、 簇的访问
客户端代码通过ZooKeeper来访问找到簇,也就是说ZooKeeper quorum将被使用,那么相关的类(包)应该在客户端的类(classes)目录下,即客户端一定要找到文件hbase-site.xml。
新建一个类:
1 package com.hj.myhbase131; 2 3 import java.io.IOException; 4 import java.util.ArrayList; 5 import java.util.List; 6 7 import org.apache.hadoop.conf.Configuration; 8 import org.apache.hadoop.hbase.client.Put; 9 import org.apache.hadoop.hbase.client.Result; 10 import org.apache.hadoop.hbase.client.ResultScanner; 11 import org.apache.hadoop.hbase.client.Scan; 12 import org.apache.hadoop.hbase.util.Bytes; 13 import org.apache.hadoop.hbase.Cell; 14 import org.apache.hadoop.hbase.CellUtil; 15 import org.apache.hadoop.hbase.HBaseConfiguration; 16 import org.apache.hadoop.hbase.HColumnDescriptor; 17 import org.apache.hadoop.hbase.HTableDescriptor; 18 import org.apache.hadoop.hbase.TableName; 19 import org.apache.hadoop.hbase.client.Connection; 20 import org.apache.hadoop.hbase.client.ConnectionFactory; 21 import org.apache.hadoop.hbase.client.Delete; 22 import org.apache.hadoop.hbase.client.Get; 23 import org.apache.hadoop.hbase.client.HBaseAdmin; 24 import org.apache.hadoop.hbase.client.HTable; 25 26 /** 27 * Hello world! 28 * 29 */ 30 public class App 31 { 32 private static Configuration conf = null; 33 private static Connection conn = null; 34 35 static { 36 conf = HBaseConfiguration.create(); 37 try { 38 conn = ConnectionFactory.createConnection(conf); 39 } catch (IOException e) { 40 // TODO Auto-generated catch block 41 e.printStackTrace(); 42 } 43 } 44 45 /**创建表 46 * @param tableName 47 * @param familys 48 * @throws Exception 49 */ 50 public static void creatTable(String tableName, String[] familys) throws Exception { 51 HBaseAdmin admin = (HBaseAdmin) conn.getAdmin(); 52 if (admin.tableExists(tableName)) { 53 System.out.println("table already exists"); 54 }else { 55 HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf(tableName)); 56 for (int i = 0; i < familys.length; i++) { 57 tableDesc.addFamily(new HColumnDescriptor(familys[i])); 58 } 59 admin.createTable(tableDesc); 60 System.out.println("create table " + tableName + " ok."); 61 } 62 } 63 64 /**删除表 65 * @param tableName 66 * @throws Exception 67 */ 68 public static void deleteTable(String tableName) throws Exception { 69 HBaseAdmin admin = (HBaseAdmin) conn.getAdmin(); 70 admin.disableTable(tableName); 71 admin.deleteTable(tableName); 72 System.out.println("delete table " + tableName + " ok."); 73 } 74 75 /**插入一行记录 76 * @param tableName 77 * @param rowKey 78 * @param family 79 * @param qualifier 80 * @param value 81 * @throws Exception 82 */ 83 public static void addRecord (String tableName, String rowKey, String family, String qualifier, String value) 84 throws Exception{ 85 try { 86 HTable table = (HTable) conn.getTable(TableName.valueOf(tableName)); 87 Put put = new Put(Bytes.toBytes(rowKey)); 88 put.addColumn(Bytes.toBytes(family),Bytes.toBytes(qualifier),Bytes.toBytes(value)); 89 table.put(put); 90 System.out.println("insert recored " + rowKey + " to table " + tableName +" ok."); 91 } catch (IOException e) { 92 e.printStackTrace(); 93 } 94 } 95 96 /** 97 * 删除一行记录 98 */ 99 public static void delRecord (String tableName, String rowKey) throws IOException{ 100 HTable table = (HTable) conn.getTable(TableName.valueOf(tableName)); 101 List <Delete>list = new ArrayList<Delete>(); 102 Delete del = new Delete(rowKey.getBytes()); 103 list.add(del); 104 table.delete(list); 105 System.out.println("del recored " + rowKey + " ok."); 106 } 107 108 /** 109 * 查找一行记录 110 */ 111 public static void getOneRecord (String tableName, String rowKey) throws IOException{ 112 HTable table = (HTable) conn.getTable(TableName.valueOf(tableName)); 113 Get get = new Get(rowKey.getBytes()); 114 Result rs = table.get(get); 115 for(Cell cell : rs.listCells()){ 116 // 一个Cell就是一个单元格,通过下面的方法可以获得这个单元格的各个值 117 System.out.print("行健: " + new String(CellUtil.cloneRow(cell))); 118 System.out.print("列簇: " + new String(CellUtil.cloneFamily(cell))); 119 System.out.print(" 列: " + new String(CellUtil.cloneQualifier(cell))); 120 System.out.print(" 值: " + new String(CellUtil.cloneValue(cell))); 121 System.out.println("时间戳: " + cell.getTimestamp()); 122 } 123 } 124 125 /** 126 * 显示所有数据 127 */ 128 public static void getAllRecord (String tableName) { 129 try{ 130 HTable table = (HTable) conn.getTable(TableName.valueOf(tableName)); 131 Scan s = new Scan(); 132 ResultScanner ss = table.getScanner(s); 133 for(Result r:ss){ 134 for(Cell cell : r.listCells()){ 135 System.out.print(Bytes.toString(CellUtil.cloneRow(cell)) + " " ); 136 System.out.print(Bytes.toString(CellUtil.cloneFamily(cell)) + ":" ); 137 System.out.print(Bytes.toString(CellUtil.cloneQualifier(cell)) + " " ); 138 System.out.println(Bytes.toString(CellUtil.cloneValue(cell))); 139 } 140 } 141 } catch (IOException e){ 142 e.printStackTrace(); 143 } 144 } 145 146 public static void main( String[] args ) throws Exception 147 { 148 try { 149 String tablename = "scores"; 150 String[] familys = {"grade", "course"}; 151 App.creatTable(tablename, familys); 152 153 //add record zkb 154 App.addRecord(tablename,"zkb","grade","","5"); 155 App.addRecord(tablename,"zkb","course","","90"); 156 App.addRecord(tablename,"zkb","course","math","97"); 157 App.addRecord(tablename,"zkb","course","art","87"); 158 //add record baoniu 159 App.addRecord(tablename,"baoniu","grade","","4"); 160 App.addRecord(tablename,"baoniu","course","math","89"); 161 162 System.out.println("===========get one record========"); 163 App.getOneRecord(tablename, "zkb"); 164 165 System.out.println("===========show all record========"); 166 App.getAllRecord(tablename); 167 168 System.out.println("===========del one record========"); 169 App.delRecord(tablename, "baoniu"); 170 App.getAllRecord(tablename); 171 172 System.out.println("===========show all record========"); 173 App.getAllRecord(tablename); 174 } catch (Exception e) { 175 e.printStackTrace(); 176 } 177 } 178 }
结果显示为:
table already exists
insert recored zkb to table scores ok.
insert recored zkb to table scores ok.
insert recored zkb to table scores ok.
insert recored zkb to table scores ok.
insert recored baoniu to table scores ok.
insert recored baoniu to table scores ok.
===========get one record========
行健: zkb列簇: course 列: 值: 90时间戳: 1501034629784
行健: zkb列簇: course 列: art 值: 87时间戳: 1501034629794
行健: zkb列簇: course 列: math 值: 97时间戳: 1501034629787
行健: zkb列簇: grade 列: 值: 5时间戳: 1501034629768
===========show all record========
baoniu course:math 89
baoniu grade: 4
zkb course: 90
zkb course:art 87
zkb course:math 97
zkb grade: 5
===========del one record========
del recored baoniu ok.
zkb course: 90
zkb course:art 87
zkb course:math 97
zkb grade: 5
===========show all record========
zkb course: 90
zkb course:art 87
zkb course:math 97
zkb grade: 5
posted on 2017-07-24 09:39 codemaker313 阅读(432) 评论(0) 编辑 收藏 举报