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;
public class TestHbaseDemo {
@Test
public void 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;
public class TestHbaseDemo {
@Test
public void testCreateTable() throws Exception{
System.out.println("-----------");
//配置ZooKeeper的地址信息
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum", "192.168.75.101");
//得到HBase的一个客户端
HBaseAdmin client = new HBaseAdmin(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
public void testPut() throws Exception{
//配置ZooKeeper的地址信息
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum", "192.168.75.101");
//得到表的客户端
HTable table = new HTable(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
public void testGet() throws Exception{
//配置ZooKeeper的地址信息
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum", "192.168.75.101");
//得到表的客户端
HTable table = new HTable(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
public void testScan() throws Exception{
//配置ZooKeeper的地址信息
Configuration conf = new Configuration();
conf.set("hbase.zookeeper.quorum", "192.168.75.101");
//得到表的客户端
HTable table = new HTable(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,
private static final String QUORUM = "192.168.110.97";
private static final String CLIENTPORT = "2181";
private static Configuration conf = null;
private static 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);
}
return connection;
}
Table table = getConnection().getTable(TableName.valueOf("test_1"));
注:连接池的大小在“hbase.hconnection.threads.max”中定义