今天就簡(jiǎn)單的介紹一下Hbase的java的api,有創(chuàng)建表,插入數(shù)據(jù),查詢數(shù)據(jù)。
先進(jìn)入shell控制臺(tái),看看現(xiàn)有幾個(gè)表:

image.png
一個(gè)rr表,一個(gè)user表
創(chuàng)建表:
前提要將對(duì)應(yīng)hbase的lib包下的jar,拷貝到項(xiàng)目中
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.TableName;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import java.io.IOException;
/**
* 測(cè)試Hbase
*
* @author songlj
* @date 2018/5/24 22:50
*/
public class HbaseDao {
public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "my06:2181,my07:2181,my08:2181");
//ddl的操作對(duì)象
HBaseAdmin admin = new HBaseAdmin(conf);
//表名
TableName name = TableName.valueOf("user1");
HTableDescriptor desc = new HTableDescriptor(name);
//列族
HColumnDescriptor base_info = new HColumnDescriptor("base_info");
HColumnDescriptor extra_info = new HColumnDescriptor("extra_info");
//最大保存的歷史版本個(gè)數(shù)
base_info.setMaxVersions(5);
desc.addFamily(base_info);
desc.addFamily(extra_info);
//創(chuàng)建表
admin.createTable(desc);
admin.close();
}
}
運(yùn)行:

image.png
結(jié)果:

image.png
添加數(shù)據(jù)
@Test
public void insertTest() throws Exception{
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "my06:2181,my07:2181,my08:2181");
//獲取dml客戶端對(duì)象
HTable user1 = new HTable(conf, "user1");
//給列族中放列以及列值 k_v hbase存儲(chǔ)的是bytes,而字節(jié)的數(shù)組
Put name = new Put(Bytes.toBytes("rk0001"));
name.add(Bytes.toBytes("base_info"), Bytes.toBytes("name"), Bytes.toBytes("songlj"));
Put age = new Put(Bytes.toBytes("rk0001"));
age.add(Bytes.toBytes("base_info"), Bytes.toBytes("age"), Bytes.toBytes(18));
ArrayList<Put> puts = new ArrayList<>();
puts.add(name);
puts.add(age);
user1.put(puts);
user1.close();
}
運(yùn)行

image.png
結(jié)果

image.png
查詢數(shù)據(jù)
@Test
public void testGet() throws Exception{
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "my06:2181,my07:2181,my08:2181");
HTable table = new HTable(conf, "user1");
Get get = new Get(Bytes.toBytes("rk0001"));
get.setMaxVersions(5);
Result result = table.get(get);
List<Cell> cells = result.listCells();
// result.getValue(family, qualifier); 可以從result中直接取出一個(gè)特定的value
//遍歷出result中所有的鍵值對(duì)
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();
}
運(yùn)行結(jié)果:

image.png
這里就介紹到這里,還有scan,各種過(guò)濾器,就不在這里贅述了,實(shí)際的應(yīng)用中用到了可以去查看一下,很簡(jiǎn)單,懂得了hbase java api的設(shè)計(jì)技巧,能夠做到舉一反三。
望指正,不吝賜教