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