1. 试用thrift python/java以及hbase client api。结论例如以下:
1.1 thrift的安装和公布繁琐。可能会遇到未知的错误,且hbase.thrift的版本号在变化中。
长处代码简单,须要打包的内容少。
1.2 hbase client api,须要的jar非常多,公布版的容量也非常大。打包后近百兆。
长处是。明白。无歧义。
2. 推荐用hbase client api的方式搞定。
3. 下面均为技术细节。
4. 有一台机器/一个集群,在执行hadoop,也执行了基于这个hadoop集群的hbase集群,同一时候,也执行了一个zookeeper集群,我们统称它是A。
5. 有一台集群负责开发,我们在上面写代码。编译代码,执行代码。我们称它是B。
6. 在B上,要改动/etc/hosts,把A的随意一台zookeeperserver的hostname和相应的ip地址放进去。由于hbase client须要连接到zookeeper,以便获得hbase的hmast信息---hbase集群有多个hmast。一个是主hmast。其它是备用hmaster,假设主hmaster挂了,备用的会顶上,避免单点故障问题。
7. 在B上开发。在elipse建立一个java项目。加入一个lib文件夹,把A上的hadoop, hbase, zookeeper的全部jar包,注意。是全部jar包,各级子文件夹的也算在内,都拷贝到lib文件夹。大概有130个左右,90M。然后,再把它们加入到buildpath。这么做的优点是,不用一点点找到底哪个类在哪个包。生命短暂,不要把时间浪费在这里。浪费点磁盘空间没关系。
假设hadoop,hbase, zookeeper都安装在一个文件夹下,能够用一个shell语句搞定:
for i in `find . -name "*.jar"`; do cp $i ~/alljars; done;
然后再把alljars下的jar包都拷贝到B的lib文件夹。
8. 写一个最简单的hbase demo。在hbase里检查一个表是否存在,假设不存在,就创建它。
-----------------------------------------
package hbasedemo;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.TableName;
public class Main {
public static void main(String[] args) throws IOException{
Configuration hbase_conf = new Configuration();
hbase_conf.set("hbase.zookeeper.quorum", "brianxxxooo"); //brianxxxooo是A里的zookeeper机器的hostname
hbase_conf.set("hbase.zookeeper.property.clientPort","2181");
Configuration conf = HBaseConfiguration.create(hbase_conf);
String tablename="scores";
String[] familys = {"grade", "course"};
HBaseAdmin admin = new HBaseAdmin(conf);
if (admin.tableExists(tablename)){
System.out.println("table exist, return!");
return;
}
HTableDescriptor td = new HTableDescriptor(TableName.valueOf(tablename));
for(int i = 0; i < familys.length; i++){
td.addFamily(new HColumnDescriptor(familys[i]));
}
admin.createTable(td);
System.out.println("create table "+tablename+" ok.");
}
}
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.TableName;
public class Main {
public static void main(String[] args) throws IOException{
Configuration hbase_conf = new Configuration();
hbase_conf.set("hbase.zookeeper.quorum", "brianxxxooo"); //brianxxxooo是A里的zookeeper机器的hostname
hbase_conf.set("hbase.zookeeper.property.clientPort","2181");
Configuration conf = HBaseConfiguration.create(hbase_conf);
String tablename="scores";
String[] familys = {"grade", "course"};
HBaseAdmin admin = new HBaseAdmin(conf);
if (admin.tableExists(tablename)){
System.out.println("table exist, return!");
return;
}
HTableDescriptor td = new HTableDescriptor(TableName.valueOf(tablename));
for(int i = 0; i < familys.length; i++){
td.addFamily(new HColumnDescriptor(familys[i]));
}
admin.createTable(td);
System.out.println("create table "+tablename+" ok.");
}
}
-----------------------------------------
9. 注意事项,hbase client的版本号变化甚多,详细api调用要依据版本号来,有时候须要參考多个版本号来。比方,0.96.x的HTableDescripter更接近http://hbase.apache.org/apidocs/index.html
, 而不是0.94的api。
但HBaseAdmin在0.94的api是有的,在2.0.0里没有。很混乱。
预计这个局面还要持续一段时间。
10. 更具体的样例
------------------------------------------
package hbasedemo;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
public class Main {
public static void main(String[] args) throws IOException{
Configuration hbase_conf = new Configuration();
hbase_conf.set("hbase.zookeeper.quorum", "brianvxxxxooooo");
hbase_conf.set("hbase.zookeeper.property.clientPort","2181");
Configuration conf = HBaseConfiguration.create(hbase_conf);
String tablename="scores";
String[] familys = {"grade", "course"};
HBaseAdmin admin = new HBaseAdmin(conf);
if (admin.tableExists(tablename)){
System.out.println("table exist!");
}else{
HTableDescriptor td = new HTableDescriptor(TableName.valueOf(tablename));
for(int i = 0; i < familys.length; i++){
td.addFamily(new HColumnDescriptor(familys[i]));
}
admin.createTable(td);
System.out.println("create table "+tablename+" ok.");
}
HTable table = new HTable(conf, "scores");
Put put = new Put(Bytes.toBytes("row1"));
//create
put.add(Bytes.toBytes("grade"), Bytes.toBytes("g1"), Bytes.toBytes(781));
put.add(Bytes.toBytes("grade"), Bytes.toBytes("g2"), Bytes.toBytes("this is test"));
table.put(put);
//read
Get get = new Get(Bytes.toBytes("row1"));
get.addColumn(Bytes.toBytes("grade"), Bytes.toBytes("g1"));
Result result = table.get(get);
byte[] val = result.getValue(Bytes.toBytes("grade"), Bytes.toBytes("g1"));
System.out.println(Bytes.toInt(val));
}
}
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
public class Main {
public static void main(String[] args) throws IOException{
Configuration hbase_conf = new Configuration();
hbase_conf.set("hbase.zookeeper.quorum", "brianvxxxxooooo");
hbase_conf.set("hbase.zookeeper.property.clientPort","2181");
Configuration conf = HBaseConfiguration.create(hbase_conf);
String tablename="scores";
String[] familys = {"grade", "course"};
HBaseAdmin admin = new HBaseAdmin(conf);
if (admin.tableExists(tablename)){
System.out.println("table exist!");
}else{
HTableDescriptor td = new HTableDescriptor(TableName.valueOf(tablename));
for(int i = 0; i < familys.length; i++){
td.addFamily(new HColumnDescriptor(familys[i]));
}
admin.createTable(td);
System.out.println("create table "+tablename+" ok.");
}
HTable table = new HTable(conf, "scores");
Put put = new Put(Bytes.toBytes("row1"));
//create
put.add(Bytes.toBytes("grade"), Bytes.toBytes("g1"), Bytes.toBytes(781));
put.add(Bytes.toBytes("grade"), Bytes.toBytes("g2"), Bytes.toBytes("this is test"));
table.put(put);
//read
Get get = new Get(Bytes.toBytes("row1"));
get.addColumn(Bytes.toBytes("grade"), Bytes.toBytes("g1"));
Result result = table.get(get);
byte[] val = result.getValue(Bytes.toBytes("grade"), Bytes.toBytes("g1"));
System.out.println(Bytes.toInt(val));
}
}
------------------------------------------
其它各种操作于此相似,不再一一列出。