鲁春利的工作笔记,好记性不如烂笔头
Java客户端:
org.apache.hadoop.hbase.client.HTable类:该类的读写是非线程安全的,不再作为client API提供给开发用户使用,建议通过Table类替代。
@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;
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";
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 {
Connection connection = ConnectionFactory.createConnection(conf);
Admin admin = connection.getAdmin();
manageMain.listTables(admin);
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();
}
}
private void listTables (Admin admin) throws IOException {
TableName [] names = admin.listTableNames();
for (TableName tableName : names) {
LOG.info("Table Name is : " + tableName.getNameAsString());
}
}
private boolean isExists (Admin admin) throws IOException {
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;
}
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);
}
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);
}
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();
}
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);
}
}
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);
}
}
}
private void queryByFilter(Connection connection) {
// 简单分页过滤器示例程序
Filter filter = new PageFilter(15); // 每页15条数据
int totalRows = 0;
byte [] lastRow = null;
Scan scan = new Scan();
scan.setFilter(filter);
// 略
}
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);
}
}