| java类 | HBase数据模型 |
| HBaseAdmin | 数据库(DataBase) |
| HBaseConfiguration | |
| HTable | 表(Table) |
| HTableDescriptor | 列族(Column Family) |
| Put | 列修饰符(Column Qualifier) |
| Get | |
| Scanner |
| 返回值 | 函数 | 描述 |
| void | addResource(Path file) | 通过给定的路径所指的文件来添加资源 |
| void | clear() | 清空所有已设置的属性 |
| string | get(String name) | 获取属性名对应的值 |
| String | getBoolean(String name, boolean defaultValue) | 获取为boolean类型的属性值,如果其属性值类型部位boolean,则返回默认属性值 |
| void | set(String name, String value) | 通过属性名来设置值 |
| void | setBoolean(String name, boolean value) | 设置boolean类型的属性值 |
- HBaseConfiguration hconfig = new HBaseConfiguration();
- hconfig.set("hbase.zookeeper.property.clientPort","2181");
| 返回值 | 函数 | 描述 |
| void | addColumn(String tableName, HColumnDescriptor column) | 向一个已经存在的表添加咧 |
| checkHBaseAvailable(HBaseConfiguration conf) | 静态函数,查看HBase是否处于运行状态 | |
| createTable(HTableDescriptor desc) | 创建一个表,同步操作 | |
| deleteTable(byte[] tableName) | 删除一个已经存在的表 | |
| enableTable(byte[] tableName) | 使表处于有效状态 | |
| disableTable(byte[] tableName) | 使表处于无效状态 | |
| HTableDescriptor[] | listTables() | 列出所有用户控件表项 |
| void | modifyTable(byte[] tableName, HTableDescriptor htd) | 修改表的模式,是异步的操作,可能需要花费一定的时间 |
| boolean | tableExists(String tableName) | 检查表是否存在 |
- HBaseAdmin admin = new HBaseAdmin(config);
- admin.disableTable("tablename")
| 返回值 | 函数 | 描述 |
| void | addFamily(HColumnDescriptor) | 添加一个列族 |
| HColumnDescriptor | removeFamily(byte[] column) | 移除一个列族 |
| byte[] | getName() | 获取表的名字 |
| byte[] | getValue(byte[] key) | 获取属性的值 |
| void | setValue(String key, String value) | 设置属性的值 |
- HTableDescriptor htd = new HTableDescriptor(table);
- htd.addFamily(new HcolumnDescriptor("family"));
| 返回值 | 函数 | 描述 |
| byte[] | getName() | 获取列族的名字 |
| byte[] | getValue(byte[] key) | 获取对应的属性的值 |
| void | setValue(String key, String value) | 设置对应属性的值 |
- HTableDescriptor htd = new HTableDescriptor(tablename);
- HColumnDescriptor col = new HColumnDescriptor("content:");
- htd.addFamily(col);
| 返回值 | 函数 | 描述 |
| void | checkAdnPut(byte[] row, byte[] family, byte[] qualifier, byte[] value, Put put | 自动的检查row/family/qualifier是否与给定的值匹配 |
| void | close() | 释放所有的资源或挂起内部缓冲区中的更新 |
| Boolean | exists(Get get) | 检查Get实例所指定的值是否存在于HTable的列中 |
| Result | get(Get get) | 获取指定行的某些单元格所对应的值 |
| byte[][] | getEndKeys() | 获取当前一打开的表每个区域的结束键值 |
| ResultScanner | getScanner(byte[] family) | 获取当前给定列族的scanner实例 |
| HTableDescriptor | getTableDescriptor() | 获取当前表的HTableDescriptor实例 |
| byte[] | getTableName() | 获取表名 |
| static boolean | isTableEnabled(HBaseConfiguration conf, String tableName) | 检查表是否有效 |
| void | put(Put put) | 向表中添加值 |
- HTable table = new HTable(conf, Bytes.toBytes(tablename));
- ResultScanner scanner = table.getScanner(family);
| 返回值 | 函数 | 描述 |
| Put | add(byte[] family, byte[] qualifier, byte[] value) | 将指定的列和对应的值添加到Put实例中 |
| Put | add(byte[] family, byte[] qualifier, long ts, byte[] value) | 将指定的列和对应的值及时间戳添加到Put实例中 |
| byte[] | getRow() | 获取Put实例的行 |
| RowLock | getRowLock() | 获取Put实例的行锁 |
| long | getTimeStamp() | 获取Put实例的时间戳 |
| boolean | isEmpty() | 检查familyMap是否为空 |
| Put | setTimeStamp(long timeStamp) | 设置Put实例的时间戳 |
- HTable table = new HTable(conf,Bytes.toBytes(tablename));
- Put p = new Put(brow);//为指定行创建一个Put操作
- p.add(family,qualifier,value);
- table.put(p);
| 返回值 | 函数 | 描述 |
| Get | addColumn(byte[] family, byte[] qualifier) | 获取指定列族和列修饰符对应的列 |
| Get | addFamily(byte[] family) | 通过指定的列族获取其对应列的所有列 |
| Get | setTimeRange(long minStamp,long maxStamp) | 获取指定取件的列的版本号 |
| Get | setFilter(Filter filter) | 当执行Get操作时设置服务器端的过滤器 |
- HTable table = new HTable(conf, Bytes.toBytes(tablename));
- Get g = new Get(Bytes.toBytes(row));
| 返回值 | 函数 | 描述 |
| boolean | containsColumn(byte[] family, byte[] qualifier) | 检查指定的列是否存在 |
| NavigableMap | getFamilyMap(byte[] family) | 获取对应列族所包含的修饰符与值的键值对 |
| byte[] | getValue(byte[] family, byte[] qualifier) | 获取对应列的最新值 |
| 返回值 | 函数 | 描述 |
| void | close() | 关闭scanner并释放分配给它的资源 |
| Result | next() | 获取下一行的值 |
Java API应用
-
Configuration
在使用Java API时,Client端需要知道HBase的配置环境,如存储地址,zookeeper等信息。这些信息通过Configuration对象来封装,可通过如下代码构建该对象:
Configuration config=HBaseConfiguration.create();
在调用HBaseConfiguration.create()方法时,HBase首先会在classpath下查找hbase-site.xml文件,将里面的信息解析出来封装到Configuration对象中,如果hbase-site.xml文件不存在,则使用默认的hbase-core.xml文件。
除了将hbase-site.xml放到classpath下,开发人员还可通过config.set(name, value)方法来手工构建Configuration对象:
Configuration.set(String name, String value);
-
HBaseAdmin
HBaseAdmin用于创建数据库表格,并管理表格的元数据信息,通过如下方法构建:
HBaseAdmin admin=new HBaseAdmin(config);
常用方法如列表所示:
Table 1. HBaseAdmin常用方法
方法名 方法描述 addColumn(tableName,column) 为表格添加栏位 deleteColumn(tableName,column) 删除指定栏位 balanceSwitch(boolean) 是否启用负载均衡 createTable(HTableDescriptor desc) 创建表格 deleteTable(tableName) 删除表格 tableExists(tableName) 判断表格是否存在
-
HTable
在HBase中,HTable封装表格对象,对表格的增删改查操作主要通过它来完成,构造方法如下:
HTable table=new HTable(config,tableName);
在构建多个HTable对象时,HBase推荐所有的HTable使用同一个Configuration。这样,HTable之间便可共享HConnection对象、zookeeper信息以及Region地址的缓存信息。
Example 3. Put操作
Put put=new Put(rowKey); put.add(columnFamily,column,value); table.put(put); 注:在HBase中,实体的新增和更新都是通过Put操作来实现。
Example 5. Scan操作
Scan scan=new Scan( ); scan.addColumn(columnFamily,column); scan.setReversed(false); scan.setBatch(10); scan.setIsolationLevel(IsolationLevel.READ_COMMITTED); scan.setRaw(false); scan.setSmall(true); SingleColumnValueFilter filter=new SingleColumnValueFilter( columnFamily,column, CompareOp.EQUAL,value ); scan.setFilter(filter); ResultScanner scanner=table.getScanner(scan); Iterator<Result> res=scanner.iterator( );
注:更多的过滤器信息请查看org.apache.hadoop.hbase.filter包。
Example 6. 遍历Result结果集
NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> maps = res.getMap(); for(byte[] family:maps.keySet()){ System.out.println("columnFamily:"+new String(family)); for(byte[] column:maps.get(family).keySet()){ System.out.print("column:"+new String(column)+"; "); for(Long time:maps.get(family).get(column).keySet()){ System.out.println("value:"+new String(maps.get(family).get(column).get(time))); } } }
package com.hbase;
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;
/**
* @author 三劫散仙
*
* **/
public class Test {
static Configuration conf=null;
static{
conf=HBaseConfiguration.create();//hbase的配置信息
conf.set("hbase.zookeeper.quorum", "10.2.143.5"); //zookeeper的地址
}
public static void main(String[] args)throws Exception {
Test t=new Test();
//t.createTable("temp", new String[]{"name","age"});
//t.insertRow("temp", "2", "age", "myage", "100");
// t.getOneDataByRowKey("temp", "2");
t.showAll("temp");
}
/***
* 创建一张表
* 并指定列簇
* */
public void createTable(String tableName,String cols[])throws Exception{
HBaseAdmin admin=new HBaseAdmin(conf);//客户端管理工具类
if(admin.tableExists(tableName)){
System.out.println("此表已经存在.......");
}else{
HTableDescriptor table=new HTableDescriptor(tableName);
for(String c:cols){
HColumnDescriptor col=new HColumnDescriptor(c);//列簇名
table.addFamily(col);//添加到此表中
}
admin.createTable(table);//创建一个表
admin.close();
System.out.println("创建表成功!");
}
}
/**
* 添加数据,
* 建议使用批量添加
* @param tableName 表名
* @param row 行号
* @param columnFamily 列簇
* @param column 列
* @param value 具体的值
*
* **/
public void insertRow(String tableName, String row,
String columnFamily, String column, String value) throws Exception {
HTable table = new HTable(conf, tableName);
Put put = new Put(Bytes.toBytes(row));
// 参数出分别:列族、列、值
put.add(Bytes.toBytes(columnFamily), Bytes.toBytes(column),
Bytes.toBytes(value));
table.put(put);
table.close();//关闭
System.out.println("插入一条数据成功!");
}
/**
* 删除一条数据
* @param tableName 表名
* @param row rowkey
* **/
public void deleteByRow(String tableName,String rowkey)throws Exception{
HTable h=new HTable(conf, tableName);
Delete d=new Delete(Bytes.toBytes(rowkey));
h.delete(d);//删除一条数据
h.close();
}
/**
* 删除多条数据
* @param tableName 表名
* @param row rowkey
* **/
public void deleteByRow(String tableName,String rowkey[])throws Exception{
HTable h=new HTable(conf, tableName);
List<Delete> list=new ArrayList<Delete>();
for(String k:rowkey){
Delete d=new Delete(Bytes.toBytes(k));
list.add(d);
}
h.delete(list);//删除
h.close();//释放资源
}
/**
* 得到一条数据
*
* @param tableName 表名
* @param rowkey 行号
* ***/
public void getOneDataByRowKey(String tableName,String rowkey)throws Exception{
HTable h=new HTable(conf, tableName);
Get g=new Get(Bytes.toBytes(rowkey));
Result r=h.get(g);
for(KeyValue k:r.raw()){
System.out.println("行号: "+Bytes.toStringBinary(k.getRow()));
System.out.println("时间戳: "+k.getTimestamp());
System.out.println("列簇: "+Bytes.toStringBinary(k.getFamily()));
System.out.println("列: "+Bytes.toStringBinary(k.getQualifier()));
//if(Bytes.toStringBinary(k.getQualifier()).equals("myage")){
// System.out.println("值: "+Bytes.toInt(k.getValue()));
//}else{
String ss= Bytes.toString(k.getValue());
System.out.println("值: "+ss);
//}
}
h.close();
}
/**
* 扫描所有数据或特定数据
* @param tableName
* **/
public void showAll(String tableName)throws Exception{
HTable h=new HTable(conf, tableName);
Scan scan=new Scan();
//扫描特定区间
//Scan scan=new Scan(Bytes.toBytes("开始行号"),Bytes.toBytes("结束行号"));
ResultScanner scanner=h.getScanner(scan);
for(Result r:scanner){
System.out.println("==================================");
for(KeyValue k:r.raw()){
System.out.println("行号: "+Bytes.toStringBinary(k.getRow()));
System.out.println("时间戳: "+k.getTimestamp());
System.out.println("列簇: "+Bytes.toStringBinary(k.getFamily()));
System.out.println("列: "+Bytes.toStringBinary(k.getQualifier()));
//if(Bytes.toStringBinary(k.getQualifier()).equals("myage")){
// System.out.println("值: "+Bytes.toInt(k.getValue()));
//}else{
String ss= Bytes.toString(k.getValue());
System.out.println("值: "+ss);
//}
}
}
h.close();
}
}
显示所有数据的打印输出如下:
==================================
行号: 1
时间戳: 1385597699287
列簇: name
列: myname
值: 秦
==================================
行号: 2
时间戳: 1385598393306
列簇: age
列: myage
值: 100
行号: 2
时间戳: 1385597723900
列簇: name
列: myname
值: 三
http://blog.sina.com.cn/s/blog_68674da70102v6q6.html
http://blog.csdn.net/javaman_chen/article/details/7220216













所有评论(0)