一 搭建前的准备工作
1 Linux下启动的Hadoop和Hbase集群
[root@centos ~]# cd /opt/hadoop-2.7.4/
[root@centos hadoop-2.7.4]# sbin/start-dfs.sh
[root@centos hadoop-2.7.4]# sbin/start-yarn.sh
[root@centos hbase-1.2.6]# bin/hbase-daemon.sh start zookeeper
[root@centos hbase-1.2.6]# bin/hbase-daemon.sh start master
[root@centos hbase-1.2.6]# bin/hbase-daemon.sh start regionserver
[root@centos hbase-1.2.6]# hadoop dfsadmin -safemode leave
2 windows下编辑C:\Windows\System32\drivers\etc下的hosts
#Linux的伪分布式环境的IP和主机名
192.168.0.102 centos
3 在windows下载Hbase包hbase-1.2.6-bin.tar.gz
注意版本和Linux中运行环境的版本保持一致
解压该包,并整理hbase-1.2.6-bin1\hbase-1.2.6\lib下的jar包,针对1.2.6版本,有111个jar包,后面搭建eclipse环境会用到。
二 在windows下完成elipse环境搭建
1 创建Java工程Hbase
2 调整JDK为1.7版本
3 开发Jar包导入
第一步:新建立一个lib目录
第二步:将准备好的111个jar包拷贝到lib目录下,并加入到环境中
4 编写代码
第一步:创建OperateTable类
第二步:编写简单的测试代码
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", "centos");//使用eclipse时必须添加这个,否则无法定位
conf.set("hbase.zookeeper.property.clientPort", "2181");
HBaseAdmin admin = new HBaseAdmin(conf);// 新建一个数据库管理员//新api
HTableDescriptor desc=new HTableDescriptor(TableName.valueOf("blog1"));
desc.addFamily(new HColumnDescriptor("article"));
desc.addFamily(new HColumnDescriptor("author"));
admin.createTable(desc );
admin.close();
}
}
5 完成以上操作后,工程结构图如下:
6 elipse运行程序
log4j:WARN No appenders could be found for logger (org.apache.hadoop.security.Groups).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
三 在linux中进行测试
测试blog1表是否在hbase中建立成功。
[root@centos hbase-1.2.6]# bin/hbase shell
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/opt/hbase-1.2.6/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/hadoop-2.7.4/share/hadoop/common/lib/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
HBase Shell; enter 'help<RETURN>' for list of supported commands.
Type "exit<RETURN>" to leave the HBase Shell
Version 1.2.6, rUnknown, Mon May 29 02:25:32 CDT 2017
hbase(main):001:0> list
TABLE
blog
blog1
testtable
3 row(s) in 0.2480 seconds
=> ["blog", "blog1", "testtable"]