注意:eclipse 调试 HBASE Java Api 需要把hbase-site.xml 和hdfs-site.xml配置文件拷贝到classpath下
mavne 加载依赖包(选择自己对应的版本):
<!-- hbase java api -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.1.2</version>
</dependency>
Hbase java api 例子:
package com.hqmart.util;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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.Table;
import org.apache.hadoop.hbase.util.Bytes;
public class HbaseUtils {
public static void main(String[] args) {
HbaseUtils.createTable("myTest", "myfc1", "myfc2", "myfc3");
HbaseUtils.insert("myTest", "1", "myfc1", "sex", "men");
HbaseUtils.insert("myTest", "1", "myfc1", "name", "xiaoming");
HbaseUtils.insert("myTest", "1", "myfc1", "age", "32");
HbaseUtils.insert("myTest", "2", "myfc2", "name", "xiaohong");
HbaseUtils.insert("myTest", "2", "myfc2", "sex", "woman");
HbaseUtils.insert("myTest", "2", "myfc2", "age", "23");
//得到一行下一个列族下的某列的数据
String result = HbaseUtils.byGet("myTest", "1", "myfc1", "name");
System.out.println("结果是的: " + result);
//得到一行下一个列族下的所有列的数据
Map<String, String> result1 = HbaseUtils.byGet("myTest", "1", "myfc1");
System.out.println("结果是的: " + result1);
//得到一行的所有列族的数据
Map<String, Map<String, String>> result2 = HbaseUtils.byGet("myTest", "1");
System.out.println("所有列族的数据是: "+result2);
System.out.println("结果是的: " + result2.get("myfc1"));
System.out.println("结果是的: " + result2.get("myfc2"));
// HbaseUtils.dropTable("myTest");
HbaseUtils.close();
}
private static Configuration conf;
private static Connection con;
// 初始化连接
static {
conf = HBaseConfiguration.create(); // 获得配制文件对象
conf.set("hbase.zookeeper.quorum", "172.xx.x.xxx,172.xx.x.xxx,172.xx.x.xxx");
conf.set("hbase.zookeeper.property.clientPort", "2181");
try {
con = ConnectionFactory.createConnection(conf);// 获得连接对象
} catch (IOException e) {
e.printStackTrace();
}
}
// 获得连接
public static Connection getCon() {
if (con == null || con.isClosed()) {
try {
con = ConnectionFactory.createConnection(conf);
} catch (IOException e) {
e.printStackTrace();
}
}
return con;
}
// 关闭连接
public static void close() {
if (con != null) {
try {
con.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 创建表
public static void createTable(String tableName, String... FamilyColumn) {
TableName tn = TableName.valueOf(tableName);
try {
Admin admin = getCon().getAdmin();
HTableDescriptor htd = new HTableDescriptor(tn);
for (String fc : FamilyColumn) {
HColumnDescriptor hcd = new HColumnDescriptor(fc);
htd.addFamily(hcd);
}
admin.createTable(htd);
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 删除表
public static void dropTable(String tableName) {
TableName tn = TableName.valueOf(tableName);
try {
Admin admin = con.getAdmin();
admin.disableTable(tn);
admin.deleteTable(tn);
admin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 插入或者更新数据
public static boolean insert(String tableName, String rowKey,
String family, String qualifier, String value) {
try {
Table t = getCon().getTable(TableName.valueOf(tableName));
Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier),
Bytes.toBytes(value));
t.put(put);
return true;
} catch (IOException e) {
e.printStackTrace();
} finally {
HbaseUtils.close();
}
return false;
}
// 删除
public static boolean del(String tableName, String rowKey, String family,
String qualifier) {
try {
Table t = getCon().getTable(TableName.valueOf(tableName));
Delete del = new Delete(Bytes.toBytes(rowKey));
if (qualifier != null) {
del.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
} else if (family != null) {
del.addFamily(Bytes.toBytes(family));
}
t.delete(del);
return true;
} catch (IOException e) {
e.printStackTrace();
} finally {
HbaseUtils.close();
}
return false;
}
//删除一行
public static boolean del(String tableName, String rowKey) {
return del(tableName, rowKey, null, null);
}
//删除一行下的一个列族
public static boolean del(String tableName, String rowKey, String family) {
return del(tableName, rowKey, family, null);
}
// 数据读取
//取到一个值
public static String byGet(String tableName, String rowKey, String family,
String qualifier) {
try {
Table t = getCon().getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowKey));
get.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier));
Result r = t.get(get);
return Bytes.toString(CellUtil.cloneValue(r.listCells().get(0)));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
//取到一个族列的值
public static Map<String, String> byGet(String tableName, String rowKey, String family) {
Map<String, String> result = null ;
try {
Table t = getCon().getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowKey));
get.addFamily(Bytes.toBytes(family));
Result r = t.get(get);
List<Cell> cs = r.listCells();
result = cs.size() > 0 ? new HashMap<String, String>() : result;
for (Cell cell : cs) {
result.put(Bytes.toString(CellUtil.cloneQualifier(cell)), Bytes.toString(CellUtil.cloneValue(cell)));
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
//取到多个族列的值
public static Map<String, Map<String, String>> byGet(String tableName, String rowKey) {
Map<String, Map<String, String>> results = null ;
try {
Table t = getCon().getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowKey));
Result r = t.get(get);
List<Cell> cs = r.listCells();
results = cs.size() > 0 ? new HashMap<String, Map<String, String>> () : results;
for (Cell cell : cs) {
String familyName = Bytes.toString(CellUtil.cloneFamily(cell));
if (results.get(familyName) == null)
{
results.put(familyName, new HashMap<String, String> ());
}
results.get(familyName).put(Bytes.toString(CellUtil.cloneQualifier(cell)), Bytes.toString(CellUtil.cloneValue(cell)));
}
} catch (IOException e) {
e.printStackTrace();
}
return results;
}
}