初始化
Configuration HBASE_CONFIG = new Configuration();
HBASE_CONFIG.set("hbase.zookeeper.quorum", "IP");
HBASE_CONFIG.set("hbase.zookeeper.property.clientPort", "端口");
cfg = new HBaseConfiguration(HBASE_CONFIG);
cfg.set("hbase.master", "master ip:60000");
创建表
public void createTable(String tablename) throws IOException {
HBaseAdmin admin = new HBaseAdmin(cfg);
if (admin.tableExists(tablename)) {
System.out.println("Exists!!!");
} else {
HTableDescriptor tableDesc = new HTableDescriptor(tablename);
tableDesc.addFamily(new HColumnDescriptor("name"));
admin.createTable(tableDesc);
System.out.println("create table ok .");
}
}
查询表的所有数据
public void getAll(String tablename) throws IOException {
HTable table = new HTable(cfg, tablename);
Scan s = new Scan();
ResultScanner rs = table.getScanner(s);
for (Result r : rs) {
KeyValue[] kv = r.raw();
for (int i = 0; i < kv.length; i++) {
System.out.print(new String(kv[i].getRow()) + "\t");
System.out.print("column=" + new String(kv[i].getFamily())+ ":");
System.out.print(new String(kv[i].getQualifier()) + ", ");
System.out.print("timestamp=" + kv[i].getTimestamp() + ", ");
System.out.println("value=" + new String(kv[i].getValue()));
}
}
}
查询一条数据
public void get(String tablename, String rowKey) throws IOException {
HTable table = new HTable(cfg, tablename);
Get g = new Get(rowKey.getBytes());
Result rs = table.get(g);
for (KeyValue kv : rs.raw()) {
System.out.print(new String(kv.getRow()) + ":");
System.out.print("column=" + new String(kv.getFamily()) + ":");
System.out.print(new String(kv.getQualifier()) + ", ");
System.out.print("timestamp=" + kv.getTimestamp() + ", ");
System.out.println("value=" + new String(kv.getValue()));
}
}
更新数据
public void update(String tablename,String rowKey,String value) throws IOException {
HTable table = new HTable(cfg, tablename);
Put put = new Put(Bytes.toBytes(rowKey));
put.add(Bytes.toBytes("name"), Bytes.toBytes("column"),Bytes.toBytes(value));
table.put(put);
}
删除一条数据(也可以删除多条)
public void delete(String tablename, String rowKey) throws IOException {
HTable table = new HTable(cfg, tablename);
List<Delete> list = new ArrayList<Delete>();
Delete d1 = new Delete(rowKey.getBytes());
list.add(d1);
table.delete(list);
}
查询多条
public void multiGet(String tablename,String[] keys) throws IOException {
HTable table = new HTable(cfg, tablename);
List<Get> list = new ArrayList<Get>();
for(String k:keys) {
Get g = new Get(k.getBytes());
list.add(g);
}
Result[] rs = table.get(list);
}
删除表
public void removeTable(String tablename) throws IOException {
HBaseAdmin admin = new HBaseAdmin(cfg);
admin.disableTable(tablename);
admin.deleteTable(tablename);
}
增加过滤器
public void filter(String tablename, String key,int count) throws IOException {
HTable table = new HTable(cfg, tablename);
Scan s = new Scan();
List<Filter> list = new ArrayList<Filter>();
list.add(new PrefixFilter(key.getBytes()));
list.add(new PageFilter(count));
FilterList filter = new FilterList(list);
s.setFilter(filter);
ResultScanner rs = table.getScanner(s);
}