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的任意一台zookeeper服务器的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));
}
}
------------------------------------------
其他各种操作于此相似,不再一一列出。