一、包的依赖
包的话用maven就很方便的下载了
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.1.2</version>
<scope>runtime</scope>
<exclusions>
<exclusion>
<artifactId>jdk.tools</artifactId>
<groupId>jdk.tools</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.6.2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
二、API的使用
1、首先,创建连接
private static Configuration conf;
private static Connection con;
// 初始化连接
static {
conf = HBaseConfiguration.create(); // 获得配制文件对象
//设置zookeeper主机IP
conf.set("hbase.zookeeper.quorum", "192.168.1.123");
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();
}
}
}
这里使用静态块确保只初始化一次连接。并使用单例模式确保同时只存在一个连接。
2、封装的创建table的方法
// 创建表
public static void createTable(String tableName, String... FamilyColumn) {
TableName tn = TableName.valueOf(tableName);
try {
Admin admin = getCon().getAdmin(); //通过连接获取Admin
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();
}
}
3、封装的删除table方法
// 删除表
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();
}
}
这里注意,在删除表之前一定要将表的状态改为不可操作。
3、封装的插入或更新方法
// 插入或者更新数据
public static boolean insert(String tableName, String rowKey,
String family, String qualifier, String value) {
try {
Table t = getCon().getTable(TableName.valueOf(tableName)); //获得所要进行操作的table
Put put = new Put(Bytes.toBytes(rowKey)); //使用Put进行插入
put.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier),
Bytes.toBytes(value)); //设置列的列族,列名,和内容
t.put(put);
return true;
} catch (IOException e) {
e.printStackTrace();
} finally {
HBaseUtil.close();
}
return false;
}
因为hbase的对相同列的数据只显示给用户最新修改的,所以其实更新也就是插入罢了。
4、封装的删除内容方法
// 删除
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)); //使用Delete来创建要删除的对象
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 {
HBaseUtil.close();
}
return false;
}
这里封装的是可以通过列族删除或者通过列名删除。
5、封装的数据读取方法
// 数据读取
//取到一个值
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创建要获取的对象
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)); //只确定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;
}
扯点题外话,Hbase的jar包要求真的是十分严格,甚至感觉有点苛刻!一定要版本相同,还对hadoop的版本有所要求。他不同版本竟然包的结构都有所不同,所以实践时一定要注意版本问题。