編輯pom.xmljava
<dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-server</artifactId> <version>1.2.0</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>1.2.0</version> </dependency>
java文件apache
package com.cenzhongman.hbase; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.client.Delete; import org.apache.hadoop.hbase.client.Get; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.ResultScanner; import org.apache.hadoop.hbase.client.Scan; import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.io.IOUtils; /** * * 演示 HBase 的 JDBC 鏈接及 CRUD 操做 * * @author cen * */ public class HBaseOperation { static HTable table = null; static String TABLE_NAME = "user"; /** * getHTablebyTbaleName * * @param tableName * @return */ @SuppressWarnings("deprecation") public static HTable getHTable() { // 1.get instance of Default Configuration 讀取 core-site.xml hdfs-site.xml 信息 // Configuration conf = new Configuration();//HDFS中獲取core-site.xml hdfs-site.xml // 的方法 // conf.addResource("hbase-default.xml"); // conf.addResource("hbase-site.xml"); // Hbase封裝的獲取方法(詳見源碼),增長了讀取HBase 的配置文件 Configuration conf = HBaseConfiguration.create(); // 2.get Table instance try { table = new HTable(conf, TABLE_NAME); } catch (IOException e) { e.printStackTrace(); } return table; } /** * getData by table,rowKey,columns[{cfs}][columns] * * @param table * @param rowKey * @param columns */ public static void getData(String rowKey, String[][] columns) { try { table = getHTable(); // create get with RowKey 選定Get的RowKey Get get = new Get(Bytes.toBytes(rowKey)); if (columns != null) { // add column 增長列查詢條件 for (int i = 0; i < columns[0].length; i++) { for (int j = 0; j < columns[1].length; j++) { get.addColumn(Bytes.toBytes(columns[0][i]), Bytes.toBytes(columns[1][j])); } } } // get Result Result result = null; result = table.get(get); // Key:rowKwy + cf + c + version + type + // value:value for (Cell cell : result.rawCells()) { System.out.println( Bytes.toString(CellUtil.cloneFamily(cell)) + ":" + Bytes.toString(CellUtil.cloneQualifier(cell)) + "->" + Bytes.toString(CellUtil.cloneValue(cell))); } } catch (IOException e) { e.printStackTrace(); } finally { IOUtils.closeStream(table); } } public static void getData(String rowKey) { getData(rowKey, null); } /** * putData to HBase by table,rowKey,column,value 一般一張表的表名和列簇都設置常量 使用Map比數組更簡單 * * @param table * @param rowKey * @param column * @param value */ @SuppressWarnings("deprecation") public static void putData(String rowKey, String cf, String column, String value) { try { table = getHTable(); // create put with rowKey Put put = new Put(Bytes.toBytes(rowKey)); // 增長列數據 put.add(Bytes.toBytes(cf), Bytes.toBytes(column), Bytes.toBytes(value)); // put The data to put. // 實際使用list<Puts> table.put(put); } catch (IOException e) { e.printStackTrace(); } finally { IOUtils.closeStream(table); } } /** * 刪除一張表中的數據 * * @param table * @param rowKey * @param cf * @param column */ public static void deleteData(String rowKey, String cf, String column) { // create Delete try { table = getHTable(); Delete delete = new Delete(Bytes.toBytes(rowKey)); // 須要 delete 的 data // delete.addColumn(Bytes.toBytes(cf), Bytes.toBytes(column));//刪除最新的版本 delete.addColumns(Bytes.toBytes(cf), Bytes.toBytes(column));// 刪除全部的版本 table.delete(delete); } catch (IOException e) { e.printStackTrace(); } finally { IOUtils.closeStream(table); } } /** * scan data from HBase 包頭不包尾 */ public static void scanData() { scanData(null, null, null, null); } public static void scanData(String startRow) { scanData(startRow, null, null, null); } public static void scanData(String startRow, String stopRow) { scanData(startRow, stopRow, null, null); } public static void scanData(String startRow, String stopRow, String family, String qualifier) { ResultScanner resultScanner = null; try { Scan scan = new Scan(); //三種方式 //1.範圍掃描可用構造函數來設置 // Scan scan2 = new Scan(startRow, stopRow) //2.設置起始範圍 if (startRow != null) { scan.setStartRow(Bytes.toBytes(startRow)); } if (stopRow != null) { scan.setStopRow(Bytes.toBytes(stopRow)); } // 列過濾條件 if (family != null) { if (qualifier != null) { scan.addFamily(Bytes.toBytes(family)); } scan.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier)); } //3.使用過濾器 /* * 使用Filter查詢速度會降低 * PrefixFilter : 前綴過濾 * PageFilter : 分頁過濾 */ // scan.setFilter(filter); //設置緩存 // scan.setCacheBlocks(cacheBlocks);//把經常使用的數據緩存到 RegionServer 的 BlocksCache 中 // scan.setCaching(caching);//每一次掃描時獲取列的數目 table = getHTable(); resultScanner = table.getScanner(scan); for (Result result : resultScanner) { System.out.println("Row:" + Bytes.toString(result.getRow())); for (Cell cell : result.rawCells()) { System.out.println(Bytes.toString(CellUtil.cloneFamily(cell)) + ":" + Bytes.toString(CellUtil.cloneQualifier(cell)) + "->" + Bytes.toString(CellUtil.cloneValue(cell))); } } } catch (IOException e) { e.printStackTrace(); } finally { IOUtils.closeStream(resultScanner); IOUtils.closeStream(table); } } public static void main(String[] args) { String rowKey = "00002"; String cf = "info"; String column = "name"; String value = "gugjhg"; putData(rowKey, cf, column, value); // deleteData(rowKey, cf, column); // String[][] columns = { { "info" }, { "name" ,"tel"} }; // getData(rowKey); scanData(); System.out.println("finish"); } }