java远程连接standalone hbase

阅读更多
本文主要描述如何实现在一台Linux机器上搭建一个standalone的hbase,在另外一台机器上通过API访问

服务器端环境搭建:
1.版本信息
  java:1.8.0_172
  hbase: 1.2.6

2.设置JAVA_HOME
  在~/.bash_profile中追加如下内容
 
 
  JAVA_HOME=/usr/java/jdk1.8.0_172-amd64
  export JAVA_HOME
  

  执行如下命令使上述设置立即生效
 
 source ~/.bash_profile

3.设置hostname
 
hostname docker05

4.下载hbase, 点击下载
5.解压下载的文件
$ tar xzvf hbase-1.2.6-bin.tar.gz


6.修改conf下的文件,使能通过远程连接
6.1 进入conf文件夹
cd ./hbase-1.2.6/conf

6.2 修改hbase-site.xml文件,最主要是追加hbase.zookeeper.quorum属性,因为默认是localhost,只能在本机访问,修改成hostname才能远程访问




 
    hbase.rootdir
    file:///root/data/hbase/data
  
  
    hbase.zookeeper.property.dataDir
    /root/data/hbase/zookeeper
  
   
    hbase.client.retries.number
    5
  
  
    hbase.zookeeper.quorum
    
    docker05
  
   
    hbase.zookeeper.property.clientport
    2181
  
  
    hbase.unsafe.stream.capability.enforce
    false
    
      Controls whether HBase will check for stream capabilities (hflush/hsync).

      Disable this if you intend to run on LocalFileSystem, denoted by a rootdir
      with the 'file://' scheme, but be mindful of the NOTE below.

      WARNING: Setting this to false blinds you to potential data loss and
      inconsistent system state in the event of process and/or node failures. If
      HBase is complaining of an inability to use hsync or hflush it's most
      likely not a false positive.
    
  


6.3 修改regionservers文件
删除localhost,修改成hostname



7.启动hbase
 
cd ./hbase-1.2.6/bin

  执行
 
./start-hbase.sh

  正常情况下hbase就会启动好了,可通过jps命令查看是否有 **** HMaster 来判断
 


客户端配置
1.配置hostname
在客户端机器上也需要设置hostname,如192.168.0.172 docker05
2.将服务器上的hbase-site.xml 拷贝到客户端并放到工程根目录下
3.工程目录如下


java远程连接standalone hbase_第1张图片

4.工程的pom.xml文件

	4.0.0
	falcon.chengf
	simple-hbase-test
	0.0.1-SNAPSHOT

	
		
			org.apache.hbase
			hbase-client
			1.3.1
		
	


5.测试类
/**
 * 
 */
package simple.hbase.test;

import java.io.IOException;
import java.io.InputStream;

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.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.io.compress.Compression.Algorithm;

/**
 * @author: 作者: chengaofeng
 * @date: 创建时间:2018-05-31 16:12:35
 * @Description: TODO
 * @version V1.0
 */
public class HbaseTest {
	public static void main(String[] args) throws IOException {
		Configuration config = HBaseConfiguration.create();
		InputStream input = HbaseTest.class.getResourceAsStream("/hbase-site.xml");
		config.addResource(input);
		try (Connection connection = ConnectionFactory.createConnection(config); Admin admin = connection.getAdmin()) {

			HTableDescriptor table = new HTableDescriptor(TableName.valueOf("chengf"));
			table.addFamily(new HColumnDescriptor("columns").setCompressionType(Algorithm.NONE));

			System.out.print("Creating table. ");
			if (admin.tableExists(table.getTableName())) {
				admin.disableTable(table.getTableName());
				admin.deleteTable(table.getTableName());
			}
			admin.createTable(table);
			System.out.println(" create table ok.");
		}
	}
}


6.执行后,控制台信息如下

7.登录hbase服务器,可以看到chengf表创建成功

java远程连接standalone hbase_第2张图片

8.遇到的异常
一开始,只是设置了服务器的hostname,没有修改hbase-site.xml 和regionservers的内容,最终结果只能在服务器端通过shell操作hbase,在client机器上连接一直报org.apache.hadoop.hbase.ipc.FailedServerException: This server is in the failed servers list: localhost/127.0.0.1:60020 一类的错误,后来把hbase-site.xml 和regionservers改完后,重新启动才好了
  • java远程连接standalone hbase_第3张图片
  • 大小: 13.2 KB
  • java远程连接standalone hbase_第4张图片
  • 大小: 41.4 KB
  • java远程连接standalone hbase_第5张图片
  • 大小: 12.5 KB
  • java远程连接standalone hbase_第6张图片
  • 大小: 54.4 KB
  • java远程连接standalone hbase_第7张图片
  • 大小: 33.7 KB
  • 查看图片附件

你可能感兴趣的:(hbase)