1、在你的window系统下 找到c:/window/system32/drivers/etc/hosts 在末尾添加 虚拟机IP 虚拟机的hostname(如 192.168.56.100 gg)

2、在java代码中添加:
Configuration config = HBaseConfiguration.create();
config.set(“hbase.rootdir”,“hdfs://192.168.56.100:9000/hbase”);//和你的hadoop中core-site.xml中信息相同
config.set(“hbase.zookeeper.quorum”,“192.168.56.100”);//zookeeper地址
config.set(“hbase.zookeeper.property.clientPort”,“2181”);//zookeeper 端口

3、修改远程hbase中的hbase-site.xml文件末尾添加:

	<property>
		<name>hbase.zookeeper.quorum</name>
		<value>192.168.56.100</value>
	</property>

4、重启远程hbase
5、代码测试:

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.zookeeper.server.ConnectionBean;

import java.io.IOException;

public class MyHbase {
    public static void main(String[] args) throws IOException {
        Configuration config = HBaseConfiguration.create();
        config.set("hbase.rootdir","hdfs://192.168.56.100:9000/hbase");
        config.set("hbase.zookeeper.quorum","192.168.56.100");
        config.set("hbase.zookeeper.property.clientPort","2181");
        Connection connection = ConnectionFactory.createConnection(config);
        Table table = connection.getTable(TableName.valueOf("mtest:demo"));
        Put put = new Put("1".getBytes());
        put.addColumn("base".getBytes(),"name".getBytes(),"hehe".getBytes());
        put.addColumn("base".getBytes(),"sex".getBytes(),"female".getBytes());
        table.put(put);
    }
}

6、远程hbase查看数据

在这里插入图片描述

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐