1.NAT设置
说明:需要VMWare专业版的虚拟网络编辑器功能实现主机和虚拟机NAT,这样主机和虚拟机形成局域网,主机和虚拟机各自有一个ip
1.1 windows设置
1.2 VMware设置
1.3 ubuntu虚拟机配置
之后输入命令
sudo /etc/network/interfaces
在这个文件中加入下面的配置
auto [网卡名称]
iface [网卡名称] inet static
address [自己设定的IP地址,要属于之前设定的子网]
netmask [子网掩码]
gateway [之前设定的网关]
#比如下面的例子
auto ens33
iface ens33 inet static
address 192.168.86.200
netmask 255.255.255.0
gateway 192.168.86.2
重启网卡: sudo /etc/init.d/networking restart
之后还要设置dns,否则无法可能无法上网,打开 /etc/systemd/resolved.conf,修改为
[Resolve]
DNS=223.5.5.5 1.1.1.1
#FallbackDNS=
#Domains=
LLMNR=no
#MulticastDNS=no
#DNSSEC=no
#Cache=yes
#DNSStubListener=yes
重启虚拟机,虚拟机内尝试ping主机网关,以及检查是否能正常上网,主机内尝试ping虚拟机ip,如果ping不通,可以将防火墙关闭重试,还有可能是windows的出入站规则ipv4回显导致的。
2. java连接hbase
2.1 设置虚拟机hosts和主机hosts
我的环境未设置之前尝试java接hbase,非常非常耗时,但是不报错
sudo vim /etc/hosts
重启ubuntu,之后设置windows的,
右击,打开windows powershell(管理员)
输入:notepad C:/WINDOWS/system32/drivers/etc/hosts
在文件末尾添加:[虚拟机ip] [你给虚拟机的命名]
如(注意空格):192.168.86.200 ubuntu
2.2 修改hbase的配置
修改hbase-site.xml
可在这个地址查看运行的地址和端口
2.3. idea搭建maven项目
pom.xml加入hbase.client依赖,和单元测试依赖
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.3.4</version>
<!--client的版本号要和hbase的版本号对应-->
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
下面这段代码是从别处复制的测试代码:
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.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Test;
import java.io.IOException;
public class JunitTest {
private static final String TABLE_NAME="employee";
public static final String FAMILY_NAME_1 = "profile";
public static final String FAMILY_NAME_2 = "department";
public static final String FAMILY_NAME_3 = "income";
@Test
public void test() {
//创建conf对象 会加载你项目资源文件下的两个XML文件
Configuration conf = HBaseConfiguration.create();
//conf.set("hbase.zookeeper.quorum","ubuntu");
//conf.set("hbase.zookeeper.property.clientPort","2222");
Connection connection = null;
Table table = null;
try {
connection = ConnectionFactory.createConnection(conf);
Admin admin = connection.getAdmin();
if (!admin.tableExists(TableName.valueOf(TABLE_NAME))) {
//create table ,create family
HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(TABLE_NAME));
HColumnDescriptor columnDescriptor_1 = new HColumnDescriptor(Bytes.toBytes(FAMILY_NAME_1));
HColumnDescriptor columnDescriptor_2 = new HColumnDescriptor(Bytes.toBytes(FAMILY_NAME_2));
HColumnDescriptor columnDescriptor_3 = new HColumnDescriptor(Bytes.toBytes(FAMILY_NAME_3));
tableDescriptor.addFamily(columnDescriptor_1);
tableDescriptor.addFamily(columnDescriptor_2);
tableDescriptor.addFamily(columnDescriptor_3);
admin.createTable(tableDescriptor);
} else {
System.err.println("table is exists!!!!!");
}
//put data
table = connection.getTable(TableName.valueOf(TABLE_NAME));
Put put=new Put(Bytes.toBytes("e001")); //rowkey 1
put.addColumn(Bytes.toBytes(FAMILY_NAME_1), Bytes.toBytes("name"), Bytes.toBytes("Alice"));
put.addColumn(Bytes.toBytes(FAMILY_NAME_1), Bytes.toBytes("age"), Bytes.toBytes("31"));
put.addColumn(Bytes.toBytes(FAMILY_NAME_2), Bytes.toBytes("name"), Bytes.toBytes("finance"));
put.addColumn(Bytes.toBytes(FAMILY_NAME_3), Bytes.toBytes("salary"), Bytes.toBytes("7000"));
put.addColumn(Bytes.toBytes(FAMILY_NAME_3), Bytes.toBytes("tax"), Bytes.toBytes("125"));
Put put2=new Put(Bytes.toBytes("e002")); //rowkey 1
put2.addColumn(Bytes.toBytes(FAMILY_NAME_1), Bytes.toBytes("name"), Bytes.toBytes("Tome"));
put2.addColumn(Bytes.toBytes(FAMILY_NAME_1), Bytes.toBytes("age"), Bytes.toBytes("34"));
put2.addColumn(Bytes.toBytes(FAMILY_NAME_2), Bytes.toBytes("name"), Bytes.toBytes("legal"));
put2.addColumn(Bytes.toBytes(FAMILY_NAME_3), Bytes.toBytes("salary"), Bytes.toBytes("6000"));
put2.addColumn(Bytes.toBytes(FAMILY_NAME_3), Bytes.toBytes("tax"), Bytes.toBytes("100"));
Put put3=new Put(Bytes.toBytes("e003")); //rowkey 1
put3.addColumn(Bytes.toBytes(FAMILY_NAME_1), Bytes.toBytes("name"), Bytes.toBytes("Lily"));
put3.addColumn(Bytes.toBytes(FAMILY_NAME_1), Bytes.toBytes("age"), Bytes.toBytes("58"));
put3.addColumn(Bytes.toBytes(FAMILY_NAME_2), Bytes.toBytes("name"), Bytes.toBytes("market"));
put3.addColumn(Bytes.toBytes(FAMILY_NAME_3), Bytes.toBytes("salary"), Bytes.toBytes("12000"));
put3.addColumn(Bytes.toBytes(FAMILY_NAME_3), Bytes.toBytes("tax"), Bytes.toBytes("300"));
table.put(put);
table.put(put2);
table.put(put3);
Get getE001=new Get(Bytes.toBytes("e001"));
//String result=getE001.getFamilyMap().get("salary").toString();
//System.out.println(result);
byte [] ss=table.get(getE001).getValue(Bytes.toBytes(FAMILY_NAME_3),Bytes.toBytes("salary"));
System.out.println("读出rowkey为“e001”的income:salary: "+new String(ss));
Scan scan=new Scan();
scan.setStartRow(Bytes.toBytes("e001"));
scan.setStopRow(Bytes.toBytes("e004")); //到 004 要不然003 的值不会输出
scan.addColumn(Bytes.toBytes(FAMILY_NAME_1), Bytes.toBytes("name"));
scan.setCaching(100);
ResultScanner results=table.getScanner(scan);
for (Result result : results) {
while (result.advance()) {
System.out.println("name :"+new String(result.current().getValueArray()));
}
}
}catch (IOException e){
e.printStackTrace();
}finally {
try {
if (table != null)table.close();
if (connection != null) connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}