hbase_在程式碼中使用(java專案)
1. 準備工作:
1) 修改hosts
因為zookeeper中存的是主機名,而不是ip地址,因此需要在本機的hosts檔案中新增對映關係,
即,在C:\Windows\System32\drivers\etc\hosts新增:
192.168.75.101 centos2
192.168.75.102 centos3
192.168.75.103 centos4
2) 建立java專案
專案名:hbase_study
將hbase安裝目錄的lib下所有包新增到專案的lib目錄,並新增到build path下。
建立一個測試類:
package com.demo;
import org.junit.Test;
publicclass
@Test
publicvoid testCreateTable() throws Exception{
System.out.println("-----------");
}
}
執行,報錯:
這是eclipse的問題,新增一個jar包就好了:
hamcrest-core-1.1.jar
2. 建立表
package com.demo;
import org.apache.hadoop.conf.Configuration;
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 org.junit.Test;
publicclass TestHbaseDemo {
@Test
publicvoid testCreateTable() throws Exception{
System.out.println("-----------");
//配置ZooKeeper的地址資訊
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum"
//得到HBase的一個客戶端
HBaseAdmin client = newHBaseAdmin(conf);
//建立表的描述符
HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("mytable"));
//建立列族
HColumnDescriptor h1 = new HColumnDescriptor("info");
HColumnDescriptor h2 = new HColumnDescriptor("grade");
//把列族加入表的描述符中
htd.addFamily(h1);
htd.addFamily(h2);
//建立表
client.createTable(htd);
client.close();
}
}
執行結果:
3. 新增資料
@SuppressWarnings("deprecation")
@Test
publicvoid testPut() throws Exception{
//配置ZooKeeper的地址資訊
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum", "192.168.75.101");
//得到表的客戶端
HTable table = newHTable(conf, "mytable");
//構造一個put物件,引數就是rowkey
Put put = new Put(Bytes.toBytes("id001"));
put.add(Bytes.toBytes("info"), //列族
Bytes.toBytes("name"), //列名
Bytes.toBytes("Tom") //值
);
//插入資料
table.put(put);
//關閉
table.close();
}
4. 獲取資料get
@Test
publicvoid testGet() throws Exception{
//配置ZooKeeper的地址資訊
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum", "192.168.75.101");
//得到表的客戶端
HTable table = newHTable(conf, "mytable");
//構造一個Get物件
Get get = new Get(Bytes.toBytes("id001"));
//查詢資料
Result r = table.get(get);
//取出值
String name = Bytes.toString(r.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")));
System.out.println(name);
table.close();
}
5. 獲取資料scan
@Test
publicvoid testScan() throws Exception{
//配置ZooKeeper的地址資訊
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum", "192.168.75.101");
//得到表的客戶端
HTable table = newHTable(conf, "mytable");
//定義一個掃描器
Scan scan = new Scan();
//scan.setFilter(filter) 設定HBase的過濾器
//通過掃描器進行查詢 ResultScanner是一個集合
ResultScanner rs = table.getScanner(scan);
for(Result r : rs){
//取出值
String name = Bytes.toString(r.getValue(Bytes.toBytes("info"), Bytes.toBytes("name")));
System.out.println(name);
}
//關閉客戶端
table.close();
}
6. Hbase連線池
HTablePool和HConnectionManager在新版hbase已經棄用,換成了Connection + ConnectionFactory,
privatestaticfinal String QUORUM = "192.168.110.97";
privatestaticfinal String CLIENTPORT = "2181";
privatestatic Configuration conf = null;
privatestatic Connection connection = null;
public ReadHbase(){
conf = new Configuration();
conf.set("hbase.zookeeper.quorum", QUORUM);
conf.set("hbase.zookeeper.property.clientPort", CLIENTPORT);
}
public Connection getConnection() throws IOException{
if (connection == null){
connection = ConnectionFactory.createConnection(conf);
}
returnconnection;
}
Table table = getConnection().getTable(TableName.valueOf("test_1"));
注:連線池的大小在“hbase.hconnection.threads.max”中定義