数据库:HBaseConfiguration HBaseAdmin
表:HTable HTableDescriptor
列族:HColumnDescriptor
行列操作:Put Get Scanner
HBaseConfiguration:
Configuration create()
//从classpath中查找hbase-site.xml初始化Configuration,如果没有,默认读取HBase-版本号.jar中的默认配置文件
void merger(Configuration destConf, Configuration srcConf) //合并两个Configuration
用法:Configuration config = HBaseConfiguration.create();
HBaseAdmin:
管理HBase数据库信息,可以创建表、删除表、列出表项、使表有效或无效、添加删除列族
void addColumn (String tableName, HColumnDescriptor column) //添加列族
void checkHBaseAvailable (HBaseConfiguration conf) //查看HBase状态,静态函数
void createTable (HTableDescriptor desc) //创建一个新表,同步操作
void deleteTable (byte[] tableName) //删除表
void enableTable (byte[] tableName) //使表有效
void disableTable (byte[] tableName) //使表无效
HTableDescriptor[] listTable() //列出所有用户空间表项
void modifyTable (byte[] tableName, HTableDescriptor htd) //修改表模式,异步操作,耗时
boolean tableExists (String tableName); //检查表是否存在
用法:HBaseAdmin admin = new HBaseAdmin(config); admin.disableTable(Bytes.toBytes("tablename"));
HTableDescriptor:
包含了表的名字及列族
void addFamily (HColumnDescriptor cdesc) //添加一个列族
HColumnDescriptor removeFamily ( byte[] column) //移除一个列族
byte[] getName () //获取表名
byte[] getValue (byte[] key) //获取属性值
void setValue (String key, String value ) //设置属性值
用法:
HTableDescriptor htd = new HTableDescriptor ("tableName");
htd.addFamily (new HColumnDescriptor("Family"));
HColumnDescriptor:
维护列族信息,如版本号、压缩设置等,通常在创建表或添加列族时使用
列族被创建后不能直接修改,只能删除后重建
列族被删除后,对应数据一同被删除
byte[] getName() //获取列族名字
byte[] getValue() //获取对应属性值
void setValue (String key, String value) //设置对应属性值
HTable:
用来与HBase表进行通信,非线程安全,若有多个线程,使用HTablePool
void checkAndPut (byte[] row, byte[] family, byte[] quaalifier, byte[] value, Put put) //自动检查row/family/qualifier/是否与给定的值匹配
void close() //释放资源
boolean exists (Get get) //检查get锁指定的值是否存在
Result get(Get get) //取出指定行的某些单元格对应的值
byte[][] getEndKeys () //获取表每个Region的结束键值
ResultScanner getScanner (byte[] family) //获取给定列族的Scanner实例
HTableDescriptor getTableDescriptor() //获取当前表的HTableDescriptor实例
byte[] getTableName() //获取表名
void put (Put put) //向表中添加值
用法:
HTable table = new HTable (conf, Bytes.toBytes("tablename"));
ResultScanner scanner = table.getScanner (Bytes.toBytes("cf"));
Put:
添加数据
Put add (byte[] family, byte[] quaalifier, byte[] value)
Put add (byte[] family, byte[] quaalifier, long ts, byte[] value)
List<Key Value> get (byte[] family, byte[] qualifier) //返回与指定 列族:列 匹配的项
boolean has (byte[] family, byte[] qualifier) //检查是否有 列族:列
用法:
HTable table = new HTable(conf, Bytes.toBytes("tablename"));
Put p = new Put(Bytes.toBytes("row")); //为指定行建一个Put操作
p.add(Bytes.toBytes("family"), Bytes.toBytes("qulifier"), Bytes.toBytes("tvalue"));
table.put(p);
Get:
获取单行信息
Get addColumn (byte[] family, byte[] qualifier)
Get addFamily (byte[] family)
Get setTimeRange (long minStamp, long maxStamp)
Get setFilter (Filter filter)
用法:
HTable table = new HTable(conf, Bytes.toBytes("tablename"));
Get g = new Get(Bytes.toBytes("row")); //为指定行建一个Get操作
Result result = table.get(g);
Result:
存储Get或Scan操作后获取的表的单行值
boolean containsColumn (byte[] family, byte[] qualifier)
NavigableMap<byte[], byte[]> getFamilyMap (byte[] family) //返回列族的qulifier:value们
byte[] getValue (byte[] family, byte[] qualifier)
ResultScanner:
void close()
Result next()
用法:
ResultScanner scanner = table.getScanner (Bytes.toBytes(family));
for ( Result rowResult : scanner )
byte[] str = rowResult.getValue ( Bytes.toBytes("family"), Bytes.toBytes("column") )
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
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.TableName;
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.filter.BinaryComparator;
import org.apache.hadoop.hbase.filter.BinaryPrefixComparator;
import org.apache.hadoop.hbase.filter.ByteArrayComparable;
import org.apache.hadoop.hbase.filter.ColumnPrefixFilter;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.FamilyFilter;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.MultipleColumnPrefixFilter;
import org.apache.hadoop.hbase.filter.PrefixFilter;
import org.apache.hadoop.hbase.filter.QualifierFilter;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.filter.SubstringComparator;
import org.apache.hadoop.hbase.master.TableNamespaceManager;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Before;
import org.junit.Test;
public class HbaseDemo {
private Configuration conf = null;
@Before
public void init(){
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "hdp0,hdp1,hdp2");
}
@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, "person_info");
Put p = new Put(Bytes.toBytes("person_rk_bj_zhang_000002"));
p.add("base_info".getBytes(), "name".getBytes(), "zhangwuji".getBytes());
table.put(p);
table.close();
}
@Test
public void testGet() throws Exception{
HTable table = new HTable(conf, "person_info");
Get get = new Get(Bytes.toBytes("person_rk_bj_zhang_000001"));
get.setMaxVersions(5);
Result result = table.get(get);
List<Cell> cells = result.listCells();
// result.getValue(family, qualifier); 可以从result中直接取出一个特定的value
//遍历出result中所有的键值对
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();
}
/**
* 多种过滤条件的使用方法
* @throws Exception
*/
@Test
public void testScan() throws Exception{
HTable table = new HTable(conf, "person_info".getBytes());
Scan scan = new Scan(Bytes.toBytes("person_rk_bj_zhang_000001"), Bytes.toBytes("person_rk_bj_zhang_000002"));
//前缀过滤器----针对行键
Filter filter = new PrefixFilter(Bytes.toBytes("rk"));
//行过滤器
ByteArrayComparable rowComparator = new BinaryComparator(Bytes.toBytes("person_rk_bj_zhang_000001"));
RowFilter rf = new RowFilter(CompareOp.LESS_OR_EQUAL, rowComparator);
/**
* 假设rowkey格式为:创建日期_发布日期_ID_TITLE
* 目标:查找 发布日期 为 2014-12-21 的数据
*/
rf = new RowFilter(CompareOp.EQUAL , new SubstringComparator("_2014-12-21_"));
//单值过滤器 1 完整匹配字节数组
new SingleColumnValueFilter("base_info".getBytes(), "name".getBytes(), CompareOp.EQUAL, "zhangsan".getBytes());
//单值过滤器2 匹配正则表达式
ByteArrayComparable comparator = new RegexStringComparator("zhang.");
new SingleColumnValueFilter("info".getBytes(), "NAME".getBytes(), CompareOp.EQUAL, comparator);
//单值过滤器2 匹配是否包含子串,大小写不敏感
comparator = new SubstringComparator("wu");
new SingleColumnValueFilter("info".getBytes(), "NAME".getBytes(), CompareOp.EQUAL, comparator);
//键值对元数据过滤-----family过滤----字节数组完整匹配
FamilyFilter ff = new FamilyFilter(
CompareOp.EQUAL ,
new BinaryComparator(Bytes.toBytes("base_info")) //表中不存在inf列族,过滤结果为空
);
//键值对元数据过滤-----family过滤----字节数组前缀匹配
ff = new FamilyFilter(
CompareOp.EQUAL ,
new BinaryPrefixComparator(Bytes.toBytes("inf")) //表中存在以inf打头的列族info,过滤结果为该列族所有行
);
//键值对元数据过滤-----qualifier过滤----字节数组完整匹配
filter = new QualifierFilter(
CompareOp.EQUAL ,
new BinaryComparator(Bytes.toBytes("na")) //表中不存在na列,过滤结果为空
);
filter = new QualifierFilter(
CompareOp.EQUAL ,
new BinaryPrefixComparator(Bytes.toBytes("na")) //表中存在以na打头的列name,过滤结果为所有行的该列数据
);
//基于列名(即Qualifier)前缀过滤数据的ColumnPrefixFilter
filter = new ColumnPrefixFilter("na".getBytes());
//基于列名(即Qualifier)多个前缀过滤数据的MultipleColumnPrefixFilter
byte[][] prefixes = new byte[][] {Bytes.toBytes("na"), Bytes.toBytes("me")};
filter = new MultipleColumnPrefixFilter(prefixes);
//为查询设置过滤条件
scan.setFilter(filter);
scan.addFamily(Bytes.toBytes("base_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()));
}
*/
//直接从result中取到某个特定的value
byte[] value = r.getValue(Bytes.toBytes("base_info"), Bytes.toBytes("name"));
System.out.println(new String(value));
}
table.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", "hdp0:2181,hdp1:2181,hdp2:2181");
HBaseAdmin admin = new HBaseAdmin(conf);
TableName tableName = TableName.valueOf("person_info");
HTableDescriptor td = new HTableDescriptor(tableName);
HColumnDescriptor cd = new HColumnDescriptor("base_info");
cd.setMaxVersions(10);
td.addFamily(cd);
admin.createTable(td);
admin.close();
}
}