class="java"> public class HBaseUtil { private static final Logger log = LoggerFactory.getLogger(HBaseUtil.class); private static Configuration conf = HBaseConfiguration.create(); private volatile static Connection conn; static { conf.set("hbase.zookeeper.quorum", "192.168.8.11"); conf.set("hbase.zookeeper.property.clientPort", "2181"); } /** * 获取HBase连接 * * @return * @throws IOException * @throws Exception */ private static Table getTable(String tableName) throws IOException { if (null == conn) { synchronized (conn) { if (null == conn) { // 创建连接 conn = ConnectionFactory.createConnection(conf); doShutDownWork(); } } } Table table = conn.getTable(TableName.valueOf(tableName)); return table; } /** * 当JVM退出之间,回调该方法 */ private static void doShutDownWork() { Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { @Override public void run() { try { closeConnection(); log.info("HBase Connection close successed"); } catch (Exception e) { log.info("HBase Connection close failed"); e.printStackTrace(); } } })); } /** * 关闭HBase连接 */ public static void closeConnection() { try { if (null != conn) { conn.close(); } } catch (IOException e) { e.printStackTrace(); } } /** * 指定的table是否存在指定rowkey的记录 * * @param tableName * @param rowkey * @return * @throws IOException */ public static boolean exist(String tableName, String rowkey) throws IOException { Table table = getTable(tableName); Get get = new Get(Bytes.toBytes(rowkey)); boolean bool = table.exists(get); table.close(); return bool; } /** * 单条插入 * * @param tableName * @param put * @throws IOException */ public static void put(String tableName, Put put) throws IOException { Table table = getTable(tableName); table.put(put); table.close(); } /** * 插入数据 * * @param tableName * @param rowkey * @param columnFamily * @param column * @param value * @throws Exception */ public static void putBatch(String tableName, List<Put> putList) throws IOException { Table table = getTable(tableName); table.put(putList); table.close(); } /** * 单条删除 * * @param tableName * @param rowkey * @param columnFamily * @param column * @throws IOException */ public static void delete(String tableName, Delete delete) throws IOException { Table table = getTable(tableName); table.delete(delete); table.close(); } /** * 批量删除 * * @param tableName * @param rowkey * @param colFamily * @param col * @throws Exception * @throws IOException */ public static void deleteBatch(String tableName, List<Delete> deleteList) throws IOException { Table table = getTable(tableName); table.delete(deleteList); table.close(); } /** * 根据表名、rowkey查找数据 * * @param tableName * @param rowkey * @throws Exception */ public static Result get(String tableName, Get get) throws IOException { Table table = getTable(tableName); Result result = table.get(get); table.close(); return result; } /** * * @param tableName * @param gets * @return * @throws IOException */ public static List<Result> getBatch(String tableName, List<Get> getList) throws IOException { Table table = getTable(tableName); Result[] results = table.get(getList); List<Result> listResult = new ArrayList<Result>(); for (Result res : results) { listResult.add(res); } table.close(); return listResult; } /** * 批量扫描数据 * * @param tableName * @param startRow * @param stopRow * @return * @throws IOException */ public static List<Result> scan(String tableName, Scan scan) throws IOException { Table table = getTable(tableName); ResultScanner resultScanner = table.getScanner(scan); List<Result> listResult = new ArrayList<Result>(); for (Result res : resultScanner) { listResult.add(res); } table.close(); return listResult; } }
public class Book { private String rowkey; private String name; private String author; private String publisher; private String type; private int sum; public String getRowkey() { return rowkey; } public void setRowkey(String rowkey) { this.rowkey = rowkey; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getPublisher() { return publisher; } public void setPublisher(String publisher) { this.publisher = publisher; } public String getType() { return type; } public void setType(String type) { this.type = type; } public int getSum() { return sum; } public void setSum(int sum) { this.sum = sum; } }
public class BookDao { private static final Logger log = LoggerFactory.getLogger(BookDao.class); public void doInsert(String tableName, List<Book> ListBook){ List<Put> putList = new ArrayList<Put>(); for(Book book : ListBook){ Put put = toPut(book); putList.add(put); } try { HBaseUtil.putBatch(tableName,putList); log.info("BookDao insert ListBook success"); } catch (IOException e) { log.error("BookDao insert ListBook occur IOException"); e.printStackTrace(); } } private Put toPut(Book book){ Put put = new Put(Bytes.toBytes(book.getRowkey())); put.addColumn(Bytes.toBytes(HBaseTableMsg.columnFamily_BOOKS), Bytes.toBytes("name"), Bytes.toBytes(book.getName())); put.addColumn(Bytes.toBytes(HBaseTableMsg.columnFamily_BOOKS), Bytes.toBytes("author"), Bytes.toBytes(book.getAuthor())); put.addColumn(Bytes.toBytes(HBaseTableMsg.columnFamily_BOOKS), Bytes.toBytes("publisher"), Bytes.toBytes(book.getPublisher())); put.addColumn(Bytes.toBytes(HBaseTableMsg.columnFamily_BOOKS), Bytes.toBytes("type"), Bytes.toBytes(book.getType())); put.addColumn(Bytes.toBytes(HBaseTableMsg.columnFamily_BOOKS), Bytes.toBytes("sum"), Bytes.toBytes(book.getSum())); return put; } public void doDelete(String tableName, List<Book> listBook){ List<Delete> deleteList = new ArrayList<Delete>(); for(Book book : listBook){ Delete delete = new Delete(Bytes.toBytes(book.getRowkey())); // //删除指定列族 // if(columnFamily != null && !"".equals(columnFamily)){ // delete.addFamily(Bytes.toBytes(columnFamily)); // } // //删除指定列 // if(columnFamily != null && !"".equals(columnFamily) && column != null && !"".equals(column)){ // delete.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(column)); // } deleteList.add(delete); } try { HBaseUtil.deleteBatch(tableName,deleteList); log.info("BookDao delete ListBook success"); } catch (IOException e) { log.error("BookDao delete ListBook occur IOException"); e.printStackTrace(); } } public List<Book> get(String tableName, List<Book> listBook) throws IOException{ List<Get> getList = new ArrayList<Get>(); for(Book book : listBook){ Get get = new Get(Bytes.toBytes(book.getRowkey())); // //获取指定列族 // if(columnFamily != null && !"".equals(columnFamily)){ // get.addFamily(Bytes.toBytes(columnFamily)); // } // //获取指定列 // if(columnFamily != null && !"".equals(columnFamily) && column != null && !"".equals(column)){ // get.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(column)); // } getList.add(get); } List<Result> listResult = HBaseUtil.getBatch(tableName, getList); List<Book> books = new ArrayList<Book>(); for(Result result : listResult){ Book book = new Book(); book = toBook(result); books.add(book); } return books; } public List<Book> scan(String tableName, String startRow,String endRow) throws IOException{ Scan scan = new Scan(); if(startRow != null && !"".equals(startRow)){ scan.setStartRow(Bytes.toBytes(startRow)); } if(endRow != null && !"".equals(endRow)){ scan.setStopRow(Bytes.toBytes(endRow)); } List<Result> listResult = HBaseUtil.scan(tableName, scan); List<Book> books = new ArrayList<Book>(); for(Result result : listResult){ Book book = new Book(); book = toBook(result); books.add(book); } return books; } private Book toBook(Result result){ Book book = new Book(); if (result.containsColumn(Bytes.toBytes(HBaseTableMsg.columnFamily_BOOKS), Bytes.toBytes("rowkey"))) { book.setRowkey(Bytes.toString(result.getRow())); } if (result.containsColumn(Bytes.toBytes(HBaseTableMsg.columnFamily_BOOKS), Bytes.toBytes("name"))) { Cell nameCell = result.getColumnLatestCell(Bytes.toBytes(HBaseTableMsg.columnFamily_BOOKS), Bytes.toBytes("name")); book.setName(Bytes.toString(CellUtil.cloneValue(nameCell))); // String name = new String(result.getValue(Bytes.toBytes("bookinfo"), Bytes.toBytes("name")), "UTF-8"); } if (result.containsColumn(Bytes.toBytes(HBaseTableMsg.columnFamily_BOOKS), Bytes.toBytes("author"))) { Cell authorCell = result.getColumnLatestCell(Bytes.toBytes(HBaseTableMsg.columnFamily_BOOKS), Bytes.toBytes("author")); book.setAuthor(Bytes.toString(CellUtil.cloneValue(authorCell))); } if (result.containsColumn(Bytes.toBytes(HBaseTableMsg.columnFamily_BOOKS), Bytes.toBytes("publisher"))) { Cell publisherCell = result.getColumnLatestCell(Bytes.toBytes(HBaseTableMsg.columnFamily_BOOKS), Bytes.toBytes("publisher")); book.setPublisher(Bytes.toString(CellUtil.cloneValue(publisherCell))); } if (result.containsColumn(Bytes.toBytes(HBaseTableMsg.columnFamily_BOOKS), Bytes.toBytes("type"))) { Cell typeCell = result.getColumnLatestCell(Bytes.toBytes(HBaseTableMsg.columnFamily_BOOKS), Bytes.toBytes("type")); book.setType(Bytes.toString(CellUtil.cloneValue(typeCell))); } if (result.containsColumn(Bytes.toBytes(HBaseTableMsg.columnFamily_BOOKS), Bytes.toBytes("sum"))) { Cell sumCell = result.getColumnLatestCell(Bytes.toBytes(HBaseTableMsg.columnFamily_BOOKS), Bytes.toBytes("sum")); book.setSum(Bytes.toInt(CellUtil.cloneValue(sumCell))); } return book; } }