hbase java API
主方法
1 import org.apache.commons.lang.StringUtils; 2 import org.apache.hadoop.conf.Configuration; 3 import org.apache.hadoop.hbase.Cell; 4 import org.apache.hadoop.hbase.HBaseConfiguration; 5 import org.apache.hadoop.hbase.TableName; 6 import org.apache.hadoop.hbase.client.*; 7 import org.apache.hadoop.hbase.util.Bytes; 8 import org.slf4j.Logger; 9 import org.slf4j.LoggerFactory; 10 11 import java.io.BufferedReader; 12 import java.io.FileNotFoundException; 13 import java.io.FileReader; 14 import java.io.IOException; 15 import java.util.*; 16 17 public class HbaseTest { 18 19 private static Logger log = LoggerFactory.getLogger(HbaseTest.class); 20 21 static Configuration conf = null; 22 static Connection conn = null; 23 static { 24 conf = HBaseConfiguration.create(); 25 conf.set("hbase.zookeeper.quorum", "linux01"); 26 conf.set("hbase.zookeeper.property.clientPort", "2181"); 27 try { 28 conn = ConnectionFactory.createConnection(conf); 29 } catch (IOException e) { 30 e.printStackTrace(); 31 } 32 } 33 34 public static void main(String[] args) { 35 String tblName = "flights"; 36 String[] columnFamilys = {"info"}; 37 // 1 创建表 38 // createTable(tblName, columnFamilys); 39 40 // 2 插入数据 41 /*List<Map<String, Object>> data = getData();//准备多行数据 42 for (Map<String, Object> map : data) { 43 insertRow(TableName.valueOf(tblName), map); 44 }*/ 45 //3 修改数据 46 /*Map<String, Object> dataOfUpdate = new HashMap<String, Object>(); 47 dataOfUpdate.put("rowKey", "fffc1ebc-236f-4807-a527-a3696f0bb9a9"); 48 dataOfUpdate.put("colFamily", "info"); 49 dataOfUpdate.put("Dest", "Pudong Shanghai"); 50 updateRow(TableName.valueOf(tblName), dataOfUpdate);*/ 51 //4 删除数据 52 //removeRow(TableName.valueOf(tblName), "fffc1ebc-236f-4807-a527-a3696f0bb9a9"); 53 //deleteColFamily(TableName.valueOf(tblName), "ffc80296-f051-4a8c-9972-845887ee1474", "info"); 54 //deleteCol(TableName.valueOf(tblName), "ff7f07c8-0787-4670-9654-c47d376a6c10", "info", "Dest"); 55 //5 查看数据 56 //scanTable(TableName.valueOf("user"), new String[]{"info1", "info2"}); 57 getRowData(TableName.valueOf("user"), "1234"); 58 }
准备数据
1 public static List<Map<String, Object>> getData() { 2 ArrayList<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); 3 String[] colNames = new String[]{"AvgTicketPrice", "Cancelled", "Carrier", "Dest", "DestAirportID", "Origin", "OriginAirportID"}; 4 try { 5 BufferedReader br = new BufferedReader(new FileReader("I:\\projectImplement\\dataWareHouse\\test-es\\data\\630data.csv")); 6 String line = null; 7 while ((line = br.readLine()) != null) { 8 String[] vals = line.split(","); 9 HashMap<String, Object> map = new HashMap<String, Object>(); 10 for (int i = 0; i < colNames.length; i++) { 11 map.put(colNames[i], vals[i]); 12 } 13 map.put("rowKey", UUID.randomUUID().toString()); 14 map.put("colFamily", "info"); 15 list.add(map); 16 } 17 } catch (FileNotFoundException e) { 18 e.printStackTrace(); 19 } catch (IOException e) { 20 e.printStackTrace(); 21 } 22 return list; 23 }
创建表
1 public static void createTable(String tName, String[] columnFamilys) { 2 if (StringUtils.isBlank(tName) || columnFamilys == null || columnFamilys.length < 0) { 3 log.error("hbase 表名,列族为空"); 4 return; 5 } 6 7 TableName hTable = TableName.valueOf(tName); 8 try { 9 Admin admin = conn.getAdmin(); 10 if (admin.tableExists(hTable)) { 11 log.info(tName + "表已经存在"); 12 return; 13 } 14 15 //创建表 16 TableDescriptorBuilder tblDescBuilder = TableDescriptorBuilder.newBuilder(hTable);//表构造器 17 for (String colFamily : columnFamilys) { 18 ColumnFamilyDescriptorBuilder colFamilyDescBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(colFamily));//列族构造器 19 ColumnFamilyDescriptor colFamilyDesc = colFamilyDescBuilder.build(); 20 tblDescBuilder.setColumnFamily(colFamilyDesc);//表构造器添加 21 } 22 TableDescriptor tblDesc = tblDescBuilder.build(); 23 admin.createTable(tblDesc); 24 } catch (IOException e) { 25 e.printStackTrace(); 26 log.error(tName + "创建失败"); 27 } 28 }
插入数据
1 public static void insertRow(TableName tblName, Map<String, Object> map) { 2 if (tblName == null || map == null || map.size() <= 2) { 3 log.info("表名或参数为空"); 4 return; 5 } 6 7 Table table = null; 8 try { 9 table = conn.getTable(tblName); 10 //封装put, 并添加数据 11 Put put = new Put(Bytes.toBytes(map.get("rowKey").toString())); 12 byte[] cf = Bytes.toBytes(map.get("colFamily").toString()); 13 for (Map.Entry<String, Object> en : map.entrySet()) { 14 if ("rowKey".equals(en.getKey()) || "colFamily".equals(en.getKey())) { 15 continue; 16 } 17 put.addColumn(cf, Bytes.toBytes(en.getKey()), Bytes.toBytes(en.getValue().toString())); 18 } 19 table.put(put);//添加数据 20 } catch (IOException e) { 21 e.printStackTrace(); 22 log.error(tblName + "插入数据失败, map=" + map); 23 } finally { 24 if (table != null) { 25 try { 26 table.close(); 27 } catch (IOException e) { 28 e.printStackTrace(); 29 } finally { 30 table = null; 31 } 32 } 33 } 34 }
删除数据
1 public static void removeRow(TableName tblName, String rowKey) { 2 if (tblName == null || rowKey == null) { 3 log.info("表名或rowkey为空"); 4 return; 5 } 6 7 Table hTable = null; 8 Delete delete = new Delete(Bytes.toBytes(rowKey)); 9 try { 10 hTable = conn.getTable(tblName); 11 hTable.delete(delete); 12 } catch (IOException e) { 13 e.printStackTrace(); 14 log.error("删除失败, rowkey=" + rowKey); 15 } finally { 16 if (hTable != null) { 17 try { 18 hTable.close(); 19 } catch (IOException e) { 20 e.printStackTrace(); 21 } finally { 22 hTable = null; 23 } 24 } 25 } 26 }
修改数据
1 public static void updateRow(TableName tblName, Map<String, Object> map) { 2 if (tblName == null || map == null || map.size() <= 2) { 3 log.info("表名或参数为空"); 4 return; 5 } 6 7 Table hTable = null; 8 try { 9 hTable = conn.getTable(tblName); 10 //封装put 新增数据 11 Put put = new Put(Bytes.toBytes(map.get("rowKey").toString())); 12 byte[] cf = Bytes.toBytes(map.get("colFamily").toString()); 13 for (Map.Entry<String, Object> en : map.entrySet()) { 14 if ("rowKey".equals(en.getKey()) || "colFamily".equals(en.getKey())) { 15 log.info("rowkey 或 colFamily 为空"); 16 continue; 17 } 18 put.addColumn(cf, Bytes.toBytes(en.getKey()), Bytes.toBytes(en.getValue().toString())); 19 } 20 hTable.put(put); //新增数据 21 } catch (IOException e) { 22 e.printStackTrace(); 23 log.error("修改失败"); 24 } finally { 25 if (hTable != null) { 26 try { 27 hTable.close(); 28 } catch (IOException e) { 29 e.printStackTrace(); 30 } finally { 31 hTable = null; 32 } 33 } 34 } 35 }
查询数据
1 public static void getRowData(TableName tblName, String rowKey) { 2 if (tblName == null || rowKey == null) { 3 log.info("表名 或 rowkey为空"); 4 return; 5 } 6 Table hTable = null; 7 ResultScanner rs = null; 8 try { 9 hTable = conn.getTable(tblName); 10 Get get = new Get(Bytes.toBytes(rowKey)); 11 Result result = hTable.get(get); 12 for (Cell c : result.rawCells()) { 13 String val = Bytes.toString(c.getValueArray(), c.getValueOffset(), c.getValueLength()); 14 String cf = Bytes.toString(c.getFamilyArray(), c.getFamilyOffset(), c.getFamilyLength()); 15 String col = Bytes.toString(c.getQualifierArray(), c.getQualifierOffset(), c.getQualifierLength()); 16 System.out.printf("tbl=%s, rowkey=%s, cf=%s, col=%s, val=%s\n", tblName.getNameAsString(), cf, col, rowKey, val); 17 } 18 19 } catch (IOException e) { 20 e.printStackTrace(); 21 log.info("扫描失败"); 22 } finally { 23 if (rs != null) { 24 rs.close(); 25 } 26 if (hTable != null) { 27 try { 28 hTable.close(); 29 } catch (IOException e) { 30 e.printStackTrace(); 31 } 32 } 33 } 34 } 35 36 public static void scanTable(TableName tblName, String[] colFamilys) { 37 if (tblName == null) { 38 log.info("表名为空"); 39 return; 40 } 41 Table hTable = null; 42 ResultScanner rs = null; 43 try { 44 hTable = conn.getTable(tblName); 45 Scan scan = new Scan(); 46 for (String cf : colFamilys) { 47 scan.addFamily(Bytes.toBytes(cf)); 48 } 49 50 rs = hTable.getScanner(scan); 51 // 结果处理, 一个result表示一个rowkey的数据 52 for (Result result : rs) { 53 String rowkey = Bytes.toString(result.getRow()); 54 for (Cell c : result.rawCells()) { 55 String val = Bytes.toString(c.getValueArray(), c.getValueOffset(), c.getValueLength()); 56 String cf = Bytes.toString(c.getFamilyArray(), c.getFamilyOffset(), c.getFamilyLength()); 57 String col = Bytes.toString(c.getQualifierArray(), c.getQualifierOffset(), c.getQualifierLength()); 58 System.out.printf("tbl=%s, rowkey=%s, cf=%s, col=%s, val=%s\n", tblName.getNameAsString(), cf, col, rowkey, val); 59 } 60 } 61 } catch (IOException e) { 62 e.printStackTrace(); 63 log.info("扫描失败"); 64 } finally { 65 if (rs != null) { 66 rs.close(); 67 } 68 if (hTable != null) { 69 try { 70 hTable.close(); 71 } catch (IOException e) { 72 e.printStackTrace(); 73 } 74 } 75 } 76 }