eclipse遠程鏈接Hbase數據庫

使用了二臺機器,一臺是win10,一臺是ubuntu,eclipse運行於win10,hadoop和hbase支行於ubuntu。java

1、環境apache

1.ubuntu:14.10ubuntu

2.jdk:1.8.0_131vim

3.hadoop:2.7.3bash

4.hbase:1.1.2eclipse

2、配置分佈式

此處hadoop和hbase都是配置了僞分佈式集羣,具體如何配置可參考博客:http://dblab.xmu.edu.cn/blog/install-hbase/oop

3、問題:參照博客配置後,在win10上用eclipse遠程鏈接ubuntu的hbase報錯,如今原有配置上作一些修改。code

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

原始配置爲:其中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

3、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();
	}

}
相關文章
相關標籤/搜索