1. 环境
- mac或者linux 1台; 客户端 电脑(windows、mac、linux) 1台
- jdk 1.8.x
- hbase 2.2.2 下载: https://hbase.apache.org/downloads.html
- hdaoop 3.2.1 下载: https://hadoop.apache.org/releases.html
- hadoop-common-bin 工具 下载(windows): https://download.csdn.net/download/shuaidan19920412/12080258
2. 安装及测试
- 将相关文件解压至相关目录,以 /opt/apache/为例
- 配置 hbase-2.2.2/conf/hbase-env.sh,
export JAVA_HOME=/usr/java/jdk1.8.0_161 (jdk路径)
- 配置 /hbase-2.2.2/conf/hbase-site.xml,
<configuration>
<property>
<name>hbase.zookeeper.quorum</name>
<!-- 增加统计支持 更改为服务器计算机名字 -->
<value>ubuntu</value>
</property>
<property>
<name>hbase.rootdir</name>
<!--自定义路径 -->
<value>file:///data/apache/hbase/root</value>
</property>
<property>
<name>hbase.tmp.dir</name>
<!--自定义路径 -->
<value>/data/apache/hbase/tmp</value>
</property>
<property>
<name>hbase.zookeeper.property.dataDir</name>
<!--自定义路径 -->
<value>/data/apache/hbase/zoo</value>
</property>
<property>
<name>hbase.unsafe.stream.capability.enforce</name>
<value>false</value>
</property>
</configuration>
- 配置 host ,此步骤影响远程访问 。linux (vi /etc/hosts)
添加当前计算机ip 名字,比如 192.168.3.1 ubuntu
- 启动及测试
(1) 命令行运行 hbase-2.2.2/bin/start-hbase.sh
如果出现 running master, logging to /opt/apache/hbase-2.2.2/bin/../logs/hbase-devin-master-ubxxxxxxxxxx
(2) 且使用 jps 命令查看 有 HMaster 进程 ,则表示成功,入下图
(3) 网页 http://192.168.3.31:16010/master-status (更改为自己ip或者主机地址)
- 服务器本地 shell 测试
(1) 打开工具 hbase-2.2.2/bin/hbase shell ,如下图
(2) 测试是否通 , 输入命令 list
(3) 也可以测试建表等命令,比如
create 'spark_test','name','sex'
3. 远程连接
- 如果windows , 配置环境遍历 HADOOP_HOME, 指向 之前准备的hadoop_commen 地址
- 配置本地host , 同服务器配置,将服务器ip地址配置为相应的名字。比如
- 关闭局域网等
- 测试是否可通: telnet 192.xxx 2181
- 编码,pom文件如下
-
<properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <encoding>UTF-8</encoding> <scala.version>2.11.12</scala.version> <scala.compat.version>2.11</scala.compat.version> <hadoop.version>3.2.1</hadoop.version> <spark.version>2.4.3</spark.version> <hbase.version>2.2.2</hbase.version> </properties> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>${hadoop.version}</version> <exclusions> <exclusion> <artifactId>commons-httpclient</artifactId> <groupId>commons-httpclient</groupId> </exclusion> <exclusion> <artifactId>httpcore</artifactId> <groupId>org.apache.httpcomponents</groupId> </exclusion> <exclusion> <artifactId>hadoop-common</artifactId> <groupId>org.apache.hadoop</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>${hbase.version}</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-common</artifactId> <version>${hbase.version}</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-server</artifactId> <version>${hbase.version}</version> </dependency>
- 部分测试代码
val tableName = "spark_test"
val columnFamilys = List("a", "b", "c")
val conf = HBaseConfiguration.create()
conf.set("hbase.zookeeper.quorum","ubuntu")
conf.set("hbase.zookeeper.property.clientPort", "2181")
val hbaseconn = ConnectionFactory.createConnection(conf)
val admin:Admin = hbaseconn.getAdmin()
val myTableName :TableName = TableName.valueOf(tableName)
if (admin.tableExists(myTableName)) {
println(tableName +" Table exists!")
//val tableDesc: HTableDescriptor = new HTableDescriptor(TableName.valueOf(tablename))
//tableDesc.addCoprocessor("org.apache.hadoop.hbase.coprocessor.AggregateImplementation")
}else {
// 表描述器构造器
println(tableName +" Table not exists!")
val tdb = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName))
if(null != columnFamilys)
for (columnFamily <- columnFamilys) {
//列族描述起构造器//列族描述起构造器
val cdb = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(columnFamily))
//获得列描述起
val cfd: ColumnFamilyDescriptor = cdb.build
//添加列族
tdb.setColumnFamily(cfd)
}
// 获得表描述器
val td = tdb.build
admin.createTable(td)
println("create successful!! ")
}
admin.close
可能碰到的问题
* 如果读取 maven xml配置异常,可能是因为 setting.xml 中包含了中文(含注释)或者非UTF8编码
* 如果报错 Could not initialize class org.fusesource.jansi.internal.Kernel32; 可能是因为windows下缺jansi-1.4.jar ;解决方案:下载jansi-1.4.jar包放到hbase-2.2.1\lib下,重新启动即可