这里使用的版本是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);
}
}
}