在hbase中添加测试数据
见《实验3-Hbase安装及配置》的5、Hbase的基本使用
添加一张student表
修改配置文件
登录centos,用hadoop用户登录
①修改hadoop核心配置
sudo vi /usr/local/hadoop/etc/hadoop/core-site.xml
结果如下
IP地址要根据自己的情况改成自己的centos系统IP地址。
保存退出(esc键,输入:wq)
②修改hbase核心配置文件hbase-site.xml
sudo vi /usr/local/hbase/conf/hbase-site.xml
IP地址要根据自己的情况改成自己的centos系统IP地址。
修改centos主机名
hostnamectl set-hostname hbasehost
bash #及时生效
将配置文件拷贝出来
将hadoop的hdfs-site.xml、core-site.xml和hbase的hbase-site.xml拷贝出来,将来放到java项目中。
启动hadoop和hbase
ssh localhost
①启动hadoop
cd /usr/local/hadoop
./sbin/start-dfs.sh
查看是否启动完成
jps
②启动hbase
cd /usr/local/hbase
./bin/start-hbase.sh
查看是否启动成功
进入shell界面
bin/hbase shell
在shell模式下输入help可以查看Hbase相关的命令。
Hbase中用create命令创建表,具体如下
create 'student','Sname','Ssex','Sage','Sdept','course'
此时,即创建了一个“student”表,属性有:Sname,Ssex,Sage,Sdept,course。因为Hbase的表中会有一个系统默认的属性作为主键,故主键无需自行创建。创建完“student”表后,可通过describe命令查看“student”表的基本信息。
设置防火墙
Hbase启动后会占用60010端口,需要防火墙规则放过该端口,也可以暂时关闭防火墙(实际生产中这是很危险的)
查看防火墙状态
systemctl status firewalld.service
Ctrl+c
关闭防火墙
systemctl stop firewalld.service
修改windows的hosts
本次实验是在windows10中安装VMware虚拟机,建立了centos8的虚拟机,而后用windows版的idea建立java项目连接centos中的hbase,因此还需要修改hosts文件
C:WindowsSystem32driversetc
用文本编辑器打开该文件,在文件的最后添加:
192.168.1.5 hbasehost-server hbasehost
IP地址要根据自己的情况改成自己的centos系统IP地址。
建立maven项目
①设置Idea
拷贝settings.xml(该文件是Apache maven的配置文件可以自行下载,也可以拷贝老师的),修改settings.xml,
修改本地库存放路径
在
如图:
在idea的设置中选择该文件:
②建立项目
③建立源代码目录和资源文件目录
将从虚拟机中拷贝出来的三个配置文件复制到resource目录下
修改pom.xml
在项目的配置文件中添加项目依赖库
在如下位置添加内容(阴影部分)
junit
hbase-client
fastjson
编写程序
public class Main {
public static void main(String[] args) {
try {
List
for(String table:tables){
System.out.println(table);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static Configuration initConfig(){
Configuration config = new Configuration();
//记住 写服务名 192.168.1.5 hbasehost-server hbasehost
config.set("hbase.zookeeper.quorum","hbasehost");
return config;
}
public static Admin getAdmin() throws IOException {
Configuration conf = initConfig();
Connection connection = ConnectionFactory.createConnection(conf);
Admin admin = connection.getAdmin();
return admin;
}
public static void isAvailable() throws IOException {
Configuration conf = initConfig();
HbaseAdmin.available(conf);
}
public static List
Admin admin = getAdmin();
//获取hbase中所有的表名
TableName[] tableNames = admin.listTableNames();
List
for(TableName tableName:tableNames){
tables.add(tableName.getNameAsString());
}
return tables;
}
}
运行结果:显示出里hbase中所有表



