您好,登錄后才能下訂單哦!
魯春利的工作筆記,好記性不如爛筆頭
Java客戶端:
org.apache.hadoop.hbase.client.HTable類:該類的讀寫是非線程安全的,不再作為client API提供給開發用戶使用,建議通過Table類替代。
/** * Creates an object to access a HBase table. * @param conf Configuration object to use. * @param tableName Name of the table. * @throws IOException if a remote or network exception occurs * @deprecated Constructing HTable objects manually has been deprecated. * {@link Connection} to instantiate a {@link Table} instead. */ @Deprecated public HTable(Configuration conf, final String tableName) throws IOException { this(conf, TableName.valueOf(tableName)); }
org.apache.hadoop.hbase.client.Table類:
org.apache.hadoop.hbase.client.HConnectionManager類:
org.apache.hadoop.hbase.client.HBaseAdmin類:
@InterfaceAudience.Private @InterfaceStability.Evolving public class HBaseAdmin implements Admin { private static final Log LOG = LogFactory.getLog(HBaseAdmin.class); // 略 @Deprecated public HBaseAdmin(Configuration c) throws MasterNotRunningException, ZooKeeperConnectionException, IOException { // Will not leak connections, as the new implementation of the constructor // does not throw exceptions anymore. this(ConnectionManager.getConnectionInternal(new Configuration(c))); this.cleanupConnectionOnClose = true; } // 略 } # 說明:HBaseAdmin不在作為客戶端API使用,標記為Private表示為HBase-internal class。 # 使用Connection#getAdmin()來獲取Admin實例。
org.apache.hadoop.hbase.client.ConnectionFactory類:
@InterfaceAudience.Public @InterfaceStability.Evolving public class ConnectionFactoryextends Object // Example: Connection connection = ConnectionFactory.createConnection(config); Table table = connection.getTable(TableName.valueOf("table1")); try { // Use the table as needed, for a single operation and a single thread } finally { table.close(); connection.close(); }
客戶端使用示例:
package com.invic.hbase; import java.io.IOException; import java.util.Iterator; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; 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.TableName; 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.Filter; import org.apache.hadoop.hbase.filter.PageFilter; import org.apache.hadoop.hbase.util.Bytes; /** * * @author lucl * HBase的配置實例 * */ public class HBaseManagerMain { private static final Log LOG = LogFactory.getLog(HBaseManagerMain.class); // 在Eclipse中運行時報錯如下 // Caused by: java.lang.ClassNotFoundException: org.apache.htrace.Trace // Caused by: java.lang.NoClassDefFoundError: io/netty/channel/ChannelHandler // 需要把單獨的htrace-core-3.1.0-incubating.jar和netty-all-4.0.5.final.jar導入項目中 private static final String TABLE_NAME = "m_domain"; private static final String COLUMN_FAMILY_NAME = "cf"; /** * @param args */ public static void main(String[] args) { Configuration conf = HBaseConfiguration.create(); conf.set("hbase.master", "nnode:60000"); conf.set("hbase.zookeeper.property.clientport", "2181"); conf.set("hbase.zookeeper.quorum", "nnode,dnode1,dnode2"); HBaseManagerMain manageMain = new HBaseManagerMain(); try { /** * HTable類讀寫時是非線程安全的,已經標記為Deprecated * 建議通過org.apache.hadoop.hbase.client.Connection來獲取實例 */ Connection connection = ConnectionFactory.createConnection(conf); Admin admin = connection.getAdmin(); /** * 列出所有的表 */ manageMain.listTables(admin); /** * 判斷表m_domain是否存在 */ boolean exists = manageMain.isExists(admin); /** * 存在就刪除 */ if (exists) { manageMain.deleteTable(admin); } /** * 創建表 */ manageMain.createTable(admin); /** * 再次列出所有的表 */ manageMain.listTables(admin); /** * 添加數據 */ manageMain.putDatas(connection); /** * 檢索數據-表掃描 */ manageMain.scanTable(connection); /** * 檢索數據-單行讀 */ manageMain.getData(connection); /** * 檢索數據-根據條件 */ manageMain.queryByFilter(connection); /** * 刪除數據 */ manageMain.deleteDatas(connection); } catch (IOException e) { e.printStackTrace(); } } /** * 列出表 * @param admin * @throws IOException */ private void listTables (Admin admin) throws IOException { TableName [] names = admin.listTableNames(); for (TableName tableName : names) { LOG.info("Table Name is : " + tableName.getNameAsString()); } } /** * 判斷表是否存在 * @param admin * @return * @throws IOException */ private boolean isExists (Admin admin) throws IOException { /** * org.apache.hadoop.hbase.TableName為為代表了表名字的Immutable POJO class對象, * 形式為<table namespace>:<table qualifier>。 * static TableName valueOf(byte[] fullName) * static TableName valueOf(byte[] namespace, byte[] qualifier) * static TableName valueOf(ByteBuffer namespace, ByteBuffer qualifier) * static TableName valueOf(String name) * static TableName valueOf(String namespaceAsString, String qualifierAsString) * HBase系統默認定義了兩個缺省的namespace * hbase:系統內建表,包括namespace和meta表 * default:用戶建表時未指定namespace的表都創建在此 * 在HBase中,namespace命名空間指對一組表的邏輯分組,類似RDBMS中的database,方便對表在業務上劃分。 * */ TableName tableName = TableName.valueOf(TABLE_NAME); boolean exists = admin.tableExists(tableName); if (exists) { LOG.info("Table " + tableName.getNameAsString() + " already exists."); } else { LOG.info("Table " + tableName.getNameAsString() + " not exists."); } return exists; } /** * 創建表 * @param admin * @throws IOException */ private void createTable (Admin admin) throws IOException { TableName tableName = TableName.valueOf(TABLE_NAME); LOG.info("To create table named " + TABLE_NAME); HTableDescriptor tableDesc = new HTableDescriptor(tableName); HColumnDescriptor columnDesc = new HColumnDescriptor(COLUMN_FAMILY_NAME); tableDesc.addFamily(columnDesc); admin.createTable(tableDesc); } /** * 刪除表 * @param admin * @throws IOException */ private void deleteTable (Admin admin) throws IOException { TableName tableName = TableName.valueOf(TABLE_NAME); LOG.info("disable and then delete table named " + TABLE_NAME); admin.disableTable(tableName); admin.deleteTable(tableName); } /** * 添加數據 * @param connection * @throws IOException */ private void putDatas (Connection connection) throws IOException { String [] rows = {"baidu.com_19991011_20151011", "alibaba.com_19990415_20220523"}; String [] columns = {"owner", "ipstr", "access_server", "reg_date", "exp_date"}; String [][] values = { {"Beijing Baidu Technology Co.", "220.181.57.217", "北京", "1999年10月11日", "2015年10月11日"}, {"Hangzhou Alibaba Advertising Co.", "205.204.101.42", "杭州", "1999年04月15日", "2022年05月23日"} }; TableName tableName = TableName.valueOf(TABLE_NAME); byte [] family = Bytes.toBytes(COLUMN_FAMILY_NAME); Table table = connection.getTable(tableName); for (int i = 0; i < rows.length; i++) { System.out.println("========================" + rows[i]); byte [] rowkey = Bytes.toBytes(rows[i]); Put put = new Put(rowkey); for (int j = 0; j < columns.length; j++) { byte [] qualifier = Bytes.toBytes(columns[j]); byte [] value = Bytes.toBytes(values[i][j]); put.addColumn(family, qualifier, value); } table.put(put); } table.close(); } /** * 檢索數據-單行獲取 * @param connection * @throws IOException */ private void getData(Connection connection) throws IOException { LOG.info("Get data from table " + TABLE_NAME + " by family."); TableName tableName = TableName.valueOf(TABLE_NAME); byte [] family = Bytes.toBytes(COLUMN_FAMILY_NAME); byte [] row = Bytes.toBytes("baidu.com_19991011_20151011"); Table table = connection.getTable(tableName); Get get = new Get(row); get.addFamily(family); // 也可以通過addFamily或addColumn來限定查詢的數據 Result result = table.get(get); List<Cell> cells = result.listCells(); for (Cell cell : cells) { String qualifier = new String(CellUtil.cloneQualifier(cell)); String value = new String(CellUtil.cloneValue(cell), "UTF-8"); // @Deprecated // LOG.info(cell.getQualifier() + "\t" + cell.getValue()); LOG.info(qualifier + "\t" + value); } } /** * 檢索數據-表掃描 * @param connection * @throws IOException */ private void scanTable(Connection connection) throws IOException { LOG.info("Scan table " + TABLE_NAME + " to browse all datas."); TableName tableName = TableName.valueOf(TABLE_NAME); byte [] family = Bytes.toBytes(COLUMN_FAMILY_NAME); Scan scan = new Scan(); scan.addFamily(family); Table table = connection.getTable(tableName); ResultScanner resultScanner = table.getScanner(scan); for (Iterator<Result> it = resultScanner.iterator(); it.hasNext(); ) { Result result = it.next(); List<Cell> cells = result.listCells(); for (Cell cell : cells) { String qualifier = new String(CellUtil.cloneQualifier(cell)); String value = new String(CellUtil.cloneValue(cell), "UTF-8"); // @Deprecated // LOG.info(cell.getQualifier() + "\t" + cell.getValue()); LOG.info(qualifier + "\t" + value); } } } /** * 安裝條件檢索數據 * @param connection */ private void queryByFilter(Connection connection) { // 簡單分頁過濾器示例程序 Filter filter = new PageFilter(15); // 每頁15條數據 int totalRows = 0; byte [] lastRow = null; Scan scan = new Scan(); scan.setFilter(filter); // 略 } /** * 刪除數據 * @param connection * @throws IOException */ private void deleteDatas(Connection connection) throws IOException { LOG.info("delete data from table " + TABLE_NAME + " ."); TableName tableName = TableName.valueOf(TABLE_NAME); byte [] family = Bytes.toBytes(COLUMN_FAMILY_NAME); byte [] row = Bytes.toBytes("baidu.com_19991011_20151011"); Delete delete = new Delete(row); // @deprecated Since hbase-1.0.0. Use {@link #addColumn(byte[], byte[])} // delete.deleteColumn(family, qualifier); // 刪除某個列的某個版本 delete.addColumn(family, Bytes.toBytes("owner")); // @deprecated Since hbase-1.0.0. Use {@link #addColumns(byte[], byte[])} // delete.deleteColumns(family, qualifier) // 刪除某個列的所有版本 // @deprecated Since 1.0.0. Use {@link #(byte[])} // delete.addFamily(family); // 刪除某個列族 Table table = connection.getTable(tableName); table.delete(delete); } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。