首先,说明一下开发环境,eclipse是部署在WIN7系统上,Hbase集群是部署在CentOS上面。
下面是网上转载的开发步骤:
1.添加包
(1)我们解压包
解压之后,得到如下包
(2)添加包
添加包的操作
如上图5个步骤:
单 击hbase-》属性弹出(2所示)Properties for hbase属性对话框.
然后我们通过下标5,单击Add External JARs。
找到hbase_home/lib,我这里是D:\hadoop2\hbase-0.98.3-hadoop2\lib
添加完毕,这样开发环境就搭建完毕。
package
www.aboutyun.com.hbase;
import java.io.IOException;
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.client.HBaseAdmin;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.util.Bytes;
public class OperateTable {
public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "master");//使用eclipse时必须添加这个,否则无法定位
conf.set("hbase.zookeeper.property.clientPort", "2181");
HBaseAdmin admin = new HBaseAdmin(conf);// 新建一个数据库管理员//新api
HTableDescriptor desc=new HTableDescriptor(TableName.valueOf("blog"));
//HTableDescriptor desc = new HTableDescriptor("blog");
desc.addFamily(new HColumnDescriptor("article"));
desc.addFamily(new HColumnDescriptor("author"));
admin.createTable(desc );
admin.close();
//admin.disableTable("blog");
//admin.deleteTable("blog");
//assertThat(admin.tableExists("blog"),is(false));
}
}
注释:
conf.set("hbase.zookeeper.quorum", "
master
");//使用eclipse时必须添加这个,否则无法定位
这里因为使用的是win7,所以master需要配置hosts。如下图所示
hosts的路径为C:\Windows\System32\drivers\etc
得到结果:我们看到blog就创建成功了。
这里说一下:
HTableDescriptor的初始化发生变化:
新API
HTableDescriptor desc=new HTableDescriptor(TableName.valueOf("blog"));
旧API
HTableDescriptor desc = new HTableDescriptor("blog");
值得注意的是,如果你是用的Hbase集群,在代码 conf.set("hbase.zookeeper.quorum", "master");
你可以配置自己的hosts文件,也就是你搭建的Hbase集群。这样就不会报错!
conf.set("hbase.zookeeper.quorum","dam01:2181,dam02:2181,dam03:2181");
OK!