版本:Hadoop-2.2.0与Hbase-0.96.0
public abstract class AbstrUtils {
protected static Logger logger = Logger.getLogger(AbstrUtils.class);
protected static Configuration configuration = null;
/** 初始化配置 **/
static {
System.setProperty("hadoop.home.dir", "D:/develop/data/hadoop/hadoop-2.2.0");
System.setProperty("HADOOP_MAPRED_HOME", "D:/develop/data/hadoop/hadoop-2.2.0");
System.setProperty("SQOOP_CONF_DIR", "D:/develop/data/sqoop/sqoop-1.4.4-hadoop-2.0.4");
configuration = new Configuration();
/** 与hbase/conf/hbase-site.xml中hbase.master配置的值相同 */
configuration.set("hbase.master", "192.168.10.10:60000");
/** 与hbase/conf/hbase-site.xml中hbase.zookeeper.quorum配置的值相同 */
configuration.set("hbase.zookeeper.quorum", "192.168.10.10");
/** 与hbase/conf/hbase-site.xml中hbase.zookeeper.property.clientPort配置的值相同 */
configuration.set("hbase.zookeeper.property.clientPort", "2181");
//configuration = HBaseConfiguration.create(configuration);
}
}
public class HBaseUtils extends AbstrUtils {
private static HBaseAdmin admin = null;
private static HTablePool tablePool = null;
static {
try {
admin = new HBaseAdmin(configuration);
tablePool = new HTablePool(configuration, 10);
tablePool.close();
} catch (IOException e) {
logger.info(e.getMessage(), e);
}
}
/** 创建一张表*/
public static void creatTable(String tableName, String[] familys) {
try {
if (admin.tableExists(tableName)) {
logger.info("table "+ tableName + " already exists!");
} else {
HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf(tableName));
for (int i = 0; i < familys.length; i++) {
tableDesc.addFamily(new HColumnDescriptor(familys[i]));
}
admin.createTable(tableDesc);
logger.info("create table " + tableName + " success.");
}
} catch (Exception e) {
logger.info(e.getMessage(), e);
}
}
/** 删除表*/
public static void deleteTable(String tableName) {
try {
admin.disableTable(tableName);
admin.deleteTable(tableName);
logger.info("delete table " + tableName + " success.");
} catch (Exception e) {
logger.info(e.getMessage(), e);
}
}
/** 插入一行记录*/
@SuppressWarnings("resource")
public static void putRecord(String tableName, String rowKey, String family, String qualifier, String value) {
try {
HTable table = new HTable(configuration, tableName);
Put put = new Put(Bytes.toBytes(rowKey));
put.add(Bytes.toBytes(family), Bytes.toBytes(qualifier), Bytes.toBytes(value));
table.put(put);
logger.info("insert recored " + rowKey + " to table " + tableName + " success.");
} catch (IOException e) {
logger.info(e.getMessage(), e);
}
}
/** 批量插入记录*/
public static void putRecords(String tableName, List<Put> puts) {
HTable table = null;
try {
table = new HTable(configuration, tableName);
table.put(puts);
} catch (IOException e) {
logger.info(e.getMessage(), e);
try {
table.flushCommits();
} catch (Exception e1) {
logger.info(e1.getMessage(), e1);
}
}
}
/** 删除一行记录*/
@SuppressWarnings("resource")
public static void deleteRecord(String tableName, String... rowKeys) {
try {
HTable table = new HTable(configuration, tableName);
List<Delete> list = new ArrayList<Delete>();
Delete delete = null;
for (String rowKey : rowKeys) {
delete = new Delete(rowKey.getBytes());
list.add(delete);
}
if (list.size() > 0) {
table.delete(list);
}
logger.info("delete recoreds " + rowKeys + " success.");
} catch (IOException e) {
logger.info(e.getMessage(), e);
}
}
/** 查找一行记录*/
@SuppressWarnings({ "resource"})
public static Result getRecord(String tableName, String rowKey) {
try {
HTable table = new HTable(configuration, tableName);
Get get = new Get(rowKey.getBytes());
get.setMaxVersions();
return table.get(get);
} catch (IOException e) {
logger.info(e.getMessage(), e);
}
return null;
}
/** 查找所有记录*/
@SuppressWarnings({"resource" })
public static ResultScanner getRecords(String tableName) {
try {
HTable table = new HTable(configuration, tableName);
return table.getScanner(new Scan());
} catch (IOException e) {
logger.info(e.getMessage(), e);
}
return null;
}
public static void printRecord(Result result) {
for (Cell cell : result.rawCells()) {
logger.info("cell row: " + new String(cell.getRowArray()));
logger.info("cell family: " + new String(cell.getFamilyArray()));
logger.info("cell qualifier: " + new String(cell.getQualifierArray()));
logger.info("cell value: " + new String(cell.getValueArray()));
logger.info("cell timestamp: " + cell.getTimestamp());
}
/** 之前版本*/
/**
for (KeyValue kv : rs.raw()) {
System.out.print(new String(kv.getRow()) + " ");
System.out.print(new String(kv.getFamily()) + ":");
System.out.print(new String(kv.getQualifier()) + " ");
System.out.print(kv.getTimestamp() + " ");
System.out.println(new String(kv.getValue()));
}
*/
}
public static void printRecords(ResultScanner resultScanner) {
for (Result result : resultScanner) {
printRecord(result);
}
}
}
版本:Hadoop-2.7.2与Hbase-1.2.3
public class HBaseUtils extends AbstrUtils {
private static HBaseAdmin admin = null;
private static Connection connection = null;
static {
try {
ExecutorService pool = Executors.newCachedThreadPool();
connection = ConnectionFactory.createConnection(configuration, pool);
admin = (HBaseAdmin) connection.getAdmin();
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
}
/** 创建一张表*/
public static void creatTable(String tableName, String[] familys) {
try {
if (admin.tableExists(tableName)) {
LOG.info("table "+ tableName + " already exists!");
} else {
HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf(tableName));
for (int i = 0; i < familys.length; i++) {
tableDesc.addFamily(new HColumnDescriptor(familys[i]));
}
admin.createTable(tableDesc);
LOG.info("create table " + tableName + " success.");
}
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
}
/** 表注册Coprocessor*/
public static void addTableCoprocessor(String tableName, String coprocessorClassName) {
try {
admin.disableTable(tableName);
HTableDescriptor htd = admin.getTableDescriptor(Bytes.toBytes(tableName));
htd.addCoprocessor(coprocessorClassName);
admin.modifyTable(Bytes.toBytes(tableName), htd);
admin.enableTable(tableName);
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
}
/** 统计表行数*/
public static long rowCount(String tableName) {
long rowCount = 0;
try {
HTable table = (HTable) connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
// scan.setFilter(new KeyOnlyFilter());
scan.setFilter(new FirstKeyOnlyFilter());
ResultScanner resultScanner = table.getScanner(scan);
for (Result result : resultScanner) {
rowCount += result.size();
}
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
return rowCount;
}
/** 插入一行记录*/
public static void insertRecord(String tableName, String rowKey, String family, String qualifier, String value) {
try {
HTable table = (HTable) connection.getTable(TableName.valueOf(tableName));
Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier), Bytes.toBytes(value));
table.put(put);
LOG.info("insert recored " + rowKey + " to table " + tableName + " success.");
} catch (IOException e) {
LOG.info(e.getMessage(), e);
}
}
/** 批量插入记录*/
public static void insertRecords(String tableName, List<Put> puts) {
HTable table = null;
try {
table = (HTable) connection.getTable(TableName.valueOf(tableName));
table.put(puts);
} catch (IOException e) {
LOG.info(e.getMessage(), e);
try {
table.flushCommits();
} catch (Exception e1) {
LOG.info(e1.getMessage(), e1);
}
}
}
/** 删除一行记录*/
public static void deleteRecord(String tableName, String... rowKeys) {
try {
HTable table = (HTable) connection.getTable(TableName.valueOf(tableName));
List<Delete> list = new ArrayList<Delete>();
Delete delete = null;
for (String rowKey : rowKeys) {
delete = new Delete(rowKey.getBytes());
list.add(delete);
}
if (list.size() > 0) {
table.delete(list);
}
LOG.info("delete recoreds " + rowKeys + " success.");
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
}
/** 删除一个列族*/
public static void deleteFamily(String tableName, String columnName) {
try {
admin.deleteColumn(tableName, columnName);
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
}
/** 删除表*/
public static void deleteTable(String tableName) {
try {
admin.disableTable(tableName);
admin.deleteTable(tableName);
LOG.info("delete table " + tableName + " success.");
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
}
/** 查找一行记录*/
public static Result getRecord(String tableName, String rowKey) {
try {
HTable table = (HTable) connection.getTable(TableName.valueOf(tableName));
Get get = new Get(rowKey.getBytes());
get.setMaxVersions();
return table.get(get);
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
return null;
}
/** 查找所有记录*/
public static ResultScanner getRecords(String tableName) {
return getRecords(tableName, null, null, null, null, null);
}
/** 查找所有记录*/
public static ResultScanner getRecords(String tableName, String family) {
return getRecords(tableName, family, null, null, null, null);
}
/** 查找所有记录*/
public static ResultScanner getRecords(String tableName, String family, String qualifier) {
return getRecords(tableName, family, qualifier, null, null, null);
}
/** 查找所有记录*/
public static ResultScanner getRecords(String tableName, Filter filter) {
return getRecords(tableName, null, null, null, null, filter);
}
/** 查找所有记录*/
public static ResultScanner getRecords(String tableName, String family, String qualifier,
String startRow, String stopRow, Filter filter) {
try {
HTable table = (HTable) connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
if (!StringUtils.isBlank(family)) scan.addFamily(Bytes.toBytes(family));
if (!StringUtils.isBlank(family) && !StringUtils.isBlank(qualifier))
scan.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
if (!StringUtils.isBlank(startRow)) scan.setStartRow(Bytes.toBytes(startRow));
if (!StringUtils.isBlank(stopRow)) scan.setStopRow(Bytes.toBytes(stopRow));
if (null != filter) scan.setFilter(filter);
return table.getScanner(scan);
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
return null;
}
/** 查找所有记录*/
public static ResultScanner getRecords(String tableName, Scan scan) {
try {
HTable table = (HTable) connection.getTable(TableName.valueOf(tableName));
return table.getScanner(scan);
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
return null;
}
public static void printRecord(Result result) {
try {
List<Cell> cells= result.listCells();
for (Cell cell : cells) {
String row = new String(result.getRow(), "UTF-8");
String family = new String(CellUtil.cloneFamily(cell), "UTF-8");
String qualifier = new String(CellUtil.cloneQualifier(cell), "UTF-8");
String value = new String(CellUtil.cloneValue(cell), "UTF-8");
System.out.println("[row:"+row+"],[family:"+family+"],[qualifier:"+qualifier+"],[value:"+value+"]");
}
} catch (UnsupportedEncodingException e) {
LOG.error(e.getMessage(), e);
}
}
public static void printRecords(ResultScanner resultScanner) {
for (Result result : resultScanner) {
printRecord(result);
}
}
}