hbase1.1.1客戶端使用入門
工作中用到hbase,作為一個程序員首先就是要知道怎么使用,快速入門:
-
引入maven依賴:
<dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>1.1.1</version> </dependency>
2. hbase配置初始化時候會檢查classpath路徑下的hbase-default.xml、hbase-site.xml,hbase-default.xml在hbase-common包中,我們在classpath路徑下加入文件hbase-site.xml,該文件最精簡配置:
<property>
<name>hbase.zookeeper.property.clientPort</name>
<value>2181</value>
</property>
<property>
<name>hbase.zookeeper.quorum</name>
<value>127.0.0.1</value>
</property> hbase.zookeeper.quorum: zookeeper集群地址,多個用逗號隔開
hbase.zookeeper.property.clientPort: zookeeper端口
3.代碼中創建連接后就可以進行建表及crud操作:
private static ThreadLocal<Map<String, Table>> threadLocal = new ThreadLocal<Map<String, Table>>();
// 初始化配置
static {
conf = HBaseConfiguration.create();
try {
connection = ConnectionFactory.createConnection(conf);
} catch (IOException e) {
logger.error("", e);
}
}
//獲取表
public static Table getTable(String tableName) throws IOException {
Map<String, Table> tables = threadLocal.get();
if(tables == null) {
tables = new HashMap<String, Table>(2);
threadLocal.set(tables);
}
Table table = null;
if(!tables.containsKey(tableName)) {
table = connection.getTable(TableName.valueOf(tableName));
tables.put(tableName, table);
} else {
table = tables.get(tableName);
}
return table;
}
//創建表
public static void createTable(String tableName) {
HBaseAdmin hBaseAdmin = null;
try {
hBaseAdmin = (HBaseAdmin) connection.getAdmin();
if (hBaseAdmin.tableExists(tableName)) {
System.out.println(tableName + " is exist....");
return ;
}
HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
tableDescriptor.addFamily(new HColumnDescriptor(Constants.FAMILY1));
hBaseAdmin.createTable(tableDescriptor);
} catch (Exception e) {
logger.error("", e);
} finally {
if(hBaseAdmin != null)
try {
hBaseAdmin.close();
} catch (IOException e) {
logger.error("", e);
}
}
}
// 添加多條數據
public static void addBatch(String tableName, List<Put> puts) {
Table table = null;
try {
table = getTable(tableName);
table.put(puts);
} catch (Exception e) {
logger.error("", e);
}
}
// 添加一條數據
public static void addRow(String tableName, Put put) {
Table table = null;
try {
table = getTable(tableName);
table.put(put);
} catch (Exception e) {
logger.error("", e);
}
}
// 刪除多條數據
public static void delMultiRows(String tableName, String[] rows) {
Table table = null;
try {
table = getTable(tableName);
List<Delete> delList = new ArrayList<Delete>();
for (String row : rows) {
Delete del = new Delete(Bytes.toBytes(row));
delList.add(del);
}
table.delete(delList);
} catch (Exception e) {
logger.error("", e);
}
}
// 刪除一條數據
public static void delSingleRow(String tableName, String row) {
Table table = null;
try {
table = getTable(tableName);
Delete del = new Delete(Bytes.toBytes(row));
table.delete(del);
} catch (Exception e) {
logger.error("", e);
}
}
// 獲取一條數據
public static Result getRow(String tableName, String row) {
Table table = null;
try {
table = getTable(tableName);
Get get = new Get(Bytes.toBytes(row));
Result result = table.get(get);
return result;
} catch (Exception e) {
logger.error("", e);
return null;
}
}
// 條件查詢
public static ResultScanner scanTable(String tableName, Scan scan) {
Table table = null;
try {
table = getTable(tableName);
ResultScanner results = table.getScanner(scan);
return results;
} catch (Exception e) {
logger.error("", e);
return null;
}
} 4. 需要在部署機器上添加hbase對應的hosts
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!