欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

eclipse远程连接Hbase数据库

程序员文章站 2022-05-30 15:13:02
...

使用了二台机器,一台是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