使用eclipse编写HBase的增删改查
HBase提供了java api来对HBase进行一系列的管理涉及到对表的管理、数据的操作等。常用的API操作有:
1、 对表的创建、删除、显示及修改
可以用HBaseAdmin,当创建了表,可以通过HTable的实例来访问表。
2、 插入数据
创建一个Put对象,在这个Put对象里可以指定要给哪个列增加数据,以及当前的时间戳等值,然后通过调用HTable.put(Put)来提交操作,在创建Put对象的时候,必须指定一个行(Row)值,在构造Put对象的时候作为参数传入。
3、 获取数据
要获取数据,使用Get对象,Get对象同Put对象一样有好几个构造函数,通常在构造的时候传入行值,表示取第几行的数据,通过HTable.get(Get)来调用。
4、 浏览每一行
通过Scan可以对表中的行进行浏览,得到每一行的信息,比如列名,时间戳等,Scan相当于一个游标,通过next()来浏览下一个,通过调用HTable.getScanner(Scan)来返回一个ResultScanner对象。HTable.get(Get)和HTable.getScanner(Scan)都是返回一个Result。Result是一个KeyValue的链表。
5、 删除
使用Delete来删除记录,通过调用HTable.delete(Delete)来执行删除操作。
6、 簇的访问
客户端代码通过ZooKeeper来访问找到簇,也就是说ZooKeeper quorum将被使用,那么相关的类(包)应该在客户端的类(classes)目录下,即客户端一定要找到文件hbase-site.xml。我选择将HBase下的conf配置文件夹导入到eclipse的该项目中。参考我的另一篇博文:MapReduce连接Hbase时报错及处理
下面是代码,亲测通过,可直接使用:
package com.hbase;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
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.util.Bytes;
public class HBaseCURD {
private static Configuration config = null;
static {
config = HBaseConfiguration.create();
}
// 创建新表
public static void createTable(String tableName, String[] columnFamilys) throws IOException {
HBaseAdmin admin = new HBaseAdmin(config);
if (admin.tableExists(tableName)) {
System.out.println("此表已存在!");
} else {
// HTableDescriptor contains the details about an HBase table such
// as
// the descriptors of all the column families, is the table a
// catalog table, -ROOT- or hbase:meta , if the table is read only,
// the maximum size of the memstore, when the region split should
// occur, coprocessors associated with it etc...
HTableDescriptor tableDesc = new HTableDescriptor(tableName);
for (int i = 0; i < columnFamilys.length; i++) {
tableDesc.addFamily(new HColumnDescriptor(columnFamilys[i]));
}
admin.createTable(tableDesc);
System.out.println(tableName + "表创建成功!");
}
}
// 删除表
public static void deleteTable(String tableName) throws IOException {
HBaseAdmin admin = new HBaseAdmin(config);
admin.disableTable(tableName);
admin.deleteTable(tableName);
System.out.println(tableName + "表删除成功!");
}
// 插入一条记录
public static void addRecord(String tableName, String rowKey, String clolumnFamily, String qualifier, String value)
throws IOException {
@SuppressWarnings("resource")
HTable table = new HTable(config, tableName);
Put put = new Put(Bytes.toBytes(rowKey));
put.add(Bytes.toBytes(clolumnFamily), Bytes.toBytes(qualifier), Bytes.toBytes(value));
table.put(put);
System.out.println("插入记录" + rowKey + "到表" + tableName);
}
// 删除一条记录
public static void deleteRecord(String tableName, String rowKey) throws IOException {
HTable table = new HTable(config, tableName);
List list = new ArrayList();
Delete del = new Delete(rowKey.getBytes());
list.add(del);
table.delete(list);
System.out.println("删除" + tableName + "表中的" + rowKey + "成功!");
}
// 查找数据
public static void getOneRecord(String tableName, String rowKey) throws IOException {
HTable table = new HTable(config, tableName);
Get get = new Get(rowKey.getBytes());
Result rs = table.get(get);
for (KeyValue kv : rs.raw()) {
System.out.println(new String(kv.getRow()) + " " + new String(kv.getFamily()) + ":"
+ new String(kv.getQualifier()) + " " + kv.getTimestamp() + " " + new String(kv.getValue()));
}
}
// 获取所有数据
public static void getAllRecord(String tableName) throws IOException {
HTable table = new HTable(config, tableName);
Scan s = new Scan();
ResultScanner ss = table.getScanner(s);
for (Result r : ss) {
for (KeyValue kv : r.raw()) {
System.out.println(new String(kv.getRow()) + " " + new String(kv.getFamily()) + ":"
+ new String(kv.getQualifier()) + " " + kv.getTimestamp() + " " + new String(kv.getValue()));
}
}
}
public static void main(String[] args) {
String tableName = "scores";
String[] clolumnFamilys = { "grade", "course" };
try {
HBaseCURD.createTable(tableName, clolumnFamilys);
HBaseCURD.addRecord(tableName, "myHbase", "grade", "", "5");
HBaseCURD.addRecord(tableName, "myHbase", "course", "", "90");
HBaseCURD.addRecord(tableName, "myHbase", "course", "math", "97");
// 在另一张表中添加数据
HBaseCURD.addRecord(tableName, "it", "grade", "", "4");
HBaseCURD.addRecord(tableName, "it", "course", "math", "89");
System.out.println("获取一条数据");
HBaseCURD.getOneRecord(tableName, "myHbase");
System.out.println("获取所有记录");
HBaseCURD.getAllRecord(tableName);
System.out.println("删除一条数据");
HBaseCURD.deleteRecord(tableName, "it");
HBaseCURD.getAllRecord(tableName);
System.out.println("获取所有记录");
HBaseCURD.getAllRecord(tableName);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
eclipse的运行结果:
还可以在hbase shell中查看,此处不给出了。