hbase的jar包
HBase-0.98.6源码和安装包下载
这里用的是hbase-0.98.6-hadoop2-bin.tar.gz ,直接下载解压后将对应的lib下的包导入项目即可,这里只是测试多一点,实际项目需要自己剔除一些不需要的jar包
hbase的java api
因为在代码里新增了注释,就不一一说明,下面直接上代码
import java.io.IOException;
import java.util.Map.Entry;
import java.util.NavigableMap;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Delete;
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;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class HBaseDemo {
Configuration conf =null;
HBaseAdmin admin =null;
private long timestamp = HConstants.LATEST_TIMESTAMP;
static final long LATEST_TIMESTAMP = Long.MAX_VALUE;
@Before
public void init() throws MasterNotRunningException, ZooKeeperConnectionException, IOException{
//初始化操作打开admin,ddl命令需要一个HBaseAdmin实例,所有的操作都需要HBaseConfiguration实例
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "hddata1,hddata2,hddata3");
admin =new HBaseAdmin(conf);
}
@Test
public void createTable() throws MasterNotRunningException, ZooKeeperConnectionException, IOException{
HTableDescriptor htb = new HTableDescriptor(TableName.valueOf("useraa"));
HColumnDescriptor htc_info =new HColumnDescriptor("info");
HColumnDescriptor htc_data =new HColumnDescriptor("data");
htc_info.setMaxVersions(5);
htb.addFamily(htc_info);
htb.addFamily(htc_data);
admin.createTable(htb);
//admin.close(); //放到了单元测试的@after方法中
}
@Test
public void dropTable()throws Exception{
admin.disableTable("useraa");
admin.deleteTable("useraa");
}
@Test
public void alterTable() throws IOException{
HColumnDescriptor htc = new HColumnDescriptor("testColumn");
//新增一个列簇testColumn,其他属性都为默认值
admin.addColumn("useraa", htc);
}
/**
* 数据的插入,可以用批量插入,批量插入的时候都放进put里面提交即可
* */
@Test
public void putRow() throws IOException{
HTable table =new HTable(conf, TableName.valueOf("people"));
Put put= new Put(Bytes.toBytes("Java00001"));
//同时处理两个cell,put会把其他类型转换成byte[]
put.add(Bytes.toBytes("data"), Bytes.toBytes("size"),this.timestamp, Bytes.toBytes("data2"));
put.add(Bytes.toBytes("data"), Bytes.toBytes("nation"),this.timestamp, Bytes.toBytes("中国"));
table.put(put);
//table.flushCommits(); //一个可选的选项,显式提交将数据刷新到远程服务器端
table.close();
}
@Test
public void deleteCol() throws IOException{ //删除数据,其实是删除数据的标记,只有在刷新之后数据才会消失
HTable ht = new HTable(conf, "people");
Delete del = new Delete(Bytes.toBytes("rk0001"));
del.deleteColumn(Bytes.toBytes("data"), Bytes.toBytes("hello"));// 删除 data:hello列
del.deleteColumn(Bytes.toBytes("info"), Bytes.toBytes("name"));//删除info:name列
ht.delete(del); //提交删除信息
ht.close();
//如果没有成功会有一个delete的List<Delete>,可以通过delete.size查看未成功的,为成功的包括未扫描到的
System.out.println(del.size());
}
/**
* 数据的查询,按照key查询、全表扫描,这里未实现Filter
* */
@SuppressWarnings("deprecation")
@Test
public void getRowKey() throws IOException{
HTable ht=new HTable(conf, "people");
Get get=new Get(Bytes.toBytes("Java00001"));
get.setMaxVersions(5);
Result rs =ht.get(get);
System.out.println("-----------方法一 keyValue获取数据---------------");
for(KeyValue kv : rs.list()){ //keyvalue获取数据
String family = new String(kv.getFamilyArray(),kv.getFamilyOffset(),kv.getFamilyLength());
System.out.println(family+":"+family);
System.out.println(kv.getTimestamp()+" "+new String(kv.getValue(),"utf-8")); //都转换为utf-8,中文会乱码
}
// NavigableMap<列簇,NavigableMap<列名,Navigable<时间戳,列值>>>
NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> map=rs.getMap();
System.out.println("------------方法二 NavigableMap获取数据---------------");
for(byte[] family:map.keySet()){
NavigableMap<byte[], NavigableMap<Long, byte[]>> familyMap = map.get(family);//列簇作为key获取其中的列相关数据
for(byte[] column:familyMap.keySet()){ //根据列名循坏
System.out.println(new String(family)+":"+new String(column));
NavigableMap<Long, byte[]> valuesMap = familyMap.get(column);
for(Entry<Long, byte[]> s:valuesMap.entrySet()){ //获取列对应的不同版本数据,默认最新的一个
System.out.println(s.getKey() +" "+new String(s.getValue(),"utf-8"));
}
}
}
System.out.println(new String(rs.getValue(Bytes.toBytes("info"), Bytes.toBytes("size"))));
ht.close();
}
@SuppressWarnings("deprecation")
@Test
public void scanRow() throws IOException{
HTable ht=new HTable(conf, "people");
Scan scan = new Scan();
scan.setSmall(true);
ResultScanner scanner = ht.getScanner(scan);
System.out.println("-----------已经废弃的方法list和raw-----");
for(Result r : scanner){
System.out.println("------"+new String(r.getRow())+"--------");
for(KeyValue kv : r.list()){
String family = new String(kv.getFamily());
System.out.println(family+":"+new String(kv.getQualifier()));
System.out.println(kv.getTimestamp()+" "+new String(kv.getValue(),"utf-8"));
}
System.out.println("-----------cell[]进行扫描-----");
for(Result rs : scanner){
for(Cell cell:rs.rawCells()){
String row = Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
String family= Bytes.toString(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getFamilyLength());
String column= Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength());
String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
System.out.println(row+"\n---"+family+":"+column+"\n "+value);
}
}
}
}
@After
public void end() throws IOException{
admin.close();
}
}
进阶
以上只是一些简单的增删改查,那么更深入的可以对hbase的过滤器的使用、rowkey的设计、hbase结合mapreduce的实现,比如对hdfs上的文件进行更新、读取等操作