环境
- Hadoop集群
- Zookeeper单机
- Hbase单机
开发环境可以关注 “后端码匠” 回复 电脑环境 获取 ,也可自行下载,也很快的.
环境变量
可自行度娘
#JAVA_HOME
export JAVA_HOME=/usr/java/jdk1.8.0_221
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
#HADOOP_HOME
export HADOOP_HOME=/opt/module/hadoop-3.1.1
export PATH=$PATH:$HADOOP_HOME/bin
export PATH=$PATH:$HADOOP_HOME/sbin
#ZOOKEEPER_HOME
export ZOOKEEPER_HOME=/opt/module/zookeeper-3.5.6
export PATH=.:$HADOOP_HOME/bin:$ZOOKEEPER_HOME/bin:$JAVA_HOME/bin:$PATH
配置
Hadoop配置在另外一条推文已经写过了
Zookeeper配置
没有使用Hbase自带的zk , 本想用docker中的zk来的, 但是有点小问题(解决了会用的)
本次使用的是官网下载的 3.5.6版本 http://archive.apache.org/dist/zookeeper/
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/opt/module/zk/data # 自己创建的
dataLogDir=/opt/module/zk/dataLog # 自己创建的
# the port at which the clients will connect
clientPort=2181
本次所有的开发环境 运行的用户为 codingce, 大家可自己创建类似账户 并分配权限, 此过程在 我写的 Hadoop集群搭建里面有如何创建用户.
切换完用户后
启动 ZooKeeper
进入到bin目录下
$ ./zkServer.sh start
查看 zk 状态
./zkServer.sh status
停止 zk
./zkServer.sh stop
重启 zk
./zkServer.sh restart
测试连接 zk
./zkCli.sh -server 127.0.0.1:2181
期间遇到的问题
./zkCli.sh -server 127.0.0.1:2181
使用 ls /
来扫描zookeeper中的数据
使用 rmr /hbase
删除zookeeper中的hbase数据
重新启动hbase即可
Hbase配置
config 目录下
- hbase-env.sh
export JAVA_HOME=/usr/java/jdk1.8.0_221/
export HBASE_MANAGES_ZK=false
- hbase-site.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<!--
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-->
<configuration>
<property>
<name>hbase.rootdir</name>
<value>hdfs://hadoop102:8020/hbase</value>
</property>
<property>
<name>hbase.cluster.distributed</name>
<value>true</value>
</property>
<property>
<name>hbase.unsafe.stream.capability.enforce</name>
<value>false</value>
</property>
<!-- 0.98 后的新变动,之前版本没有.port,默认端口为 60000 -->
<property>
<name>hbase.master.port</name>
<value>16000</value>
</property>
<property>
<name>hbase.zookeeper.quorum</name>
<value>127.0.0.1:2181</value>
</property>
<property>
<name>hbase.zookeeper.property.dataDir</name>
<value>/opt/module/zk/data</value>
</property>
</configuration>
软连接 hadoop 配置文件到 hbase
[codingce@linuxmxz conf]$ ln -s /opt/module/hadoop-3.1.1/etc/hadoop/core-site.xml /opt/module/hbase-2.2.7/conf/core-site.xml
[codingce@linuxmxz conf]$ ln -s /opt/module/hadoop-3.1.1/etc/hadoop/hdfs-site.xml /opt/module/hbase-2.2.7/conf/hdfs-site.xml
启动
bin/start-hbase.sh
停止
bin/stop-hbase.sh
环境启动无误后 先试下 Shell操作然后进行代码操作
Java 操作
maven项目
pom
<dependencies>
<!-- hbase -->
<!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-client -->
<!-- <dependency>-->
<!-- <groupId>org.apache.hbase</groupId>-->
<!-- <artifactId>hbase-client</artifactId>-->
<!-- <version>2.2.7</version>-->
<!-- </dependency>-->
<!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-client -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.4.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hbase/hbase-server -->
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.4.2</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.apache.hbase</groupId>-->
<!-- <artifactId>hbase-it</artifactId>-->
<!-- <version>2.2.7</version>-->
<!-- <scope>test</scope>-->
<!-- </dependency>-->
<!-- hbase-server -->
<!-- <dependency>-->
<!-- <groupId>org.apache.hbase</groupId>-->
<!-- <artifactId>hbase-server</artifactId>-->
<!-- <version>2.2.7</version>-->
<!-- </dependency>-->
<!-- hbase-protocol -->
<!-- <dependency>-->
<!-- <groupId>org.apache.hbase</groupId>-->
<!-- <artifactId>hbase-protocol</artifactId>-->
<!-- <version>2.2.7</version>-->
<!-- </dependency>-->
<!-- hadoop -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.30</version>
</dependency>
</dependencies>
业务代码
测试代码
package cn.com.codingce.hbase.mydemo;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.NamespaceNotFoundException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
/**
* 1.4.2 版本写法
*
* @author williamma
*/
public class TestApi_1 {
public static Configuration conf;
static {
//使用 HBaseConfiguration 的单例方法实例化
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "139.9.34.48");
conf.set("hbase.zookeeper.property.clientPort", "2181");
}
public static void main(String[] args) throws IOException {
// 获取连接
//setClassLoader 类加载器
// public static Configuration addHbaseResources(Configuration conf) {
// conf.addResource("hbase-default.xml");
// conf.addResource("hbase-site.xml");
// checkDefaultsVersion(conf);
// return conf;
// }
Connection connection = ConnectionFactory.createConnection(conf);
System.out.println("============ 获取连接对象 ================");
System.out.println(connection);
// 获取操作对象 admin
//new HBaseAdmin(connection); 不推荐使用
Admin admin = connection.getAdmin();
//操作数据库 判断数据库中有没有某张表
//判断命名空间
try {
admin.getNamespaceDescriptor("codingce");
} catch (NamespaceNotFoundException e) {
//创建命名空间
NamespaceDescriptor codingce = NamespaceDescriptor.create("codingce").build();
admin.createNamespace(codingce);
}
//判断hbase中是否存在某张表
TableName tableName = TableName.valueOf("codingce:student");
boolean b = admin.tableExists(tableName);
System.out.println("判断hbase中是否存在某张表: codingce:student" + b);
if (b) {
//查询数据
//获取指定表对象
Table table = connection.getTable(tableName);
String rowkey = "1003";
//注意字符编码问题
//Get get = new Get(rowkey.getBytes());
Get get = new Get(Bytes.toBytes(rowkey));
//查询结果
Result result = table.get(get);
boolean empty = result.isEmpty();
System.out.println("是否存在:" + empty);
if (empty) {
//新增数据
Put put = new Put(Bytes.toBytes(rowkey));
String family = "info";
String column = "name";
String val = "后端码匠";
put.addColumn(Bytes.toBytes(family), Bytes.toBytes(column), Bytes.toBytes(val));
table.put(put);
System.out.println("增加数据。。。");
} else {
//展示数据
for (Cell cell : result.rawCells()) {
// cell.
System.out.println(" value " + Bytes.toString(CellUtil.cloneValue(cell)));
System.out.println(" rowkey " + Bytes.toString(CellUtil.cloneRow(cell)));
System.out.println(" family " + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println(" column " + Bytes.toString(CellUtil.cloneQualifier(cell)));
}
}
} else {
//创建表
//创建表的描述对象
HTableDescriptor td = new HTableDescriptor(tableName);
//增加列族
HColumnDescriptor cd = new HColumnDescriptor("info");
td.addFamily(cd);
admin.createTable(td);
System.out.println("创建完成");
}
}
/**
* 判断表是否存在
*
* @param tableName
* @return
* @throws MasterNotRunningException
* @throws ZooKeeperConnectionException
* @throws IOException
*/
public static boolean isTableExist(String tableName, Connection connection) throws MasterNotRunningException,
ZooKeeperConnectionException, IOException {
//在 HBase 中管理、访问表需要先创建 HBaseAdmin 对象
//Connection connection = ConnectionFactory.createConnection(conf); //HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
Admin admin = connection.getAdmin();
//判断命名空间
try {
NamespaceDescriptor namespace = admin.getNamespaceDescriptor("xxx");
if (namespace == null) {
//创建表空间
return true;
}
} catch (NamespaceNotFoundException e) {
System.out.println(e.getMessage());
return false;
}
return admin.tableExists(TableName.valueOf(tableName));
}
public static void createTable(String tableName, Connection connection, String... columnFamily) throws
MasterNotRunningException, ZooKeeperConnectionException, IOException {
Admin admin = connection.getAdmin();
//判断表是否存在
if (isTableExist(tableName, connection)) {
System.out.println("表" + tableName + "已存在");
//System.exit(0);
} else {
//创建表属性对象,表名需要转字节
HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf(tableName));
//创建多个列族
for (String cf : columnFamily) {
descriptor.addFamily(new HColumnDescriptor(cf));
}
//根据对表的配置,创建表 admin.createTable(descriptor); System.out.println("表" + tableName + "创建成功!");
}
}
}
业务封装
package cn.com.codingce.hbase.mydemo;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.NamespaceNotFoundException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* 1.4.2 版本写法
*
* @author williamma
*/
public class TestApi_2 {
public static void main(String[] args) throws IOException {
// dropTable("codingce:student");
// createTable("user", "info");
getAllRows("codingce:student");
}
//获取 Configuration 对象
public static Configuration conf;
static {
//使用 HBaseConfiguration 的单例方法实例化
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "139.9.34.48");
conf.set("hbase.zookeeper.property.clientPort", "2181");
}
/**
* 获取连接
*
* @return
* @throws IOException
*/
public static Connection getConnection() throws IOException {
Connection connection = ConnectionFactory.createConnection(conf);
System.out.println("创建连接。。。" + connection);
return connection;
}
/**
* 判断表是否存在
*
* @param tableName
* @return
* @throws MasterNotRunningException
* @throws ZooKeeperConnectionException
* @throws IOException
*/
public static boolean isTableExist(String tableName) throws MasterNotRunningException,
ZooKeeperConnectionException, IOException {
//在 HBase 中管理、访问表需要先创建 HBaseAdmin 对象
//Connection connection = ConnectionFactory.createConnection(conf);
// HBaseAdmin admin = (HBaseAdmin) connection.getAdmin();
Connection connection = getConnection();
Admin admin = connection.getAdmin();
boolean result = admin.tableExists(TableName.valueOf(tableName));
System.out.println("表是否存在:" + result);
return result;
}
/**
* 创建表
*
* @param tableName
* @param columnFamily
* @throws MasterNotRunningException
* @throws ZooKeeperConnectionException
* @throws IOException
*/
public static void createTable(String tableName, String... columnFamily) throws
MasterNotRunningException, ZooKeeperConnectionException, IOException {
Connection connection = getConnection();
Admin admin = connection.getAdmin();
//操作数据库 判断数据库中有没有某张表
//判断命名空间
try {
admin.getNamespaceDescriptor("codingce");
} catch (NamespaceNotFoundException e) {
//创建命名空间
NamespaceDescriptor codingce = NamespaceDescriptor.create("codingce").build();
admin.createNamespace(codingce);
}
//判断表是否存在
if (isTableExist("codingce:" + tableName)) {
System.out.println("表 " + "codingce:" + tableName + "已存在");
//System.exit(0);
} else {
//创建表
//创建表属性对象,表名需要转字节
HTableDescriptor descriptor = new HTableDescriptor(TableName.valueOf("codingce:" + tableName));
//创建多个列族
for (String cf : columnFamily) {
descriptor.addFamily(new HColumnDescriptor(cf));
}
//根据对表的配置,创建表
admin.createTable(descriptor);
System.out.println("表 " + "codingce:" + tableName + "创建成功!");
}
}
/**
* 删除表
* <p>
* codingce:student
* 命名空间 : 表名
*
* @param tableName
* @throws MasterNotRunningException
* @throws ZooKeeperConnectionException
* @throws IOException
*/
public static void dropTable(String tableName) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
Connection connection = getConnection();
Admin admin = connection.getAdmin();
if (isTableExist(tableName)) {
admin.disableTable(TableName.valueOf(tableName));
admin.deleteTable(TableName.valueOf(tableName));
System.out.println("表" + tableName + "删除成功!");
} else {
System.out.println("表" + tableName + "不存在!");
}
}
/**
* 向表中插入数据
*
* @param tableName
* @param rowKey
* @param columnFamily
* @param column
* @param value
* @throws IOException
*/
public static void addRowData(String tableName,
String rowKey,
String columnFamily,
String column,
String value) throws IOException {
Connection connection = getConnection();
//查询数据
//获取指定表对象
Table table = connection.getTable(TableName.valueOf(tableName));
//注意字符编码问题
//Get get = new Get(rowkey.getBytes());
Get get = new Get(Bytes.toBytes(rowKey));
//查询结果
Result result = table.get(get);
boolean empty = result.isEmpty();
System.out.println("是否存在:" + empty);
if (empty) {
//新增数据
Put put = new Put(Bytes.toBytes(rowKey));
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));
table.put(put);
System.out.println("插入数据成功。。。");
} else {
System.out.println("已存在此数据。。。");
}
}
/**
* 删除多行数据
*
* @param tableName
* @param rows
* @throws IOException
*/
public static void deleteMultiRow(String tableName, String... rows) throws IOException {
Connection connection = getConnection();
Table table = connection.getTable(TableName.valueOf(tableName));
List<Delete> deleteList = new ArrayList<Delete>();
for (String row : rows) {
Delete delete = new Delete(Bytes.toBytes(row));
deleteList.add(delete);
}
table.delete(deleteList);
table.close();
}
/**
* 获取所有数据
*
* @param tableName
* @throws IOException
*/
public static void getAllRows(String tableName) throws IOException {
Connection connection = getConnection();
Table table = connection.getTable(TableName.valueOf(tableName));
//得到用于扫描 region 的对象
Scan scan = new Scan();
//使用 HTable 得到 resultcanner 实现类的对象
ResultScanner resultScanner = table.getScanner(scan);
for (Result result : resultScanner) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
//上下两个循环待改
System.out.println(" value " + Bytes.toString(CellUtil.cloneValue(cell)));
System.out.println(" rowkey " + Bytes.toString(CellUtil.cloneRow(cell)));
System.out.println(" family " + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println(" column " + Bytes.toString(CellUtil.cloneQualifier(cell)));
}
}
}
/**
* 获取某一行数据
*
* @param tableName
* @param rowKey
* @throws IOException
*/
public static void getRow(String tableName, String rowKey) throws IOException {
Connection connection = getConnection();
Table table = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowKey));
//get.setMaxVersions();显示所有版本
//get.setTimeStamp();显示指定时间戳的版本
Result result = table.get(get);
for (Cell cell : result.rawCells()) {
System.out.println(" value " + Bytes.toString(CellUtil.cloneValue(cell)));
System.out.println(" rowkey " + Bytes.toString(CellUtil.cloneRow(cell)));
System.out.println(" family " + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println(" column " + Bytes.toString(CellUtil.cloneQualifier(cell)));
}
}
}
项目地址
目前还在github(太慢了最近在迁移项目到gitee)
今天刚踩的坑
如有问题欢迎讨论, 我在 后端码匠
等你