在使用HBase Java API 之前,大家首先要了解HBase Java API类,可参考博客:http://www.cnblogs.com/ggjucheng/p/3380267.html
几个相关类与HBase数据模型之间的对应关系
java类 | HBase数据模型 |
HBaseAdmin | 数据库(DataBase) |
HBaseConfiguration | |
HTable | 表(Table) |
HTableDescriptor | 列族(Column Family) |
Put | 列修饰符(Column Qualifier) |
Get | |
Scanner |
HBaseConfiguration:对HBase进行配置。
HBaseAdmin:提供了一个接口来管理HBase数据库的表信息。它提供的方法包括:创建表,删除表,列出表项,使表有效或无效,以及添加或删除表列族成员等。
HTableDescriptor:包含了表的名字极其对应表的列族
HColumnDescriptor:维护着关于列族的信息,例如版本号,压缩设置等。它通常在创建表或者为表添加列族的时候使用。列族被创建后不能直接修改,只能通过删除然后重新创建的方式。列族被删除的时候,列族里面的数据也会同时被删除。
- public class HBaseDemo {
- private Configuration conf = null;
- @Before
- public void init(){
- conf = HBaseConfiguration.create();
- //客户端连接zookeeper
- conf.set(”hbase.zookeeper.quorum”, “hadoop01,hadoop02,hadoop03”);
- }
- @Test
- public void testDrop() throws Exception{
- HBaseAdmin admin = new HBaseAdmin(conf);
- admin.disableTable(”account”);
- admin.deleteTable(”account”);
- admin.close();
- }
- /**
- *插入数据
- */
- @Test
- public void testPut() throws Exception{
- HTable table = new HTable(conf, “user”);
- //提供row key
- Put put = new Put(Bytes.toBytes(“rk0003”));
- //列族、列的标示符、值,参数都是字节数组
- put.add(Bytes.toBytes(”info”), Bytes.toBytes(“name”), Bytes.toBytes(“liuyan”));
- table.put(put);
- table.close();
- }
- /**
- *获取数据
- */
- @Test
- public void testGet() throws Exception{
- //HTablePool pool = new HTablePool(conf, 10);
- //HTable table = (HTable) pool.getTable(“user”);
- HTable table = new HTable(conf, “user”);
- Get get = new Get(Bytes.toBytes(“rk0001”));
- //get.addColumn(Bytes.toBytes(“info”), Bytes.toBytes(“name”));
- get.setMaxVersions(5);
- Result result = table.get(get);
- //String r = Bytes.toString(result.getValue(family, qualifier) );这个获取方法不常用
- for(KeyValue kv : result.list()){
- String family = new String(kv.getFamily());
- System.out.println(family);
- String qualifier = new String(kv.getQualifier());
- System.out.println(qualifier);
- System.out.println(new String(kv.getValue()));
- }
- table.close();
- }
- @Test
- public void testScan() throws Exception{
- HTablePool pool = new HTablePool(conf, 10);
- HTableInterface table = pool.getTable(”user”);
- Scan scan = new Scan(Bytes.toBytes(“rk0001”), Bytes.toBytes(“rk0002”));
- scan.addFamily(Bytes.toBytes(”info”));
- //结果集
- ResultScanner scanner = table.getScanner(scan);
- for(Result r : scanner){
- /**
- for(KeyValue kv : r.list()){
- String family = new String(kv.getFamily());
- System.out.println(family);
- String qualifier = new String(kv.getQualifier());
- System.out.println(qualifier);
- System.out.println(new String(kv.getValue()));
- }
- */
- byte[] value = r.getValue(Bytes.toBytes(“info”), Bytes.toBytes(“name”));
- System.out.println(new String(value));
- }
- pool.close();
- }
- @Test
- public void testDel() throws Exception{
- HTable table = new HTable(conf, “user”);
- Delete del = new Delete(Bytes.toBytes(“rk0001”));
- del.deleteColumn(Bytes.toBytes(”data”), Bytes.toBytes(“pic”));
- table.delete(del);
- table.close();
- }
- public static void main(String[] args) throws Exception {
- Configuration conf = HBaseConfiguration.create();
- conf.set(”hbase.zookeeper.quorum”, “hadoop01,hadoop02,hadoop03”);
- //ddl操作,传入conf,可知操作哪个hbase集群
- HBaseAdmin admin = new HBaseAdmin(conf);
- HTableDescriptor td = new HTableDescriptor(“account”);
- HColumnDescriptor cd = new HColumnDescriptor(“info”);
- cd.setMaxVersions(10);
- td.addFamily(cd);
- admin.createTable(td);
- admin.close();
- }
- public void createTable(String tableName, int maxVersion, String… cf){
- }
- }
public class HBaseDemo {
private Configuration conf = null;
@Before
public void init(){
conf = HBaseConfiguration.create();
//客户端连接zookeeper
conf.set("hbase.zookeeper.quorum", "hadoop01,hadoop02,hadoop03");
}
@Test
public void testDrop() throws Exception{
HBaseAdmin admin = new HBaseAdmin(conf);
admin.disableTable("account");
admin.deleteTable("account");
admin.close();
}
/**
*插入数据
*/
@Test
public void testPut() throws Exception{
HTable table = new HTable(conf, "user");
//提供row key
Put put = new Put(Bytes.toBytes("rk0003"));
//列族、列的标示符、值,参数都是字节数组
put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("liuyan"));
table.put(put);
table.close();
}
/**
*获取数据
*/
@Test
public void testGet() throws Exception{
//HTablePool pool = new HTablePool(conf, 10);
//HTable table = (HTable) pool.getTable("user");
HTable table = new HTable(conf, "user");
Get get = new Get(Bytes.toBytes("rk0001"));
//get.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"));
get.setMaxVersions(5);
Result result = table.get(get);
//String r = Bytes.toString(result.getValue(family, qualifier) );这个获取方法不常用
for(KeyValue kv : result.list()){
String family = new String(kv.getFamily());
System.out.println(family);
String qualifier = new String(kv.getQualifier());
System.out.println(qualifier);
System.out.println(new String(kv.getValue()));
}
table.close();
}
@Test
public void testScan() throws Exception{
HTablePool pool = new HTablePool(conf, 10);
HTableInterface table = pool.getTable("user");
Scan scan = new Scan(Bytes.toBytes("rk0001"), Bytes.toBytes("rk0002"));
scan.addFamily(Bytes.toBytes("info"));
//结果集
ResultScanner scanner = table.getScanner(scan);
for(Result r : scanner){
/**
for(KeyValue kv : r.list()){
String family = new String(kv.getFamily());
System.out.println(family);
String qualifier = new String(kv.getQualifier());
System.out.println(qualifier);
System.out.println(new String(kv.getValue()));
}
*/
byte[] value = r.getValue(Bytes.toBytes("info"), Bytes.toBytes("name"));
System.out.println(new String(value));
}
pool.close();
}
@Test
public void testDel() throws Exception{
HTable table = new HTable(conf, "user");
Delete del = new Delete(Bytes.toBytes("rk0001"));
del.deleteColumn(Bytes.toBytes("data"), Bytes.toBytes("pic"));
table.delete(del);
table.close();
}
public static void main(String[] args) throws Exception {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "hadoop01,hadoop02,hadoop03");
//ddl操作,传入conf,可知操作哪个hbase集群
HBaseAdmin admin = new HBaseAdmin(conf);
HTableDescriptor td = new HTableDescriptor("account");
HColumnDescriptor cd = new HColumnDescriptor("info");
cd.setMaxVersions(10);
td.addFamily(cd);
admin.createTable(td);
admin.close();
}
public void createTable(String tableName, int maxVersion, String... cf){
}
}