eclipse远程连接Hbase数据库

使用了二台机器,一台是win10,一台是ubuntu,eclipse运行于win10,hadoop和hbase支行于ubuntu。

一、环境

1.ubuntu:14.10

2.jdk:1.8.0_131

3.hadoop:2.7.3

4.hbase:1.1.2

二、配置

此处hadoop和hbase都是配置了伪分布式集群,具体如何配置可参考博客:http://dblab.xmu.edu.cn/blog/install-hbase/

三、问题:参照博客配置后,在win10上用eclipse远程连接ubuntu的hbase报错,现在原有配置上做一些修改。

1.修改hosts文件:sudo vim /etc/hosts

原始配置为:其中xj-Lenovo-IdeaPad-V450为ubuntu的机器名

127.0.0.1       localhost
127.0.1.1    xj-Lenovo-IdeaPad-V450

修改为:其中192.168.1.105为ubuntu机器的IP

127.0.0.1       localhost
192.168.1.105   xj-Lenovo-IdeaPad-V450

2.修改./conf/hbase_site.xml文件:vim ./conf/hbase-site.xml,此处配置都使用机器名,不要使用ip和localhost

<configuration>
<property>
                <name>hbase.rootdir</name>
                        <!--对应于hdfs的配置-->
                <value>hdfs://xj-Lenovo-IdeaPad-V450:9000/hbase</value>
        </property>
        <property>
                <name>hbase.cluster.distributed</name>
                <value>true</value>
        </property>
<property>
<name>hbase.master</name>
<value>hdfs://xj-Lenovo-IdeaPad-V450:60000</value>
</property>
<property>
    <name>hbase.zookeeper.quorum</name>
    <value>xj-Lenovo-IdeaPad-V450:2181</value>
</property>
</configuration>

3.修改win10机器的hosts文件 

192.168.1.105  xj-Lenovo-IdeaPad-V450

三、java代码 

package com.xiaoxing.hadoop;

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

/**
 * HBase操作
 * 
 * @author Administrator
 *
 */
public class HBaseDemo {

	/**
	 * hadoop配置
	 */
	private static Configuration configuration;

	/**
	 * hbase客户端连接
	 */
	private static Connection connection;

	private static Admin admin;

	private static final String HBASE_ROOT_DIR = "hdfs://192.168.1.105:9000/hbase";

	/**
	 * HBase初始化,创建连接
	 */
	public static void init() {
		configuration = HBaseConfiguration.create();
		configuration.set("hbase.rootdir", HBASE_ROOT_DIR);
		configuration.set("hbase.zookeeper.quorum", "192.168.1.105");
		configuration.set("hbase.zookeeper.property.clientPort", "2181");
		try {
			// 创建连接
			connection = ConnectionFactory.createConnection(configuration);
			admin = connection.getAdmin();
			System.out.println("创建连接成功");
		} catch (IOException e) {
			System.out.println("创建连接失败");
			e.printStackTrace();
		}
	}

	public static void close() {
		if (null != admin) {
			try {
				admin.close();
				System.out.println("关闭admin");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (null != connection) {
			try {
				connection.close();
				System.out.println("关闭connection");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 创建表
	 * 
	 * @param tableName:表名
	 * @param columnFamily:列簇
	 */
	public static void createTable(String tableName, String[] columnFamily) {
		init();
		TableName table = TableName.valueOf(tableName);
		try {
			
			if (admin.tableExists(table)) {
				System.out.println("表:" + tableName + "存在");
			} else {
				HTableDescriptor hTableDescriptor = new HTableDescriptor(table);
				for (String col : columnFamily) {
					HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(col);
					hTableDescriptor.addFamily(hColumnDescriptor);
				}
				admin.createTable(hTableDescriptor);
				System.out.println("创建表:" + tableName + "成功");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			//close();
		}
	}

	public static void main(String[] args) {
		// init();
		String[] colFamily = { "sname", "course" };
		createTable("Score", colFamily);
		// close();
	}

}

 

转载于:https://my.oschina.net/u/729917/blog/911358

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值