Java连接hbase,保存退出
在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,
修改本地库存放路径
<localRepository>E:\Program Files\apache-maven-3.6.3\repository</localRepository>
在<mirrors>标签中添加下面内容
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
如图:
在idea的设置中选择该文件:
②建立项目
③建立源代码目录和资源文件目录
将从虚拟机中拷贝出来的三个配置文件复制到resource目录下
修改pom.xml
在项目的配置文件中添加项目依赖库
在如下位置添加内容(阴影部分)
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.0.5</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
</dependency>
</dependencies>
编写程序
public class Main {
public static void main(String[] args) {
try {
List<String> tables= getTables();
for(String table:tables){
System.out.println(table);
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
<br>功能描述: 初始化配置
<br>处理逻辑:
<br>修改记录: {修改人 修改原因 修改时间}
* @param
* @throws
* @return org.apache.hadoop.conf.Configuration
* @see #
*/
public static Configuration initConfig(){
Configuration config = new Configuration();
//记住 写服务名 192.168.1.5 hbasehost-server hbasehost
config.set("hbase.zookeeper.quorum","hbasehost");
return config;
}
/**
<br>功能描述: 获取数据库管理员对象
<br>处理逻辑:
* @throws
* @return org.apache.hadoop.hbase.client.Admin
* @see #
*/
public static Admin getAdmin() throws IOException {
Configuration conf = initConfig();
Connection connection = ConnectionFactory.createConnection(conf);
Admin admin = connection.getAdmin();
return admin;
}
/**
<br>功能描述: 检查hbase是否可用
<br>处理逻辑:
<br>修改记录: {修改人 修改原因 修改时间}
* @param
* @throws
* @return void
* @see #
*/
public static void isAvailable() throws IOException {
Configuration conf = initConfig();
HBaseAdmin.available(conf);
}
/**
<br>功能描述: 获取表名集合
<br>处理逻辑:
<br>修改记录: {修改人 修改原因 修改时间}
* @param
* @throws
* @return java.util.List<java.lang.String>
* @see #
*/
public static List<String> getTables() throws IOException {
Admin admin = getAdmin();
//获取hbase中所有的表名
TableName[] tableNames = admin.listTableNames();
List<String> tables = new ArrayList<String>();
for(TableName tableName:tableNames){
tables.add(tableName.getNameAsString());
}
return tables;
}
}
运行结果:显示出里hbase中所有表