【HBase】HBase入门详解(二)
(图片来源于网络,侵删)一、HBASE集群安装前提:Hadoop集群要启动正常,Zookeeper集群启动正常【1】解压tar包tar -zxf hbase-1.2.0-cdh5.14.0.tar.gz ../servers/hbase【2】配置环境变量(根据自己环境配置)vim /etc/profile.d/hbase.shexport HBASE_HOME=/ex...
·
(图片来源于网络,侵删)
一、HBASE集群安装
前提:Hadoop集群要启动正常,Zookeeper集群启动正常
【1】解压tar包
tar -zxf hbase-1.2.0-cdh5.14.0.tar.gz /opt/modules
mv hbase-1.2.0-cdh5.14.0 hbase
【2】配置环境变量(根据自己环境配置)
vim /etc/profile.d/hbase.sh
export HBASE_HOME=/export/servers/hbase
export PATH=$PATH:$HBASE_HOME/bin
source /etc/profile
【3】修改配置文件
cd $HBASE_HOME/conf
【4】修改配置文件
vim hbase-env.sh
27行左右配置JAVA_HOME
46、47行左右注释掉,JDK1.7需要使用,我们使用的JDK1.8,所以不需要,注释掉,不然启动Hbase会有警告,看着‘烦’
128行左右取消注释,修改true为false,使用我们自己Zookeeper
vim hbase-site.xml
根据自己环境添加如下代码↓↓↓
<configuration>
<!-- Hbase在HDFS上的根目录 -->
<property>
<name>hbase.rootdir</name>
<value>hdfs://mycluster:8020/hbase</value>
</property>
<!-- Hbase集群模式,false表示hbase单机模式,true表示是分布式模式,默认false-->
<property>
<name>hbase.cluster.distributed</name>
<value>true</value>
</property>
<!-- 0.98后的新变动,之前版本没有.port,默认端口为60000 -->
<!-- hbase master节点端口 -->
<property>
<name>hbase.master.port</name>
<value>16000</value>
</property>
<!-- 添加zookeeper节点 -->
<property>
<name>hbase.zookeeper.quorum</name>
<value>node01,node02,node03</value>
</property>
<property>
<name>hbase.zookeeper.property.clientPort</name>
<value>2181</value>
</property>
<!-- 配置hbase的数据在zookeeper上存储的目录,也就是Zookeeper的myid所在的目录 -->
<property>
<name>hbase.zookeeper.property.dataDir</name>
<value>/opt/modules/zookeeper/zkdata</value>
</property>
</configuration>
vim regionservers
添加RegionServer节点主机名
vim backup-masters(配置备份master,此文件默认不存在)
添加Master备份节点主机名
【5】软连接Hadoop配置到HBase下
因为Hbase基于HDFS,所以需要读取Hadoop配置
ln -s $HADOOP_HOME/etc/hadoop/core-site.xml $HBASE_HOME/conf/core-site.xml
ln -s $HADOOP_HOME/etc/hadoop/hdfs-site.xml $HBASE_HOME/conf/hdfs-site.xml
【6】分发安装包和环境变量
scp -r hbase node02:`pwd`
scp -r hbase node03:`pwd`
scp hbase.sh node02:`pwd`
scp hbase.sh node03:`pwd`
注意:
分发完记得 source /etc/profile
【7】启动并使用JPS查看
在主节点启动时记得免密钥哦!
群起hbase集群:
start-hbase.sh
单独启动master:
hbase-daemon.sh start master
单独启动regionserver :
hbase-daemon.sh start regionserver
jps查看
也可以通过访问WEB页面 主机名:60010 来查看
或使用 hbase shell命令进入 hbase shell窗口 使用 list 命令查看
二、HBase配置文件详解
-
backup-masters
- 这是一个纯文本文件,其中列出了主服务器应在其上启动备份主进程的主机列表,每行一台主机名(默认情况下不存在) -
hadoop-metric2-hbase.properties
- 用于连接HBase和Hadoop的Metric2框架,默认情况下只包含注释出的示例 -
hbase-env.cmd 和 hbase-env.sh
- 用于Windows和Linux/Unix环境的脚本,用于设置HBase的工作环境,包括 JAVA和其他环境变量的配置(该文件包含许多注释示例来提供指导,该文件的改动需要重启HBase才能生效) -
hbase-policy.xml
- RPC服务器使用默认策略配置文件对客户端请求进行授权决策(仅在启用HBase安全性的情况下使用) -
hbase-site.xml
- 主要的HBase配置文件,该文件覆盖了HBase的默认配置的配置选项(可以在HBase Web UI的HBase配置选项中查看整个集群的有效配置,包括默认和覆盖的) -
log4j.properties
- HBase日志记录的配置文件 -
regionservers
- 纯文本文件,包含在HBase集群中运行RegionSever的主机名或IP列表,每行一个(默认情况下,这个文件包含单条条目 localhost)
三、HBase Shell
1.进入HBase客户端命令操作界面
hbase shell
2.查看帮助命令
help
3.统计一张表有多少行数据
count 'table'
4.显示服务器状态
status '主机名'
5.显示HBase当前用户
whoami
6.检查表是否存在,适用于表量特别多的情况
exists 'table'
7.检查表是否启用或禁用
is_enabled 'table'
is_disabled 'table'
8.启用一张表/禁用一张表
enable 'table'
disable 'table'
DDL:
增
1.创建表
create 'table','columnFamily'...'columnFamily'...
2.创建命名空间
create_namespace '命名空间名'
2.创建表并指定版本数
create 'table',{NAME => 'columnFamily',VERSIONS => num}...
删
1.删除表(先禁用,再删除)
(1).disable 'table'
(2).drop 'table'
2.删除命名空间(先禁用命名空间下所有表,再删除所有表,最后删除命名空间)
(1).disable_all '命名空间名.*'
(2).drop_all '命名空间名.*'
(3).drop_namespace '命名空间名'
改
1.添加列族/修改列族版本数
默认VERSION版本
alter 'table','columnFamily'...
指定VERSION版本
alter 'table',{NAME => 'columnFamily',VERSIONS => num}...
查
1.查看所有表
list
2.查看表结构信息
desc(describe) 'table'
3.查看所有命名空间
list_namespace
DML:
增/改
1.表添加/修改数据
put 'table','rowkey','columnFamily:column','value',(timestamp)
删
1.表删除列
delete 'table','rowkey','columnFamily:column',(timestamp)
2.表删除列族
(1).alter 'table', NAME => 'columnFamily', METHOD => 'delete'
alter 'table', 'delete' => 'columnFamily'
3.表删除rowkey
(1).delete 'table','rowkey'
(2).deleteall 'table','rowkey'
4.清空表数据
truncate 'table'
查
1.查询表的所有数据
scan 'table'
2.根据 列族/列名 查询表的数据
scan 'table',{COLUMN => 'columnFamily:column'}
scan 'table', {COLUMNS => ['columnFamily:column'...],VERSIONS => num}
3.根据 rowkey 查询指定范围(左闭右开)
scan 'table',{STARTROW => 'rowkey',ENDROW => 'rowkey'}
4.查询版本号为num以内的表的所有数据
scan 'table',{RAW => true , VERSIONS => num}
scan 'table',{COLUMNs => 'columnFamily',RAW => true , VERSIONS => num}
5.scan模糊查询
scan 'table',{FILTER=>"PrefixFilter('startStr')"}
scan 'table',{TIMERANGE => [时间戳1, 时间戳2]}
6.get查询
(1).get 'table','rowkey'
(2).get 'table','rowkey','columnFamily'...
(3).get 'table','rowkey','columnFamily:column'...
(4).get 'table','rowkey',{COLUMN => 'columnFamily:column',VERSIONS => num}
(5).get 'table','rowkey',{FILTER => "ValueFilter(=, 'binary:str')"}
四、HBase API
【1】API基本操作
创建Maven工程,导入jar包
<repositories>
<repository>
<id>cloudera</id>
<url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.6.0-mr1-cdh5.14.0</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.2.0-cdh5.14.0</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>1.2.0-cdh5.14.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<!-- <verbal>true</verbal>-->
</configuration>
</plugin>
<!--将我们其他用到的一些jar包全部都打包进来 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<minimizeJar>false</minimizeJar>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
(1)HBaseAPI基本操作
public class HBaseCRUD{
private static Admin admin;
private static Connection connection;
/**
* 初始化参数
*/
static {
try {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "node01:2181,node02:2181,node03:2181");
connection = ConnectionFactory.createConnection(conf);
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 关闭资源
*/
private static void close() {
if (admin != null) {
try {
admin.close();
} catch (IOException e) {
System.out.println("admin为null!!!");
}
}
if (connection != null) {
try {
connection.close();
} catch (IOException e) {
System.out.println("connection为null!!!");
}
}
}
public static void main(String[] args) throws IOException {
createNameSpace("MyNameSpace");
createTable("MyNameSpace:student", "info", "message");
putData("MyNameSpace:student","1000","info","name","lisi");
putData("MyNameSpace:student","1000","info","name","zhangsan");
putData("MyNameSpace:student","1001","info","name","wangwu");
getData("MyNameSpace:student","1000","info","name");
scanDatas("MyNameSpace:student");
deleteTables("MyNameSpace:student");
deleteNameSpace("MyNameSpace");
}
/**
* 创建命名空间
*
* @param nameSpace
*/
private static void createNameSpace(String nameSpace) throws IOException {
try {
NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create(nameSpace).build();
admin.createNamespace(namespaceDescriptor);
System.out.println(nameSpace + " 命名空间创建成功!");
} catch (NamespaceExistException e) {
System.out.println(nameSpace + " 命名空间已经存在!");
}
}
/**
* 删除命名空间
*
* @param nameSpace
*/
private static void deleteNameSpace(String nameSpace) throws IOException {
try {
admin.deleteNamespace(nameSpace);
} catch (NamespaceNotFoundException e) {
System.out.println(nameSpace + " 命名空间不存在!!!");
}
}
/**
* 校验表是否存在
*
* @param tableName
* @return
* @throws IOException
*/
private static Boolean checkTableExists(String tableName) throws IOException {
return admin.tableExists(TableName.valueOf(tableName));
}
/**
* 创建表
*
* @param tableName
* @param cfs
*/
private static void createTable(String tableName, String... cfs) throws IOException {
if (checkTableExists(tableName)) {
System.out.println(tableName + " 表已存在!!!");
return;
}
// 1.创建表描述器
HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
// 2.创建列描述器
HColumnDescriptor hColumnDescriptor;
// 3.循环添加列簇
for (String cf : cfs) {
hColumnDescriptor = new HColumnDescriptor(cf);
hTableDescriptor.addFamily(hColumnDescriptor);
}
// 4.创建表
admin.createTable(hTableDescriptor);
}
/**
* 删除一个或多个表
*
* @param tableName
* @throws IOException
*/
private static void deleteTables(String... tableName) throws IOException {
// 判断可变数组长度,如果为 1 则只需要删除一个表,不为 1则需要循环删除
if (tableName.length == 1) {
// 获取表名
String name = tableName[0];
// 校验表是否存在,不存在直接返回
if (!checkTableExists(name)) {
System.out.println(tableName + " 表不存在!!!");
return;
}
TableName value = TableName.valueOf(name);
// 存在就删除该表,先禁用,再删除
admin.disableTable(value);
admin.deleteTable(value);
} else {
// 循环遍历表
for (String name : tableName) {
// 校验表是否存在,不存在则跳过
if (!checkTableExists(name)) {
continue;
}
// 存在就删除该表,先禁用,再删除
TableName value = TableName.valueOf(name);
admin.disableTable(value);
admin.deleteTable(value);
}
}
}
/**
* 插入数据
* @param tableName 表名
* @param rowKey rowKey
* @param cf 列簇
* @param cn 列
* @param value 值
* @throws IOException
*/
private static void putData(String tableName, String rowKey, String cf, String cn, String value) throws IOException {
// 1.校验表是否存在,不存在直接返回
if (!checkTableExists(tableName)) {
System.out.println(tableName + " 表不存在!!!");
return;
}
// 2.获取Table表对象
Table table = connection.getTable(TableName.valueOf(tableName));
// 3.获取Put对象
Put put = new Put(Bytes.toBytes(rowKey));
// 4.添加列
put.addColumn(Bytes.toBytes(cf),Bytes.toBytes(cn),Bytes.toBytes(value));
// 5.插入数据
table.put(put);
// 6.关闭表连接
table.close();
System.out.println("数据插入成功!");
}
/**
* 获取数据
* @param tableName 表名
* @param rowKey rowKey
* @param cf 列簇
* @param cn 列
* @throws IOException
*/
private static void getData(String tableName, String rowKey, String cf, String cn) throws IOException {
// 1.校验表是否存在,不存在直接返回
if (!checkTableExists(tableName)) {
System.out.println(tableName + " 表不存在!!!");
return;
}
Table table = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(Bytes.toBytes(rowKey));
get.addColumn(Bytes.toBytes(cf),Bytes.toBytes(cn));
Result result = table.get(get);
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
}
table.close();
}
/**
* 获取数据
* @param tableName 表名
* @param startRow startRow 查询的最小Row
* @param stopRow stopRow 查询的最大ow
* @throws IOException
*/
private static void scanDatas(String tableName,String startRow,String stopRow) throws IOException {
// 1.校验表是否存在,不存在直接返回
if (!checkTableExists(tableName)) {
System.out.println(tableName + " 表不存在!!!");
return;
}
// 2.获取Table表对象
Table table = connection.getTable(TableName.valueOf(tableName));
// 3.获取Scan对象
Scan scan = new Scan();
// 4.判断是否设置开始row
if(startRow != null){
scan.setStartRow(Bytes.toBytes(startRow));
}
// 5.判断是否设置结束row
if(stopRow != null){
scan.setStopRow(Bytes.toBytes(stopRow));
}
// 6.获取scanner对象
ResultScanner scanner = table.getScanner(scan);
// 7.遍历打印结果
for (Result result : scanner) {
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.println(Bytes.toString(CellUtil.cloneValue(cell)));
}
}
// 8.关闭资源
table.close();
}
/**
* 获取数据
* @param tableName 表名
* @throws IOException
*/
private static void scanDatas(String tableName) throws IOException {
scanDatas(tableName, null,null);
}
/**
* 获取数据
* @param tableName 表名
* @param startRow 查询的最小Row
* @throws IOException
*/
private static void scanDatas(String tableName,String startRow) throws IOException {
scanDatas(tableName, startRow,null);
}
}
都看到这里了,点赞评论一下吧!!!
点击查看👇
【HBase】HBase入门详解(三)
更多推荐
已为社区贡献2条内容
所有评论(0)