您好,登錄后才能下訂單哦!
這篇文章主要介紹“Hbase的測試代碼是怎樣的”,在日常操作中,相信很多人在Hbase的測試代碼是怎樣的問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Hbase的測試代碼是怎樣的”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
import java.io.IOException; import java.util.ArrayList; import java.util.List; 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.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.MasterNotRunningException; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.ZooKeeperConnectionException; import org.apache.hadoop.hbase.client.Admin; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.Delete; import org.apache.hadoop.hbase.client.Get; 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.client.Table; import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp; import org.apache.hadoop.hbase.filter.Filter; import org.apache.hadoop.hbase.filter.FilterList; import org.apache.hadoop.hbase.filter.SingleColumnValueFilter; import org.apache.hadoop.hbase.util.Bytes; public class HBaseTest { public static Configuration configuration; public static Connection conn = null; static { configuration = HBaseConfiguration.create(); configuration.set("hbase.zookeeper.property.clientPort", "2181"); // configuration.set("hbase.zookeeper.quorum", "bfct163"); // configuration.set("hbase.master", "bfct162:60000"); configuration.set("hbase.zookeeper.quorum", "192.168.17.163"); configuration.set("hbase.master", "192.168.17.162:60000"); try { conn = ConnectionFactory.createConnection(configuration); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { // createTable("wujintao"); // insertData("wujintao"); QueryAll("wujintao"); // QueryByCondition1("wujintao"); // QueryByCondition2("wujintao"); // QueryByCondition3("wujintao"); // deleteRow("wujintao","112233bbbcccc"); // deleteByCondition("wujintao","abcdef"); // dropTable("wujintao"); } /** * 創建表 * * @param tableName */ public static void createTable(String tableName) { System.out.println("start create table ......"); try { TableName TABLE = TableName.valueOf(tableName); Admin hBaseAdmin = conn.getAdmin(); if (hBaseAdmin.tableExists(TABLE)) {// 如果存在要創建的表,那么先刪除,再創建 hBaseAdmin.disableTable(TABLE); hBaseAdmin.deleteTable(TABLE); System.out.println(tableName + " is exist,detele...."); } HTableDescriptor tableDescriptor = new HTableDescriptor(TABLE); tableDescriptor.addFamily(new HColumnDescriptor("column1")); tableDescriptor.addFamily(new HColumnDescriptor("column2")); tableDescriptor.addFamily(new HColumnDescriptor("column3")); hBaseAdmin.createTable(tableDescriptor); } catch (MasterNotRunningException e) { e.printStackTrace(); } catch (ZooKeeperConnectionException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println("end create table ......"); } /** * 插入數據 * * @param tableName */ public static void insertData(String tableName){ System.out.println("start insert data ......"); try { TableName TABLE = TableName.valueOf(tableName); Table table = conn.getTable(TABLE); Put put = new Put("112233bbbcccc".getBytes());// 一個PUT代表一行數據,再NEW一個PUT表示第二行數據,每行一個唯一的ROWKEY,此處rowkey為put構造方法中傳入的值 put.addColumn("column1".getBytes(), null, "aaa".getBytes());// 本行數據的第一列addColumn put.addColumn("column2".getBytes(), null, "bbb".getBytes());// 本行數據的第三列 put.addColumn("column3".getBytes(), null, "ccc".getBytes());// 本行數據的第三列 table.put(put); } catch (IOException e) { e.printStackTrace(); } System.out.println("end insert data ......"); } /** * 刪除一張表 * * @param tableName */ public static void dropTable(String tableName) { try { TableName TABLE = TableName.valueOf(tableName); Admin admin = conn.getAdmin(); admin.disableTable(TABLE); admin.deleteTable(TABLE); } catch (MasterNotRunningException e) { e.printStackTrace(); } catch (ZooKeeperConnectionException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 根據 rowkey刪除一條記錄 * * @param tablename * @param rowkey */ public static void deleteRow(String tableName, String rowkey) { try { TableName TABLE = TableName.valueOf(tableName); Table table = conn.getTable(TABLE); List<Delete> list = new ArrayList<Delete>(); Delete d1 = new Delete(rowkey.getBytes()); list.add(d1); table.delete(list); System.out.println("刪除行成功!"); } catch (IOException e) { e.printStackTrace(); } } /** * 組合條件刪除 * * @param tablename * @param rowkey */ public static void deleteByCondition(String tablename, String rowkey) { // 目前還沒有發現有效的API能夠實現 根據非rowkey的條件刪除 這個功能能,還有清空表全部數據的API操作 } /** * 查詢所有數據 * * @param tableName */ public static void QueryAll(String tableName) { try { TableName TABLE = TableName.valueOf(tableName); Table table = conn.getTable(TABLE); ResultScanner rs = table.getScanner(new Scan()); for (Result r : rs) { System.out.println("獲得到rowkey:" + new String(r.getRow())); for (Cell cell : r.listCells()) { System.out.println("列:" + new String(CellUtil.cloneFamily(cell)) + "====值:" + new String(CellUtil.cloneValue(cell))); } } } catch (IOException e) { e.printStackTrace(); } } /** * 單條件查詢,根據rowkey查詢唯一一條記錄 * * @param tableName */ public static void QueryByCondition1(String tableName) { try { TableName TABLE = TableName.valueOf(tableName); Table table = conn.getTable(TABLE); Get scan = new Get("112233bbbcccc".getBytes());// 根據rowkey查詢 Result r = table.get(scan); System.out.println("獲得到rowkey:" + new String(r.getRow())); for (Cell cell : r.listCells()) { System.out.println("列:" + new String(CellUtil.cloneFamily(cell)) + "====值:" + new String(CellUtil.cloneValue(cell))); } } catch (IOException e) { e.printStackTrace(); } } /** * 單條件按查詢,查詢多條記錄 * * @param tableName */ public static void QueryByCondition2(String tableName) { try { TableName TABLE = TableName.valueOf(tableName); Table table = conn.getTable(TABLE); Filter filter = new SingleColumnValueFilter(Bytes.toBytes("column1"), null, CompareOp.EQUAL, Bytes.toBytes("aaa")); // 當列column1的值為aaa時進行查詢 Scan s = new Scan(); s.setFilter(filter); ResultScanner rs = table.getScanner(s); for (Result r : rs) { System.out.println("獲得到rowkey:" + new String(r.getRow())); for (Cell cell : r.listCells()) { System.out.println("列:" + new String(CellUtil.cloneFamily(cell)) + "====值:" + new String(CellUtil.cloneValue(cell))); } } } catch (Exception e) { e.printStackTrace(); } } /** * 組合條件查詢 * * @param tableName */ public static void QueryByCondition3(String tableName) { try { TableName TABLE = TableName.valueOf(tableName); Table table = conn.getTable(TABLE); List<Filter> filters = new ArrayList<Filter>(); Filter filter1 = new SingleColumnValueFilter(Bytes.toBytes("column1"), null, CompareOp.EQUAL, Bytes.toBytes("aaa")); filters.add(filter1); Filter filter2 = new SingleColumnValueFilter(Bytes.toBytes("column2"), null, CompareOp.EQUAL, Bytes.toBytes("bbb")); filters.add(filter2); Filter filter3 = new SingleColumnValueFilter(Bytes.toBytes("column3"), null, CompareOp.EQUAL, Bytes.toBytes("ccc")); filters.add(filter3); FilterList filterList1 = new FilterList(filters); Scan scan = new Scan(); scan.setFilter(filterList1); ResultScanner rs = table.getScanner(scan); for (Result r : rs) { System.out.println("獲得到rowkey:" + new String(r.getRow())); for (Cell cell : r.listCells()) { System.out.println("列:" + new String(CellUtil.cloneFamily(cell)) + "====值:" + new String(CellUtil.cloneValue(cell))); } } rs.close(); } catch (Exception e) { e.printStackTrace(); } } }
到此,關于“Hbase的測試代碼是怎樣的”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。