基于Java API编程接口的 HBase Shell交互式命令行终端模拟程序

1.hadoop完全分布式

前期准备
三台linux,yum和网络都配置好
centos网络配置文件模板

BOOTPROTO=static
DEVICE=eno16777736
ONBOOT=yes
IPADDR=192.168.26.10
GATEWAY=192.168.26.2
DNS=8.8.8.8

能ping通ip却不能ping通域名
1.编辑配置文件

 vi /etc/resolv.conf

2.写入下列地址,保存退出,reboot重启

nameserver 8.8.8.8
nameserver 8.8.4.4

配置yum
1.备份原文件

cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

2.下载并设置阿里yum源

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

如果没有wget尝试

curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

3.清理和生成缓存

yum clean all    
yum makecache     

4.重启网卡

systemctl restart network

1.修改主机名

master下执行

hostnamectl set-hostname master
bash

slave1下执行

hostnamectl set-hostname slave1
bash

slave2下执行

hostnamectl set-hostname slave2
bash

2.主机映射
三台都要执行

vi /etc/hosts

末尾写入

192.168.26.148 master
192.168.26.149 slave1
192.168.26.150 slave2

3.ssh免密
仅master执行
master输入

ssh-keygen -t rsa

三次回车出现密文如下(大概这个样子,不完全一样)

+--[ RSA 2048]----+
|  .o.+.   ..     |
|. . o..   .E.    |
|.o.++  . * .     |
|..+o..  + =      |
|   +.   S+       |
|    .. .  .      |
|      .          |
|                 |
|                 |
+-----------------+
ssh-copy-id -i /root/.ssh/id_rsa.pub master
ssh-copy-id -i /root/.ssh/id_rsa.pub slave1
ssh-copy-id -i /root/.ssh/id_rsa.pub slave2

依次输入 yes和root 用户的密码
依次验证

ssh master
ssh slave1
ssh slave2

登录一次就及时退出一次(exit)

4.关闭防火墙
三台都要执行

systemctl stop firewalld
systemctl disable firewalld

查看防火墙状态

 systemctl status firewalld

5.安装jdk
仅master执行
解压缩

tar -zxvf jdk-8u152-linux-x64.tar.gz  -C  /usr/

过去改个好名字
直接改成jdk

6.安装hadoop
仅master执行
解压缩

tar -zxvf hadoop-2.7.1.tar.gz -C  /usr

过去改个好名字
直接改成hadoop
7.hadoop配置
仅master执行
①进入目录

cd /usr/hadoop/etc/hadoop

② core-site.xml

<configuration>
 <property>
 <name>fs.defaultFS</name> 
 <value>hdfs://master:8020</value> 
 </property> 
 <property>
 <name>hadoop.tmp.dir</name>
 <value>/var/log/hadoop/tmp</value>
 </property>
</configuration>

③ hadoop-env.sh

export JAVA_HOME=/usr/jdk

④ hdfs-site.xml

<configuration>
<property>
 <name>dfs.namenode.name.dir</name>
 <value>file:///data/hadoop/hdfs/name</value>
</property>
<property>
 <name>dfs.datanode.data.dir</name>
 <value>file:///data/hadoop/hdfs/data</value>
</property>
<property>
 <name>dfs.namenode.secondary.http-address</name>
 <value>master:50090</value>
</property>
<property>
 <name>dfs.replication</name>
 <value>3</value>
</property>
</configuration>

⑤ mapred-site.xml
目录中默认没有该文件,需要先通过如下命令将文件复制并重命名为“mapred-site.xml”

cp mapred-site.xml.template mapred-site.xml
<configuration>
<property>
 <name>mapreduce.framework.name</name>
 <value>yarn</value>
</property>
<!-- jobhistory properties -->
<property>
 <name>mapreduce.jobhistory.address</name>
 <value>master:10020</value>
</property>
<property>
 <name>mapreduce.jobhistory.webapp.address</name>
 <value>master:19888</value>
</property>
</configuration>

⑥ yarn-site.xml

<configuration>
 <property>
 <name>yarn.resourcemanager.hostname</name>
 <value>master</value>
 </property> 
 <property>
 <name>yarn.resourcemanager.address</name>
 <value>${yarn.resourcemanager.hostname}:8032</value>
 </property>
 <property>
 <name>yarn.resourcemanager.scheduler.address</name>
 <value>${yarn.resourcemanager.hostname}:8030</value>
 </property>
 <property>
 <name>yarn.resourcemanager.webapp.address</name>
 <value>${yarn.resourcemanager.hostname}:8088</value>
 </property>
 <property>
 <name>yarn.resourcemanager.webapp.https.address</name>
 <value>${yarn.resourcemanager.hostname}:8090</value>
 </property>
 <property>
 <name>yarn.resourcemanager.resource-tracker.address</name>
 <value>${yarn.resourcemanager.hostname}:8031</value>
 </property>
 <property>
 <name>yarn.resourcemanager.admin.address</name>
 <value>${yarn.resourcemanager.hostname}:8033</value>
 </property>
 <property>
 <name>yarn.nodemanager.local-dirs</name>
 <value>/data/hadoop/yarn/local</value>
 </property>
 <property>
 <name>yarn.log-aggregation-enable</name>
 <value>true</value>
 </property>
 <property>
 <name>yarn.nodemanager.remote-app-log-dir</name>
 <value>/data/tmp/logs</value>
 </property>
<property> 
<name>yarn.log.server.url</name> 
<value>http://master:19888/jobhistory/logs/</value>
<description>URL for job history server</description>
</property>
<property>
 <name>yarn.nodemanager.vmem-check-enabled</name>
 <value>false</value>
 </property>
<property>
 <name>yarn.nodemanager.aux-services</name>
 <value>mapreduce_shuffle</value>
 </property>
 <property>
 <name>yarn.nodemanager.aux-services.mapreduce.shuffle.class</name>
 <value>org.apache.hadoop.mapred.ShuffleHandler</value>
 </property>
<property> 
 <name>yarn.nodemanager.resource.memory-mb</name> 
 <value>2048</value> 
</property> 
<property> 
 <name>yarn.scheduler.minimum-allocation-mb</name> 
 <value>512</value> 
</property> 
<property> 
 <name>yarn.scheduler.maximum-allocation-mb</name> 
 <value>4096</value> 
</property> 
<property> 
 <name>mapreduce.map.memory.mb</name> 
 <value>2048</value> 
</property> 
<property> 
 <name>mapreduce.reduce.memory.mb</name> 
 <value>2048</value> 
</property> 
<property> 
 <name>yarn.nodemanager.resource.cpu-vcores</name> 
 <value>1</value> 
</property>
</configuration>

⑦ yarn-env.sh

export JAVA_HOME=/usr/jdk

⑧ slaves
删除原有内容
添加

slave1
slave2

⑨配置环境变量

vi /etc/profile

写入

#jdk
export JAVA_HOME=/usr/jdk
export PATH=$PATH:$JAVA_HOME/bin

#hadoop
export HADOOP_HOME=/usr/hadoop
export PATH=$HADOOP_HOME/bin:$PATH:$HADOOP_HOME/sbin

刷新环境变量

source /etc/profile

8.分发文件
jdk

scp -r /usr/jdk slave1:/usr/
scp -r /usr/jdk slave2:/usr/

hadoop

scp -r /usr/hadoop slave1:/usr/
scp -r /usr/hadoop slave2:/usr/

环境变量

scp -r /etc/profile slave1:/etc/profile
scp -r /etc/profile slave2:/etc/profile

刷新环境变量

source /etc/profile

9.格式化

hdfs namenode -format

10.启动集群

start-dfs.sh
start-yarn.sh

11.查看集群状态
①jps
master

NameNode
SecondaryNameNode
Jps
ResourceManager

slave

Jps
DataNode
NodeManager

②web

http://192.168.26.148:50070
http://192.168.26.148:8088

12.执行MapReduce词频统计任务
本地创建文件

cd /root
vi 1.txt

写入

Give me the strength lightly to bear my joys and sorrows.Give me the strength to make my love fruitful in service.Give me the strength never to disown the poor or bend my knees before insolent might.Give me the strength to raise my mind high above daily trifles.And give me the strength to surrender my strength to thy will with love.

创建文件夹并上传文件

hadoop fs -mkdir /input
hadoop fs -put /root/1.txt /input

执行命令

cd /usr/hadoop/share/hadoop/mapreduce
hadoop jar hadoop-mapreduce-examples-2.7.1.jar wordcount  /input/1.txt /output

此处的hadoop-mapreduce-examples-2.7.1.jar 根据实际版本填写
查看结果

hadoop fs -ls /output  
hadoop fs -cat /output/part-r-00000

如下

Give	1
above	1
and	1
bear	1
before	1
bend	1
daily	1
disown	1
fruitful	1
give	1
high	1
in	1
insolent	1
joys	1
knees	1
lightly	1
love	1
love.	1
make	1
me	5
might.Give	1
mind	1
my	5
never	1
or	1
poor	1
raise	1
service.Give	1
sorrows.Give	1
strength	6
surrender	1
the	6
thy	1
to	6
trifles.And	1
will	1
with	1

2.zookeeper完全分布式

1.安装jdk(前面已经安装了,可以跳过这一步)
解压缩
解压到目标文件夹
-C 指定路径

tar -zxvf jdk-8u261-linux-x64.tar.gz -C /usr/

过去改个好名字,直接改成jdk

配置环境变量

执行命令vi /etc/profile 增加:

#jdk
export JAVA_HOME=/usr/jdk
export PATH=$PATH:$JAVA_HOME/bin

执行命令 source /etc/profile 刷新环境变量
输入java -version检查安装成功与否
2.安装zookeeper
解压缩

tar -zxvf zookeeper-3.4.8.tar.gz -C  /usr/

过去改个好名字,直接改成zookeeper

编辑环境变量

vi  /etc/profile

写入以下内容

#zk
export ZOOKEEPER_HOME=/usr/zookeeper
export PATH=$PATH:$JAVA_HOME/bin:$ZOOKEEPER_HOME/bin

刷新环境变量

source /etc/profile

进入zk安装目录下的conf,编辑配置文件
复制模板

cp zoo_sample.cfg zoo.cfg
vi zoo.cfg

写入以下

server.1=192.168.26.148:2888:3888
server.2=192.168.26.149:2888:3888
server.3=192.168.26.150:2888:3888

dataDir=/tmp/zookeeper我使用默认的,可以自行修改
创建临时文件目录
三台都要创建

mkdir /tmp/zookeeper

分发文件

scp -r /usr/zookeeper/ slave1:/usr/
scp -r /usr/zookeeper/ slave2:/usr/
 scp /etc/profile slave1:/etc/
 scp /etc/profile slave2:/etc/

刷新

source  /etc/profile

分别写入myid
master

echo 1 >  /tmp/zookeeper/myid

slave1

echo 2 >  /tmp/zookeeper/myid

slave2

echo 3 >  /tmp/zookeeper/myid

3.启动zk
三台都要启动

 zkServer.sh start

jps看一下会多一个进程

QuorumPeerMain

查看状态

zkServer.sh status

应该有一个leader,其余为follower

登录终端

zkCli.sh -server master:2181

3.hbase完全分布式

1.解压缩

tar -zxvf hbase-1.4.13-bin.tar.gz  -C /usr/

改个好名字

mv /usr/hbase-1.4.13/ /usr/hbase

2.编辑配置文件

cd /usr/hbase/conf/
vi hbase-site.xml

修改<configuration></configuration>为以下内容

<configuration>
<property>
<name>hbase.rootdir</name>
<value>hdfs://master:8020/hbase</value>
</property>
<property>
<name>hbase.master</name>
<value>master</value>
</property>
<property>
<name>hbase.cluster.distributed</name>
<value>true</value>
</property>
<property>
<name>hbase.zookeeper.property.clientPort</name>
<value>2181</value>
</property>
<property>
<name>hbase.zookeeper.quorum</name>
<value>slave1,slave2</value>
</property>
<property>
<name>zookeeper.session.timeout</name>
<value>60000000</value>
</property>
<property>
<name>dfs.support.append</name>
<value>true</value>
</property>
</configuration>
vi hbase-env.sh

注释下面两句

export HBASE_MASTER_OPTS="$HBASE_MASTER_OPTS -XX:PermSize=128m -XX:MaxPermSize=128m"
export HBASE_REGIONSERVER_OPTS="$HBASE_REGIONSERVER_OPTS -XX:PermSize=128m -XX:MaxPermSize=128m"

末行模式下 ?和 / 可查找
配置以下内容

export HBASE_CLASSPATH=/usr/hadoop/etc/hadoop
export JAVA_HOME=/usr/jdk
export HBASE_MANAGES_ZK=false
vi regionservers

写入

slave1
slave2
vi /etc/profile
export HBASE_HOME=/usr/hbase
export PATH=$PATH:$HBASE_HOME/bin

刷新

source /etc/profile

3.分发文件
hbase

scp -r /usr/hbase/ slave1:/usr/
scp -r /usr/hbase/ slave2:/usr/

环境变量

scp /etc/profile slave1:/etc
scp /etc/profile slave2:/etc

刷新

source /etc/profile

4.开启hbase
先开zk和hadoop

start-all.sh
zkServer.sh start

开hbase

start-hbase.sh

jps看看有没有

master:HMaster
slave:HRegionServer

看看web

192.168.26.148:16010

在这里插入图片描述
这就有了

4.开启进程

start-all.sh
zkServer.sh start
start-hbase.sh

zookeeper需要三台都输入命令,其他两个只需要在master输入即可。
关闭顺序,先关hbase,然后zk,hadoop。

5.准备工作以及注意事项

修改

C:\Windows\System32\drivers\etc

hosts文件
在这里插入图片描述
添加这三行

下载hbase的安装目录下的lib文件夹
导入jar包,就是把这个文件夹中的jar导入(最好就直接用本机的)

6.连接测试

测试连接代码

import java.io.IOException;
 
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
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.TableName;
 
 
public class testHBase 
{
	public static void main(String[] args) throws IOException
	{
		//调用HBaseConfiguration类的create静态方法,创建一个Configuration类的对象
		Configuration configuration = HBaseConfiguration.create();
		
		//加载需要连接HBase的各项配置参数,记得在开发机win7的hosts文件中增加虚拟机的主机名和IP地址的对应关系
		configuration.set("hbase.zookeeper.quorum", "master");
        //configuration.set("hbase.rootdir", "hdfs://hadoop:8020/hbase");	
		//configuration.set("hbase.zookeeper.property.clientPort", "2181");
		
		//调用ConnectionFactory类的createConnection静态方法,创建一个Connection类的对象,用于连接HBase
		Connection  connection = ConnectionFactory.createConnection(configuration);
		
		//调用connection对象的getAdmin方法,获取一个Admin类的对象,用于执行HBase的管理操作
		Admin admin = connection.getAdmin();
		
		//调用TableName类的valueOf静态方法,得到一个TableName类的对象,tableName对象是表的名字
		TableName tableName = TableName.valueOf("testTable");
	
		//调用admin对象的tableExists方法,判断HBase中是否存在tableName这张表,存在则返回True
		boolean isExists = admin.tableExists(tableName);	
		
		if(isExists) 
		{
			System.out.println("Table exists");
		}
		else 
		{
			System.out.println("Table dose not exists");
		}	
	}
}

7.具体案例代码

import java.io.IOException;
import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.io.compress.Compression.Algorithm;

public class HBaseJavaAPIDemo 
{
	public static void main(String args[]) throws IOException
	{	
		HBaseJavaAPIUtils.init();//执行数据库操作之前,先 初始化,生成配置,建立连接
		
		//Demo1判断表是否存在,对应hbase shell的exists命令
		//输入参数为TableName(缺省default名字空间)或NameSpace:TableName两种方式,对应exists 't1'和exists 'ns1:t1'两种命令方式
	    System.out.println("Is Table exists? " + HBaseJavaAPIUtils.isTableExist("students"));
		System.out.println("Is Table exists? " + HBaseJavaAPIUtils.isTableExist("default:students"));
		System.out.println("Is Table exists? " + HBaseJavaAPIUtils.isTableExist("hbase:meta"));
	
		//Demo2:列出HBase中的所有表的表名,实现列出表名 ListTable方法,对应hbase shell的list命令
		//Demo2.1:列出默认名字空间default的所有表名,listTables方法重载1
		HBaseJavaAPIUtils.listTables();
		
		//Demo2.2:实现列出表名(默认名字空间) listTables方法重载2
		HTableDescriptor[] tableDescriptors = HBaseJavaAPIUtils.admin.listTables();
		HBaseJavaAPIUtils.listTables(tableDescriptors);
		
		//Demo2.3:实现列出表名(默认名字空间) listTables方法重载3
		TableName[] tableNames = HBaseJavaAPIUtils.admin.listTableNames();
		HBaseJavaAPIUtils.listTables(tableNames);
		
		//Demo2.4:实现列出指定的名字空间的 表名 listTables方法重载4
		HBaseJavaAPIUtils.listTables("hbase");
		
		//Demo2.5:列出默认名字空间default的所有表名,listTables方法重载5
		HBaseJavaAPIUtils.listTables(HBaseJavaAPIUtils.admin);
		
		//Demo2.6:列出默认名字空间default的所有表名,listTables方法重载6
		HBaseJavaAPIUtils.listTables("default", HBaseJavaAPIUtils.admin);
		
		//Demo3: 与HBase名字空间有关的方法实现
		//Demo3.1:列出HBase中的所有名字空间
		HBaseJavaAPIUtils.listNameSpaces();
		
		//Demo3.2:判断HBase中是否存在指定名称的名字空间
		System.out.println(HBaseJavaAPIUtils.isNameSpacesExists("hbase"));
//		
		//Demo3.3:创建一个新的名字空间
		HBaseJavaAPIUtils.createNameSpace("my_namespace");
		
		//Demo3.4:删除指定的名字空间
		HBaseJavaAPIUtils.dropNameSpace("my_namespace");
				
		//Demo4:创建表,对应hbase shell的create命令		
		//Demo4.1:根据输入参数表名和一个列族名,创建只包含一个列族的表
		HBaseJavaAPIUtils.createTable("teachers", "info");
		HBaseJavaAPIUtils.createTable("my_namespace:my_table", "my_family");
		HBaseJavaAPIUtils.createTable("t1", "f1");
		HBaseJavaAPIUtils.createTable("t2", "f1");
		HBaseJavaAPIUtils.createTable("t3", "f1");
		
		//Demo4.2:根据输入参数表名和多个列族名的数组,创建包含多个列族的表
		//输入参数表名的格式为TableName(缺省default名字空间)或NameSpace:TableName两种方式,对应create 't1'和create 'ns1:t1'两种命令方式
		String[] strFamilies = new String[]{"family1", "family2", "family3"};
		HBaseJavaAPIUtils.createTable("teachers", strFamilies);
		HBaseJavaAPIUtils.createTable("my_namespace:teachers", strFamilies);
		
		//Demo5:根据输入参数表名删除表,对应hbase shell的drop命令
		//输入参数表名的格式为TableName(缺省default名字空间)或NameSpace:TableName两种方式,对应drop 't1'和drop 'ns1:t1'两种命令方式
		HBaseJavaAPIUtils.dropTable("t1");//实现drop表命令,删除表
		HBaseJavaAPIUtils.dropTable("default:t2");
		
		//Demo6:修改表,对应hbase shell的alter命令
		//Demo6.1:给表增加一个新的列族
		HBaseJavaAPIUtils.alterTable("t3", "f2");
		//Demo6.2:删除表的指定列族
		HBaseJavaAPIUtils.alterTable("t3", "f1", "");
		//Demo6.3:修改表的指定列族的最大版本数和最小版本数
		HBaseJavaAPIUtils.alterTable("students", "score", 3, 0);	
		HBaseJavaAPIUtils.alterTable("students", "info", HConstants.ALL_VERSIONS, 0);//设置最大版本数为最大整数值
		//Demo6.4:修改表的指定列族的压缩模式算法
		HBaseJavaAPIUtils.alterTable("students", "info", Algorithm.GZ);//设置指定列族的压缩模式算法为GZ
		HBaseJavaAPIUtils.alterTable("students", "info", Algorithm.NONE);//设置指定列族为无压缩
		
		//Demo7:向表中插入一个单元格值,对应hbase shell的put命令
		//Demo7.1:向表插入一个新的单元格值,指定表名,行键、列族和列,对应hbase shell的 put 't1', 'r1', 'c1', 'value'命令
		HBaseJavaAPIUtils.putACellValue("students", "s001", "info", "Name", "Jack");
		HBaseJavaAPIUtils.putACellValue("students", "s001", "score", "Hadoop", "95");
		HBaseJavaAPIUtils.putACellValue("students", "s002", "info", "Name", "Tom");
		HBaseJavaAPIUtils.putACellValue("students", "s002", "score", "HBase", "80");
		HBaseJavaAPIUtils.putACellValue("students", "s003", "info", "Name", "Tom");
		HBaseJavaAPIUtils.putACellValue("students", "s003", "info", "Name", "Hadoop");
		HBaseJavaAPIUtils.putACellValue("students", "s004", "info", "Name", "Jerry");
		HBaseJavaAPIUtils.putACellValue("students", "s005", "info", "Name", "Lucy");
		HBaseJavaAPIUtils.putACellValue("students", "s006", "info", "Name", "Lily");
		HBaseJavaAPIUtils.putACellValue("my_namespace:teachers", "001", "info", "age", "30");
		
		//Demo7.2:向表插入一个新的单元格值,指定表名,行键、列族、列和时间戳版本值,对应hbase shell的 put 't1', 'r1', 'c1', 'value', ts1命令
		HBaseJavaAPIUtils.putACellValue("students", "s001", "info", "name", "Jack", 1000000000000l);
		HBaseJavaAPIUtils.putACellValue("students", "s001", "info", "name", "Jack", 1000000000001l);
		HBaseJavaAPIUtils.putACellValue("students", "s001", "info", "name", "Jack", 1000000000002l);
		
		//Demo8:获取表中的单元格值,对应hbase shell的get命令
		//Demo8.1:根据表名、行键查询某行所包含的所有列的单元格值,对应hbase shell的get 't1', 'r1'命令
		HBaseJavaAPIUtils.getCellValue("students", "002");
		HBaseJavaAPIUtils.getCellValue("my_namespace:teachers", "001");
		
		//Demo8.2:根据表名、行键和列族名,查询某行所包含的所有列的单元格值,对应hbase shell的get 't1', 'r1'命令
		HBaseJavaAPIUtils.getCellValue("students", "002", "score");
		
		//Demo8.3:根据表名、行键、列族名和列,查询某行所包含的所有列的单元格值,也对应hbase shell的get 't1', 'r1'命令
		HBaseJavaAPIUtils.getCellValue("students", "002", "score", "HBase");
		
		//Demo9:扫描表,对应hbase shell的scan命令
		//Demo9.1:扫描整个表,对应hbase shell的scan 'ns1:t1'命令
		HBaseJavaAPIUtils.scanTable("students");
		HBaseJavaAPIUtils.scanTable("hbase:meta");
		
		//Demo9.2:扫描整个表的指定列族,对应hbase shell的scan 'hbase:meta', {COLUMNS => 'info'}命令
		HBaseJavaAPIUtils.scanTable("students", "info");
		HBaseJavaAPIUtils.scanTable("hbase:meta", "info");
		
		//Demo9.3:扫描整个表的指定列族和列名,对应hbase shell的scan 'hbase:meta', {COLUMNS => 'info:regioninfo'}命令
		HBaseJavaAPIUtils.scanTable("students", "info", "name");
		HBaseJavaAPIUtils.scanTable("hbase:meta", "info", "regioninfo");
		
		//Demo9.4:扫描整个表的指定行键范围,对应hbase shell的scan 'students', { STARTROW => '002', STOPROW =>'005'}命令
		HBaseJavaAPIUtils.scanTable("students", new String[]{"002", "005"});//扫描的行键范围是002至004
		HBaseJavaAPIUtils.scanTable("students", new String[]{"002", ""});//扫描的行键范围是002至结束
		HBaseJavaAPIUtils.scanTable("students", new String[]{"", "005"});//扫描的行键范围是开始至004
		HBaseJavaAPIUtils.scanTable("students", new String[]{"", ""});//扫描的行键范围是从开始到结束,即全部行键
		HBaseJavaAPIUtils.scanTable("students", new String[]{"001", "005"});//扫描的行键范围是001至004
		HBaseJavaAPIUtils.scanTable("students", new String[]{"000", "005"});//扫描的行键范围是001至004
		HBaseJavaAPIUtils.scanTable("students", new String[]{"00-1", "005"});//扫描的行键范围是001至004
		HBaseJavaAPIUtils.scanTable("students", new String[]{"00a", "005"});//开始行键的值非法,无扫描结果
		
		//Demo9.5:扫描整个表的指定时间戳范围,对应hbase shell的scan 'students', { TIMERANGE => [1587898000000, 1587898500000]}命令
		HBaseJavaAPIUtils.scanTable("students", new long[]{1587898000000l, 1587898500000l});//扫描的时间戳范围是1587898000000l至1587898500000l
		
		//Demo10:删除表中的指定列,对应hbase shell的delete命令
		//Demo10.1:删除指定列的单元格值,指定表名、行键、列族和列名,对应hbase shell的delete 'students', '001', 'info:name'命令
		HBaseJavaAPIUtils.deleteCellValue("students", "s002", "info", "name");
		
		//Demo10.2:删除指定行的所有列的单元格值,指定表名、行键,对应hbase shell的deleteall "students", '001'命令
		HBaseJavaAPIUtils.deleteCellValue("students", "s002");
		
		//Demo10.3:删除指定列的单元格值,指定表名、行键和时间戳,无对应的hbase shell的delete命令格式
		HBaseJavaAPIUtils.deleteCellValue("students", "s003", 1587898519275l);
		
		//Demo10.4:删除指定列的单元格值,指定表名、行行键、列族、列名和时间戳,无对应的hbase shell的delete命令格式
		HBaseJavaAPIUtils.deleteCellValue("students", "s003", "score", "Hadoop", 1592137954352l);
		
		HBaseJavaAPIUtils.close();//执行数据库操作之后,关闭连接,释放资源
	}		

}

import java.io.IOException;
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.NamespaceDescriptor;
import org.apache.hadoop.hbase.TableName;
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.io.compress.Compression.Algorithm;
import org.apache.hadoop.hbase.util.Bytes;

import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
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;


public class HBaseJavaAPIUtils 
{
	public static Configuration configuration;
	public static Connection connection;
	public static Admin admin;
	
	
	public static void init() throws IOException  //执行数据库操作之前,把与创建configuration, Connection, Admin相关的代码统一放到静态的初始化方法init(){
		configuration = HBaseConfiguration.create();		
		configuration.set("hbase.zookeeper.quorum", "hadoop");	
	
		connection = ConnectionFactory.createConnection(configuration);	//实质得到是Connection接口的间接实现类HConnectionImplementation的一个实例	
		System.out.println(connection.getClass().getName());
		admin = connection.getAdmin();//实质得到是Admin接口的直接接实现类HBaseAdmin的一个实例			
	}
	
	public static void close() throws IOException
	{
		connection.close();
		admin.close();
	}
	
	/**
	 * 将检查表名格式是否正确的代码提炼成一个方法供其他方法调用,提高代码的复用性
	* 检查输入参数的表名字符串的格式是否正确,只有TableName(缺省default名字空间)或NameSpace:TableName两种格式,正确则返回true,不正确则返回false
	* @parameter String  strTableName 表名
	* @return boolean 
	*/
	public static boolean isTableNameFormatCorrect(String strTableName)
	{
		System.out.println(strTableName.indexOf(":"));
		System.out.println(strTableName.lastIndexOf(":"));	
		System.out.println(strTableName.length());	
		//输入参数的合法性检查,提高方法代码的健壮性
		//包含不只1个冒号||第一个字符是冒号||最后一个字符是冒号
		if(strTableName.indexOf(":") != strTableName.lastIndexOf(":") || //第一个字符是冒号
		   strTableName.indexOf(":")==0 || //最后一个字符是冒号
		   strTableName.indexOf(":")==strTableName.length()-1)//包含不只1个冒号|
		{
			System.out.println("Input parameter format incorrect");
			return false;
		}
		return true;
	}
	
	/**
	* 判断表是否存在,输入参数为TableName(缺省default名字空间)或NameSpace:TableName两种方式,存在则返回true,不存在返回false
	* @parameter String  strTableName 表名
	* @return boolean 
	*/
	public static boolean isTableExist(String strTableName) throws IOException//判断表是否存在
	{	
		//处理不合法的情况,直接return
		if(!isTableNameFormatCorrect(strTableName)) 
		{
			return false;
		}
		
		TableName tableName  = TableName.valueOf(strTableName);	
		return admin.tableExists(tableName);
	}
	
	
	/**
	* 列出默认名字空间default的所有表的表名,方法重载1
	* @parameter void
	* @return void 
	*/
	public static void listTables() throws IOException//实现列出所有的表名
	{	
		//通过TableDesciptors类获取表名
//		HTableDescriptor[] tableDescriptors = admin.listTables();
//		for(HTableDescriptor tableDescriptor : tableDescriptors)
//		{
//			System.out.println(tableDescriptor.getTableName());//获取表名字符串
//		}	
		
		//通过TableName类获取表名
		TableName[] tableNames = admin.listTableNames();
		 for(TableName tableName : tableNames)
		 {	 
			 System.out.println(new String(tableName.getName()));//把字节数组byte[]转换成String
		 }
	}	
	
	/**
	* 列出默认名字空间default的所有表的表名,方法重载2
	* @parameter HTableDescriptor[] tableDescriptors
	* @return void 
	*/
	public static void listTables(HTableDescriptor[] tableDescriptors) throws IOException//实现列出所有的表名
	{	
		for(HTableDescriptor tableDescriptor : tableDescriptors)
		{
			System.out.println(tableDescriptor.getTableName());//获取表名字符串
		}	
	}	
	
	/**
	* 列出默认名字空间default的所有表的表名,方法重载3
	* @parameter TableName[] tableNames
	* @return void 
	*/
	public static void listTables(TableName[] tableNames) throws IOException//实现列出所有的表名
	{	
		 for(TableName tableName : tableNames)
		 {	
//			 System.out.println(new String(tableName.getName()));
			 System.out.println(tableName.getNameAsString());
		 }
	}	
	
	/**
	* 列出指定名字空间的所有表的表名,方法重载4
	* @parameter String strNameSpace 名字空间的名称
	* @return void 
	*/
//	public static void listNamespaceTables(String strNameSpace) throws IOException//实现列出所有的表名
	public static void listTables(String strNameSpace) throws IOException//实现列出所有的表名
	{	
		//检查名字空间是否存在
		if(!isNameSpacesExists(strNameSpace)) //不存在则直接返回
		{
			return;
		}
				
		TableName[] tableNames = admin.listTableNamesByNamespace(strNameSpace);		
		listTables(tableNames);
	}

	/**
	* 列出默认名字空间default的所有表的表名,方法重载5
	* @parameter Admin admin  输入参数是Admin的引用变量,解除了类的静态成员变量和类的成员方法之间的耦合性
	* @return void 
	*/
	public static void listTables(Admin admin) throws IOException//实现列出所有的表名
	{	
		//通过TableDesciptors类获取表名
//		HTableDescriptor[] tableDescriptors = admin.listTables();
//		for(HTableDescriptor tableDescriptor : tableDescriptors)
//		{
//			System.out.println(tableDescriptor.getTableName());//获取表名字符串
//		}		
		//通过TableName类获取表名
		TableName[] tableNames = admin.listTableNames();
		 for(TableName tableName : tableNames)
		 {	 
			 System.out.println(new String(tableName.getName()));//把字节数组byte[]转换成String
		 }
	}	
	
	/**
	* 列出指定名字空间的所有表的表名,方法重载6
	* @parameter String strNameSpace 名字空间的名称 ; Admin admin Admin类的引用变量,解除耦合性
	* @return void 
	*/
	public static void listTables(String strNameSpace, Admin admin) throws IOException//实现列出所有的表名
	{	
		//检查名字空间是否存在
		if(!isNameSpacesExists(strNameSpace)) //不存在则直接返回
		{
			return;
		}
				
		TableName[] tableNames = admin.listTableNamesByNamespace(strNameSpace);		
		listTables(tableNames);
	}
	
	/**
	* 判断表是否存在,输入参数为TableName(缺省default名字空间)或NameSpace:TableName两种方式,允许创建则返回true,不允许创建则返回false
	* @parameter String  strTableName 表名
	* @return boolean 
	*/
	public static boolean isTableAllowCreate(String strTableName) throws IOException//判断表是否存在
	{	
		if(!isTableNameFormatCorrect(strTableName)) //输入合法性检查
		{
			return false;
		}
    	//判断名字空间是否存在
		if(strTableName.indexOf(":") != -1) //如果表名字符串包含冒号,说明其格式是NameSpace:TableName
		{
			String strNameSpace = strTableName.substring(0, strTableName.indexOf(":"));//获取表名字符串中,冒号前面的子字符串NameSpace
			System.out.println(strNameSpace);
			if(!isNameSpacesExists(strNameSpace))//检查名字空间是否存在
			{
				return false;
			}
		}
		
		TableName tableName = TableName.valueOf(strTableName);
		if(admin.tableExists(tableName)) //检查表是否不存在,存在则返回true,表存在,不执行创建
		{
			System.out.println("Table already exists");
			return false;
		}
		return true;
	}
	
	/**
	* 实现创建一张表的方法,输入参数是表名和列族名,创建只包含一个列族的表(本方法可以被弃用,只使用另一个重载方法即可),重载1
	* @parameter String  strTableName 表名,  String strColumnFamilyName 列族名
	* @return void
	*/	
    public static void createTable(String strTableName, String strColumnFamilyName) throws IOException
	{
    	if(!isTableAllowCreate(strTableName)) //判断表是否允许创建
    	{
    		return;
    	}
    	
    	TableName tableName = TableName.valueOf(strTableName);
		HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
		HColumnDescriptor columnDescriptor = new HColumnDescriptor(strColumnFamilyName);
		tableDescriptor.addFamily(columnDescriptor);		
		admin.createTable(tableDescriptor);		
	}
	
    /**
	* 实现创建一张表的方法,输入参数是表名和多个列族名的数组,创建包含多列族的表,重载2
	* 其中输入参数表名的格式为TableName(缺省default名字空间)或NameSpace:TableName两种方式,注意创建表的NameSpace必须存在
	* @parameter String  strTableName 表名, String[] strColumnFamilyNames 多个列族名的数组
	* @return void
	*/	
    public static void createTable(String strTableName, String[] strColumnFamilyNames) throws IOException
	{
    	if(!isTableAllowCreate(strTableName)) //判断表是否允许创建
    	{
    		return;
    	}
    	
    	TableName tableName = TableName.valueOf(strTableName);
		HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);		
		for(String strColumnFamilyName:strColumnFamilyNames) 
		{
			HColumnDescriptor columnDescriptor = new HColumnDescriptor(strColumnFamilyName);
			tableDescriptor.addFamily(columnDescriptor);
		}				
		admin.createTable(tableDescriptor);		
	}
    
    /**
	* 判断表是否存在,输入参数为TableName(缺省default名字空间)或NameSpace:TableName两种方式,允许删除则返回true,不允许删除则返回false
	* @parameter String  strTableName 表名
	* @return boolean 
	*/
	public static boolean isTableAllowDrop(String strTableName) throws IOException//判断表是否存在
	{	
		if(!isTableExist(strTableName)) //表不允许被删除返回false
		{
			System.out.println("Table dose not exists");
			return false;
		}	
		TableName tableName = TableName.valueOf(strTableName);
		if(admin.isTableEnabled(tableName)) //判断表是否被禁用,如果是enabled,返回true
		{
			admin.disableTable(tableName);//删除前先将表禁用 ,使其off-line
		}
		return true;
	}
    
	/**
	* 实现删除表的方法,输入参数是要删除的表名
	* 输入参数表名的格式为TableName(缺省default名字空间)或NameSpace:TableName两种方式,对应drop 't1'和drop 'ns1:t1'两种命令方式
	* @parameter String  strTableName 表名字符串
	* @return void
	*/	
    public static void dropTable(String strTableName) throws IOException
	{
		if(!isTableAllowDrop(strTableName)) //返回false,表不允许删除
		{
			System.out.println("Table dose not allow to be dropped");
			return;
		}  	
		TableName tableName = TableName.valueOf(strTableName);
		admin.deleteTable(tableName);
		System.out.println("Table deleted successfully");
	} 
    
	/**
	* 列出HBase中所有的名字空间的名称
	* @parameter void
	* @return void 
	*/
	public static void listNameSpaces() throws IOException//实现列出所有的表名
	{
		NamespaceDescriptor[] namespaceDescriptors = admin.listNamespaceDescriptors();
		for(NamespaceDescriptor namespaceDescriptor: namespaceDescriptors) 
		{
			System.out.println(namespaceDescriptor.getName());//获取名字空间名称的字符串
		}	
	}
	
	/**
	* 判断HBase中是否存在指定名称的名字空间,输入参数是名字空间名称
	* @parameter String strNameSpace
	* @return boolean  存在返回true
	*/
	public static boolean isNameSpacesExists(String strNameSpace) throws IOException//实现列出所有的表名
	{
		NamespaceDescriptor[] namespaceDescriptors = admin.listNamespaceDescriptors();
		for(NamespaceDescriptor namespaceDescriptor: namespaceDescriptors) 
		{
			if(namespaceDescriptor.getName().equals(strNameSpace))//判断名字空间名称的字符串是否相等
			{
				return true;
			}
		}
		System.out.println("NameSpace does not exists");
		return false;
	}
	
	/**
	* 判断指定的名字空间里是否有表存在,输入参数是名字空间名称
	* @parameter String strNameSpace
	* @return boolean  存在表返回true 不存在表返回false
	*/
	public static boolean isNameSpacesHasTable(String strNameSpace) throws IOException//实现列出所有的表名
	{
		TableName[] tableNames = admin.listTableNamesByNamespace(strNameSpace);	
		if(tableNames.length <1) 
		{
			System.out.println("The namespace has no tables");
			return false;
		}
		return true;
	}

	/**
	* 创建一个新的名字空间,输入参数是名字空间的名称
	* @parameter String strNameSpace
	* @return void 
	*/
	public static void createNameSpace(String strNameSpace) throws IOException//实现列出所有的表名
	{
		if(isNameSpacesExists(strNameSpace))//如果名字空间存在
		{
			System.out.println("NameSpace already exists");
			return;
		}
		
//		NamespaceDescriptor namespaceDescriptor = new NamespaceDescriptor();//构造方法是私有的,不能直接调用
		NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create(strNameSpace).build();
		admin.createNamespace(namespaceDescriptor);
	}
	
	/**
	* 删除指定的名字空间,输入参数是名字空间的名称
	* @parameter String strNameSpace
	* @return void 
	*/
	public static void dropNameSpace(String strNameSpace) throws IOException//实现列出所有的表名
	{
		if(!isNameSpacesExists(strNameSpace))//如果名字空间不存在
		{
			return;
		}
				
		if(isNameSpacesHasTable(strNameSpace)) 
		{
			System.out.println("Only empty namespaces can be removed. The namespace has one or more tables");
			return;
		}
		
		admin.deleteNamespace(strNameSpace);
	}
	
	/**
	* 实现给表增加一个新列族的方法,输入参数是表名和新列族名
	* @parameter String  strTableName 表名,  String newColumnFamily 新列族名
	* @return void
	*/	
	public static void alterTable(String strTableName, String newColumnFamily) throws IOException
	{    
		if(!isTableExist(strTableName)) //表不存在则返回false
		{
			System.out.println("Table dose not exists");
			return;
		}
	
		TableName tableName = TableName.valueOf(strTableName);//获取表名对象	    	
		HTableDescriptor tableDescriptor = admin.getTableDescriptor(tableName);//获取表的描述器		
		
		if(isColumnFamilyExists(tableDescriptor, newColumnFamily)) //如果表的列族已存在,则直接返回
		{
			return;
		}
		
		HColumnDescriptor columnDescriptor = new HColumnDescriptor(newColumnFamily);//创建一个新的列族描述器			
		tableDescriptor.addFamily(columnDescriptor);//添加新列族描述器到表描述器		
		admin.modifyTable(tableName, tableDescriptor);	//提交修改表的操作
	 }
	
	/**
	* 实现删除表的指定列族的方法,输入参数是表名和要删除的列族名
	* @parameter String  strTableName 表名,  String delColumnFamily 要删除的列族名,执行的操作 String method
	* @return void
	*/
    public static void alterTable(String strTableName, String delColumnFamily, String method) throws IOException
	{ 			
    	if(!isTableExist(strTableName)) //表不存在则直接返回
    	{
    		System.out.println("Table dose not exists");
    		return;
    	}
    	
    	TableName tableName = TableName.valueOf(strTableName);	//获取表名对象
    	HTableDescriptor tableDescriptor = admin.getTableDescriptor(tableName);	//获取表的描述器
    	
		if(isTableHasOnlyOneFamily(tableDescriptor)) //如果表只包含一个列族,则直接返回
		{
			return;
		}
    		
		tableDescriptor.removeFamily(Bytes.toBytes(delColumnFamily));//表描述器中删除指定列族
		admin.modifyTable(tableName, tableDescriptor);	//提交修改表的操作
	}
    
	/**
	* 实现修改表的指定列族的最大和最小的版本数的方法,输入参数是表名,列族名,最大版本数,最小版本数
	* @parameter String  strTableName 表名,  String delColumnFamily 要删除的列族名,int maxVersion 最大版本数,,int minVersion 最小版本数
	* @return void
	*/
    public static void alterTable(String strTableName, String columnFamily, int maxVersion, int minVersion) throws IOException
	{	
		if(!isTableExist(strTableName)) //表不存在则直接返回
		{
			System.out.println("Table dose not exists");
			return;
		}
    	
    	if(maxVersion < 1 ||minVersion < 0 || maxVersion < minVersion) //输入合法性检查:最大版本数不能小于1,最小版本数不能小于0,最大版本数不能小于最小版本数
    	{
    		System.out.println("max versions or min versions is illegal" );
    		return;
    	}
    	
    	TableName tableName = TableName.valueOf(strTableName);//获取表名对象
		HTableDescriptor tableDescriptor = admin.getTableDescriptor(tableName);//获取表的描述器
		
		if(!isColumnFamilyExists(tableDescriptor, columnFamily)) //如果表的列族不存在,则直接返回
		{
			System.out.println("column family dose not exists");
			return;
		}
//		HColumnDescriptor columnDescriptor = new HColumnDescriptor(columnFamily);//不能采用创建一个新的列族描述器,原因是会用默认属性值覆盖掉之前已修改的属性值
		HColumnDescriptor columnDescriptor = tableDescriptor.getFamily(Bytes.toBytes(columnFamily));//应该获取要修改的列族的描述器,这样可以获得之前已修改的属性值
		
		System.out.println("Max Versions: " + columnDescriptor.getMaxVersions());//修改前的最大版本数,列值最多能保留的版本数
		System.out.println("Min Versions: " + columnDescriptor.getMinVersions());//修改前的最小版本数,列值至少须保留的版本数
		
//		if(maxVersion < columnDescriptor.getMinVersions()) //要修改的最大版本数不能小于当前的最小版本数
//		{
//			System.out.println("Maximum versions to be set must be equal or more than current minimum versions" );
//    		return;
//		}
		columnDescriptor.setMinVersions(minVersion);//先修改列值的最小版本数,避免出现要修改的最大版本数小于当前的最小版本数
		columnDescriptor.setMaxVersions(maxVersion);//再修改列值的最大版本数	
		tableDescriptor.modifyFamily(columnDescriptor);//修改列族	
		admin.modifyTable(tableName, tableDescriptor);//提交修改表的操作
		System.out.println("Max Versions: " + columnDescriptor.getMaxVersions());//修改后的最大版本数,列值最多能保留的版本数
		System.out.println("Min Versions: " + columnDescriptor.getMinVersions());//修改后的最小版本数,列值至少须保留的版本数
	}
    
	/**
	* 实现修改表的指定列族的压缩模式,输入参数是表名,列族名,压缩模式
	* @parameter String  strTableName 表名,  String delColumnFamily 要删除的列族名,Algorithm compression 要修改的压缩模式算法
	* @return void
	*/
    public static void alterTable(String strTableName, String columnFamily, Algorithm compression) throws IOException
	{			
    	if(!isTableExist(strTableName)) //表不存在则直接返回
    	{
    		System.out.println("Table dose not exists");
    		return;
    	}
    	
    	TableName tableName = TableName.valueOf(strTableName);//获取表名对象
		HTableDescriptor tableDescriptor = admin.getTableDescriptor(tableName);//获取表的描述器
		
		if(!isColumnFamilyExists(tableDescriptor, columnFamily)) //如果表的列族不存在,则直接返回
		{
			System.out.println("column family dose not exists");
			return;
		}
		
//		HColumnDescriptor columnDescriptor = new HColumnDescriptor(columnFamily);//不能采用创建一个新的列族描述器,原因是会用默认属性值覆盖掉之前设置的属性值创建一个新的列族描述器		
		HColumnDescriptor columnDescriptor = tableDescriptor.getFamily(Bytes.toBytes(columnFamily));//应该获取要修改的列族的描述器,这样可以获得之前已修改的属性值
		columnDescriptor.setCompressionType(compression);//修改压缩模式
		tableDescriptor.modifyFamily(columnDescriptor);//修改列族	
		admin.modifyTable(tableName, tableDescriptor);//提交修改表的操作
	}


	/**
	* 判断HBase中是否存在指定名称的名字空间,输入参数是名字空间名称
	* @parameter HTableDescriptor tableDescriptor, String strNameSpace
	* @return boolean  存在返回true
	*/
	public static boolean isColumnFamilyExists(HTableDescriptor tableDescriptor, String strFamiliyName) throws IOException//实现列出所有的表名
	{
		HColumnDescriptor[] hColumnDescriptors = tableDescriptor.getColumnFamilies();
		for(HColumnDescriptor hColumnDescriptor: hColumnDescriptors) 
		{
			if(hColumnDescriptor.getNameAsString().equals(strFamiliyName))
			{
				System.out.println("Column Family already exists");
				return true;
			}
		}
		return false;	
	}
	
	/**
	* 判断指定的表是否只有一个列族,输入参数是名字空间名称
	* @parameter HTableDescriptor tableDescriptor
	* @return boolean  只有一个列族返回true 有多个列族返回false
	*/
	public static boolean isTableHasOnlyOneFamily(HTableDescriptor tableDescriptor) throws IOException//实现列出所有的表名
	{
    	HColumnDescriptor[] hColumnDescriptors = tableDescriptor.getColumnFamilies();
    	if(hColumnDescriptors.length <=1) 
    	{
    		System.out.println("Table has only one Column Family");
			return true;
    	}
        return false;
	}
	
	/**
	* 实现向表中插入一个单元格值的方法,指定表名,行键、列族和列
	* @parameter String  strTableName 表名,  String rowKey 行键,String columnFamily 列族 ,String column 列 ,String value 单元格值
	* @return void
	*/
	public static void putACellValue(String strTableName, String rowKey, String columnFamily, String column, String value) throws IOException
	{
		if(!HBaseJavaAPIUtils.isTableExist(strTableName)) //如果表不存在则直接返回
		{
			System.out.println("Table dose not exists");
			return;
		}
    	
		TableName tableName = TableName.valueOf(strTableName);//获取表名对象 		
		if(!HBaseJavaAPIUtils.isColumnFamilyExists(HBaseJavaAPIUtils.admin.getTableDescriptor(tableName), columnFamily)) //如果列族不存在则直接返回
		{
			System.out.println("Column Family does not exists");
			return;
		}
    	
    	Table table =  HBaseJavaAPIUtils.connection.getTable(tableName);//获取操作表的Table对象
    	Put put = new Put(Bytes.toBytes(rowKey));//创建一个Put对象,指定行键rowKey
    	put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));//增加一列,指定列族、列修饰符、单元格值
    	table.put(put);	//执行put方法,插入一列
	}
	
	/**
	* 实现向表中插入一个单元格值的方法,指定表名,行键、列族、列和时间戳
	* @parameter String  strTableName 表名,  String rowKey 行键,String columnFamily 列族 ,String column 列 ,String value 单元格值,long timeStamp 时间戳版本值
	* @return void
	*/
	public static void putACellValue(String strTableName, String rowKey, String columnFamily, String column, String value, long timeStamp) throws IOException
	{
		if(!HBaseJavaAPIUtils.isTableExist(strTableName)) //如果表不存在则直接返回
		{
			System.out.println("Table dose not exists");
			return;
		}
    	
		TableName tableName = TableName.valueOf(strTableName);//获取表名对象 		
		if(!HBaseJavaAPIUtils.isColumnFamilyExists(HBaseJavaAPIUtils.admin.getTableDescriptor(tableName), columnFamily)) //如果列族不存在则直接返回
		{
			System.out.println("Column Family does not exists");
			return;
		}
    	
    	Table table =  HBaseJavaAPIUtils.connection.getTable(tableName);//获取操作表的Table对象
    	Put put = new Put(Bytes.toBytes(rowKey));//创建一个Put对象,指定行键rowKey
    	put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), timeStamp, Bytes.toBytes(value));	//增加一列,指定列族、列修饰符、时间戳版本值、单元格值
    	table.put(put);//执行put方法,插入一列
	}
	
	/**
	* 实现根据表名、行键查询某行所包含的所有列的单元格值的方法,指定表名,行键
	* @parameter String  strTableName 表名,  String rowKey 行键
	* @return void
	*/
    public static void getCellValue(String strTableName, String rowKey) throws IOException
	{	
		if(!HBaseJavaAPIUtils.isTableExist(strTableName)) //如果表不存在则直接返回
		{
			System.out.println("Table dose not exists");
			return;
		}
    	
    	TableName tableName = TableName.valueOf(strTableName); //获取表名对象
    	Table table =  HBaseJavaAPIUtils.connection.getTable(tableName);//获取操作表的Table对象    	
    	Get get = new Get(Bytes.toBytes(rowKey));//创建一个Get对象,指定行键rowKey 	
    	Result result = table.get(get);//执行get方法,获得指定列的指,返回结果
    	Cell[] cells = result.rawCells();//获取单元格对象的数组
    	
    	printCellValue(cells);//输出单元格数组的内容
	}
    
    /**
	* 实现根据表名、行键查询某行所包含的所有列的单元格值的方法,指定表名,行键和列族
	* @parameter String  strTableName 表名,  String rowKey 行键,String columnFamily 列族
	* @return void
	*/
    public static void getCellValue(String strTableName, String rowKey, String columnFamily) throws IOException
	{	
		if(!HBaseJavaAPIUtils.isTableExist(strTableName)) //如果表不存在则直接返回
		{
			System.out.println("Table dose not exists");
			return;
		}
		
		TableName tableName = TableName.valueOf(strTableName);//获取表名对象 
		if(!HBaseJavaAPIUtils.isColumnFamilyExists(HBaseJavaAPIUtils.admin.getTableDescriptor(tableName), columnFamily)) //如果列族不存在则直接返回
		{
			System.out.println("Column Family does not exists");
			return;
		}
    	
    	Table table =  HBaseJavaAPIUtils.connection.getTable(tableName);//获取操作表的Table对象    	
    	Get get = new Get(Bytes.toBytes(rowKey));//创建一个Get对象,指定行键rowKey
    	get.addFamily(Bytes.toBytes(columnFamily));//加入列族名作为查询条件
    	Result result = table.get(get);//执行get方法,获得指定列的指,返回结果
    	Cell[] cells = result.rawCells();//获取单元格对象的数组
    	
    	printCellValue(cells);//输出单元格数组的内容
	}
    
    /**
	* 实现根据表名、行键查询某行所包含的所有列的单元格值的方法,指定表名,行键,列族和列
	* @parameter String  strTableName 表名,  String rowKey 行键,String columnFamily 列族
	* @return void
	*/
    public static void getCellValue(String strTableName, String rowKey, String columnFamily, String column) throws IOException
	{	
		if(!HBaseJavaAPIUtils.isTableExist(strTableName)) //如果表不存在则直接返回
		{
			System.out.println("Table dose not exists");
			return;
		}
  	
    	TableName tableName = TableName.valueOf(strTableName); //获取表名对象		
    	if(!HBaseJavaAPIUtils.isColumnFamilyExists(HBaseJavaAPIUtils.admin.getTableDescriptor(tableName), columnFamily)) //如果列族不存在则直接返回
		{
			System.out.println("Column Family does not exists");
			return;
		}
    	
    	Table table =  HBaseJavaAPIUtils.connection.getTable(tableName);//获取操作表的Table对象    	
    	Get get = new Get(Bytes.toBytes(rowKey));//创建一个Get对象,指定行键rowKey
    	get.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column));//加入列族名和列名作为查询条件
    	Result result = table.get(get);//执行get方法,获得指定列的指,返回结果
    	Cell[] cells = result.rawCells();//获取单元格对象的数组
    	
    	printCellValue(cells);//输出单元格数组的内容
	}
    
	/**
	* 实现输出单元格值的方法,输入参数是单元格对象的数组
	* @parameter Cell[] cells 单元格对象的数组
	* @return void 
	*/
	public static void printCellValue(Cell[] cells) throws IOException//实现列出所有的表名
	{
        for (Cell cell : cells) 
        { 
            System.out.println("rowkey:" + Bytes.toString(CellUtil.cloneRow(cell)) + 
            		         " | column family:" + Bytes.toString(CellUtil.cloneFamily(cell)) + 
            		         " | column:" + Bytes.toString(CellUtil.cloneQualifier(cell)) + 
            		         " | timestamp:" + cell.getTimestamp() +
            		         " | cell value:" + Bytes.toString(CellUtil.cloneValue(cell)));
        }
	}
	
	/**
	* 实现扫描整个表的方法,输入参数是表名
	* @parameter String  strTableName 表名
	* @return void
	*/	
    public static void scanTable(String strTableName) throws IOException
	{	
		if(!HBaseJavaAPIUtils.isTableExist(strTableName)) //如果表不存在则直接返回
		{
			System.out.println("Table dose not exists");
			return;
		}
		
    	TableName tableName = TableName.valueOf(strTableName); //获取表名对象	
    	Scan scan = new Scan(); //创建扫描器对象   	
    	Table table =  HBaseJavaAPIUtils.connection.getTable(tableName);//获取操作表的Table对象   	
    	ResultScanner resultScanner = table.getScanner(scan);//获得用于检索数据的ResultScanner对象
    	
    	printScanResult(resultScanner);//输出扫描结果
	} 
    
	/**
	* 实现扫描整个表的方法,输入参数是表名和列族
	* @parameter String  strTableName 表名, String columnFamily 列族名
	* @return void
	*/	
    public static void scanTable(String strTableName, String columnFamily) throws IOException
	{	
		if(!HBaseJavaAPIUtils.isTableExist(strTableName)) //如果表不存在则直接返回
		{
			System.out.println("Table dose not exists");
			return;
		}
		
    	TableName tableName = TableName.valueOf(strTableName); //获取表名对象	
    	Scan scan = new Scan(); //创建扫描器对象   	
    	scan.addFamily(Bytes.toBytes(columnFamily));//把要扫描的一个列族增加到扫描器中
    	Table table =  HBaseJavaAPIUtils.connection.getTable(tableName);//获取操作表的Table对象   	
    	ResultScanner resultScanner = table.getScanner(scan);//获得用于检索数据的ResultScanner对象
    	
    	printScanResult(resultScanner);//输出扫描结果
	} 
    
	/**
	* 实现扫描整个表的方法,输入参数是表名,列族和列名
	* @parameter String  strTableName 表名, String columnFamily 列族名, String column 列名
	* @return void
	*/	
    public static void scanTable(String strTableName, String columnFamily, String column) throws IOException
	{	
		if(!HBaseJavaAPIUtils.isTableExist(strTableName)) //如果表不存在则直接返回
		{
			System.out.println("Table dose not exists");
			return;
		}
		
    	TableName tableName = TableName.valueOf(strTableName); //获取表名对象	
    	Scan scan = new Scan(); //创建扫描器对象   	
    	scan.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column));//把把要扫描的一个列族和列名增加到扫描器中
    	Table table =  HBaseJavaAPIUtils.connection.getTable(tableName);//获取操作表的Table对象   	
    	ResultScanner resultScanner = table.getScanner(scan);//获得用于检索数据的ResultScanner对象
    	
    	printScanResult(resultScanner);//输出扫描结果
	} 
    
	/**
	* 实现扫描整个表的方法,输入参数是表名和扫描的行键范围
	* @parameter String  strTableName 表名, String [] rowKeyRange 扫描的行键范围,包括开始行键和停止行键
	* @return void
	*/	
    public static void scanTable(String strTableName, String [] rowKeyRange) throws IOException
	{	
    	if(rowKeyRange.length !=2) 
    	{
    		System.out.println("Input rowkey range incorrect");
    		return;
    	}
    	
		if(!HBaseJavaAPIUtils.isTableExist(strTableName)) //如果表不存在则直接返回
		{
			System.out.println("Table dose not exists");
			return;
		}
		
    	TableName tableName = TableName.valueOf(strTableName); //获取表名对象	
    	Scan scan = new Scan(); //创建扫描器对象   	
    	scan.setStartRow(Bytes.toBytes(rowKeyRange[0]));//设置扫描的开始行键
    	scan.setStopRow(Bytes.toBytes(rowKeyRange[1]));//设置扫描的停止行键
    	Table table =  HBaseJavaAPIUtils.connection.getTable(tableName);//获取操作表的Table对象   	
    	ResultScanner resultScanner = table.getScanner(scan);//获得用于检索数据的ResultScanner对象
    	
    	printScanResult(resultScanner);//输出扫描结果
	} 
    
    /**
	* 实现扫描整个表的方法,输入参数是表名和扫描的时间戳范围
	* @parameter String  strTableName 表名, String [] timeRange 扫描的时间戳范围,包括开始时间戳和结束时间戳
	* @return void
	*/	
    public static void scanTable(String strTableName, long [] timeRange) throws IOException
	{	
    	if(timeRange.length !=2 || timeRange[0] > timeRange[1]) //开始时间戳大于结束时间戳
    	{
    		System.out.println("Input time range incorrect");
    		return;
    	}
    	
		if(!HBaseJavaAPIUtils.isTableExist(strTableName)) //如果表不存在则直接返回
		{
			System.out.println("Table dose not exists");
			return;
		}
		
    	TableName tableName = TableName.valueOf(strTableName); //获取表名对象	
    	Scan scan = new Scan(); //创建扫描器对象   	
    	scan.setTimeRange(timeRange[0], timeRange[1]);//设置扫描的时间戳范围
    	Table table =  HBaseJavaAPIUtils.connection.getTable(tableName);//获取操作表的Table对象   	
    	ResultScanner resultScanner = table.getScanner(scan);//获得用于检索数据的ResultScanner对象
    	
    	printScanResult(resultScanner);//输出扫描结果
	} 
    
	/**
	* 实现输出扫描结果的方法,输入参数是ResultScanner对象
	* @parameter ResultScanner resultScanner 结果扫描器对象
	* @return void 
	*/
	public static void printScanResult(ResultScanner resultScanner) throws IOException
	{
        for (Result result : resultScanner) 
        { 
//        	System.out.println(result);
        	Cell[] cells = result.rawCells();//返回单元格Cell的数组     	
        	printCellValue(cells);//输出单元格Cell数组的内容
        }
	}
	
	/**
	* 实现删除指定列的单元格值的方法,指定表名、行键、列族和列名
	* @parameter String strTableName 表名, String rowKey行键, String columnFamily 列族, String column 列
	* @return void 
	*/
    public static void deleteCellValue(String strTableName, String rowKey, String columnFamily, String column) throws IOException
	{
		if(!HBaseJavaAPIUtils.isTableExist(strTableName)) //如果表不存在则直接返回
		{
			System.out.println("Table dose not exists");
			return;
		}
    	
		TableName tableName = TableName.valueOf(strTableName); //获取表名对象
    	
		if(!HBaseJavaAPIUtils.isColumnFamilyExists(HBaseJavaAPIUtils.admin.getTableDescriptor(tableName), columnFamily)) //如果列族不存在则直接返回
		{
			System.out.println("Column Family does not exists");
			return;
		}
    	
		Table table =  HBaseJavaAPIUtils.connection.getTable(tableName);//获取操作表的Table对象
    	Delete delete = new Delete(Bytes.toBytes(rowKey));//创建一个Delete对象,指定要删除列的行键rowKey
    	delete.addColumns(Bytes.toBytes(columnFamily), Bytes.toBytes(column));//在Delete对象中增加要删除的列,指定列族和列名
    	table.delete(delete);//执行delete方法,删除该列
	}
    
	/**
	* 实现删除指定行的所有列的单元格值的方法,指定表名、行键
	* @parameter String strTableName 表名, String rowKey 行键
	* @return void 
	*/
    public static void deleteCellValue(String strTableName, String rowKey) throws IOException
	{
		if(!HBaseJavaAPIUtils.isTableExist(strTableName)) //如果表不存在则直接返回
		{
			System.out.println("Table dose not exists");
			return;
		}
    	
		TableName tableName = TableName.valueOf(strTableName); //获取表名对象   	

		Table table =  HBaseJavaAPIUtils.connection.getTable(tableName);//获取操作表的Table对象
    	Delete delete = new Delete(Bytes.toBytes(rowKey));//创建一个Delete对象,指定要删除列的行键rowKey
    	table.delete(delete);//执行delete方法,删除该列
	}
    
	/**
	* 实现删除指定列的单元格值的方法,指定表名、行键和时间戳
	* @parameter String strTableName 表名, String rowKey行键, long timeStamp 时间戳值
	* @return void 
	*/
    public static void deleteCellValue(String strTableName, String rowKey, long timeStamp) throws IOException
	{
		if(!HBaseJavaAPIUtils.isTableExist(strTableName)) //如果表不存在则直接返回
		{
			System.out.println("Table dose not exists");
			return;
		}
    	
		TableName tableName = TableName.valueOf(strTableName); //获取表名对象  	
		Table table =  HBaseJavaAPIUtils.connection.getTable(tableName);//获取操作表的Table对象
//    	Delete delete = new Delete(Bytes.toBytes(rowKey));//创建一个Delete对象,指定要删除列的行键rowKey
    	Delete delete = new Delete(Bytes.toBytes(rowKey), timeStamp);   //创建一个Delete对象,指定要删除列的行键rowKey
//    	delete.setTimestamp(timeStamp);//设置要删除指定列时间戳值的列
    	table.delete(delete);//执行delete方法,删除该列
	}
    
    /**
	* 实现删除指定列的单元格值的方法,指定表名、行键、列族、列名和时间戳
	* @parameter String strTableName 表名, String rowKey行键, String columnFamily 列族, String column 列,long timeStamp 时间戳值
	* @return void 
	*/
    public static void deleteCellValue(String strTableName, String rowKey, String columnFamily, String column, long timeStamp) throws IOException
	{
		if(!HBaseJavaAPIUtils.isTableExist(strTableName)) //如果表不存在则直接返回
		{
			System.out.println("Table dose not exists");
			return;
		}
    	
		TableName tableName = TableName.valueOf(strTableName); //获取表名对象
    	
		if(!HBaseJavaAPIUtils.isColumnFamilyExists(HBaseJavaAPIUtils.admin.getTableDescriptor(tableName), columnFamily)) //如果列族不存在则直接返回
		{
			System.out.println("Column Family does not exists");
			return;
		}
    	
		Table table =  HBaseJavaAPIUtils.connection.getTable(tableName);//获取操作表的Table对象
    	Delete delete = new Delete(Bytes.toBytes(rowKey));//创建一个Delete对象,指定要删除列的行键rowKey
    	delete.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), timeStamp);//在Delete对象中增加要删除的列,指定列族,列名和时间戳
    	table.delete(delete);//执行delete方法,删除该列
	}

}




8.部分实例

import java.io.IOException;
import java.util.Scanner;

public class hbaseshell {
    public static void main(String[] args) throws IOException {

        hbaseutils.init();//执行数据库操作之前,先 初始化,生成配置,建立连接
        String input = "";
        String[] param = {};
        
        while (true) {
            System.out.print("HbaseShell>");
            Scanner scan = new Scanner(System.in);
            input = scan.nextLine();//获取终端的下一行输入,nextLine()回车结束,不会以空格结束
            String tmp="";
            if(input.length()>0) {
            	tmp+=input.charAt(0);
            }
    		for(int i=1;i<input.length();i++) {//将各参数之间的多个空格去除,仅保留一个空格
    			if(input.charAt(i-1)==' '&&input.charAt(i)==' ') {
    				continue;
    			}
    			tmp+=input.charAt(i);
    		}
            input = tmp.trim();//去掉首尾空格字符
            param = input.split(" ");//对输入按空格分隔,返回字符串列表
          
            if (param.length < 1) {//没有任何输出则跳过
                continue;
            }
   
            switch (param[0]) {
            
                case "help":
                    doCmd(param, 1);
                    break;
                case "list":
                    doCmd(param, 1);
                    break;
                case "exists":
                    doCmd(param, 2);
                    break;
                case "desc":
                    doCmd(param, 2);
                    break;
                case "create":
                    doCmd(param, 3);
                    break;
                case "alter":
                    doCmd(param, 3);
                    break;
                case "delete":
                    doCmd(param, 2);
                    break;
                case "put":
                    doCmd(param, 6);
                    break;
                case "getr":
                    doCmd(param, 3);
                    break;
                case "getc":
                    doCmd(param, 5);
                    break;
                case "scant":
                    doCmd(param, 2);
                    break;
                case "scanf":
                    doCmd(param, 3);
                    break;
                case "deleter":
                    doCmd(param, 3);
                    break;
                case "deletec":
                    doCmd(param, 5);
                    break;
                case "quit":
                    hbaseutils.close();//执行数据库操作之后,关闭连接,释放资源
                    return;
                case "":
                    break;
                default:
                    System.out.println("invalid command");
            }
        }
    }

    //根据终端输入的不同参数,执行不同的命令
    static void doCmd(String[] param, int len) throws IOException {
        if (param.length == len && len == 1) {
            if (param[0].equals("help")) {
            	hbaseutils.helpinfo();//显示帮助信息
            }
            if (param[0].equals("list")) {
                hbaseutils.listTables();//列出默认名字空间default的所有表名
            }
        } else if (param.length == len && len == 2) {
            
            if (param[0].equals("exists")) {
                if (hbaseutils.isTableExist(param[1])) {
                    System.out.println("Table " + param[1] + " does exist");
                } else {
                    System.out.println("Table " + param[1] + " does not exist");
                }
            }
            if (param[0].equals("desc")) {
                hbaseutils.descTable(param[1]);
            }
            if (param[0].equals("scant")) {
                hbaseutils.scanTable(param[1]);
            }
            if(param[0].equals("delete")) {
            	hbaseutils.dropTable(param[1]);
            }
        } else if (param.length == len && len == 3) {
        	 if (param[0].equals("create")) {
        		 hbaseutils.createTable(param[1], param[2]);
        	 }
        	 if(param[0].equals("alter")) {
        		 hbaseutils.alterTable(param[1], param[2]);
        	 }
        	 if (param[0].equals("scanf")) {
                 hbaseutils.scanTable(param[1],param[2]);
             }
        	 if(param[0].equals("deleter")) {
             	hbaseutils.deleteCellValue(param[1], param[2]);
             }
        	 if(param[0].equals("getr")) {
             	hbaseutils.getCellValue(param[1], param[2]);
             }
        } else if (param.length == len && len == 4) {
        	
        } else if (param.length == len && len == 5) {
        	if(param[0].equals("getc")) {
            	hbaseutils.getCellValue(param[1], param[2], param[3], param[4]);
            }
        	if(param[0].equals("deletec")) {
            	hbaseutils.deleteCellValue(param[1], param[2], param[3], param[4]);
            }
        } else if (param.length == len && len == 6) {
        	if(param[0].equals("put")) {
            	hbaseutils.putACellValue(param[1], param[2], param[3], param[4], param[5]);
            }
        } else {
            System.out.println("invalid format");
        }
    }

    
}

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;

public class hbaseutils {
    public static Configuration configuration;
    public static Connection connection;
    public static Admin admin;

    public static void init() throws IOException  //执行数据库操作之前,把与创建configuration, Connection, Admin相关的代码统一放到静态的初始化方法init(){
        configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "master");

        connection = ConnectionFactory.createConnection(configuration);	//实质得到是Connection接口的间接实现类HConnectionImplementation的一个实例
        admin = connection.getAdmin();//实质得到是Admin接口的直接接实现类HBaseAdmin的一个实例
        System.out.println("hbase init success");
    }

    public static void close() throws IOException
    {
        connection.close();
        admin.close();
    }
    
    
    
    
    //help
    public static void helpinfo() {
        System.out.println("help:		help information");
        System.out.println("list:		list");
        System.out.println("exists:		exists t1");
        System.out.println("desc:		desc t1");
        System.out.println("create:		create t1 f1");
        System.out.println("alter:		alter t1 f1");
        System.out.println("delete:		delete t1");
        System.out.println("put:		put t1 r1 f1 q1 v1");
        System.out.println("getr:		getr t1 r1");
        System.out.println("getc:		getc t1 r1 f1 q1");
        System.out.println("scant:		scant t1");
        System.out.println("scanf:		scanf t1 f1");
        System.out.println("deleter:	deleter t1 r1");
        System.out.println("deletec:	deletec t1 r1 f1 q1");
    }

    
    
    
    
    
    
    //list
    public static void listTables() throws IOException//实现列出所有的表名
    {
        //通过TableName类获取表名
        TableName[] tableNames = admin.listTableNames();
        for(TableName tableName : tableNames)
        {
            System.out.println(new String(tableName.getName()));//把字节数组byte[]转换成String
        }
    }

    
    
    
    
    
    
    //exists
    public static boolean isTableExist(String strTableName) throws IOException//判断表是否存在
    {
        //处理不合法的情况,直接return
        if(!isTableNameFormatCorrect(strTableName))
        {
            return false;
        }

        TableName tableName  = TableName.valueOf(strTableName);
        return admin.tableExists(tableName);
    }
    public static boolean isTableNameFormatCorrect(String strTableName)
    {
//        System.out.println(strTableName.indexOf(":"));
//        System.out.println(strTableName.lastIndexOf(":"));
//        System.out.println(strTableName.length());
        //输入参数的合法性检查,提高方法代码的健壮性
        //包含不只1个冒号||第一个字符是冒号||最后一个字符是冒号
        if(strTableName.indexOf(":") != strTableName.lastIndexOf(":") || //第一个字符是冒号
                strTableName.indexOf(":")==0 || //最后一个字符是冒号
                strTableName.indexOf(":")==strTableName.length()-1)//包含不只1个冒号|
        {
            System.out.println("Input parameter format incorrect");
            return false;
        }
        return true;
    }
    
    
    
    
    
    
    
    
  //desc
    public static void descTable(String strTableName) throws IOException//描述指定表的属性信息
    {
        //处理不合法的情况,直接返回false
        if (!isTableNameFormatCorrect(strTableName)) {
            return;
        }
        if(!isTableExist(strTableName)) //表不允许被删除返回false
		{
			System.out.println("Table dose not exists");
			return ;
		}

        TableName tableName = TableName.valueOf(strTableName);//获取表名对象

        if (admin.isTableEnabled(tableName)) //判断表是否可用
        {
            System.out.println("Table " + strTableName + " is ENABLED");
        } else {
            System.out.println("Table " + strTableName + " is DISABLED");
        }
        HTableDescriptor tableDescriptor = admin.getTableDescriptor(tableName);
//        System.out.println(strTableName);
        HColumnDescriptor[] hcds = tableDescriptor.getColumnFamilies();
        System.out.println("COLUMN FAMILIES DESCRIPTION");
        for (HColumnDescriptor hcd : hcds) {
            System.out.println("{NAME => " + hcd.getNameAsString() + ", " +
                    "BLOOMFILTER => " + hcd.getBloomFilterType() + ", " +
                    "VERSIONS => " + hcd.getMaxVersions() + ", " +
                    "IN_MEMORY => " + hcd.isInMemory() + ", " +
                    "KEEP_DELETED_CELLS => " + hcd.getKeepDeletedCells().name() + ", " +
                    "DATA_BLOCK_ENCODING => " + hcd.getDataBlockEncoding().name() + ", " +
                    "TTL => " + hcd.getTimeToLive() + ", " +
                    "COMPRESSION => " + hcd.getCompression().getName() + ", " +
                    "MIN_VERSIONS => " + hcd.getMinVersions() + ", " +
                    "BLOCKCACHE => " + hcd.isBlockCacheEnabled() + ", " +
                    "BLOCKSIZE => " + hcd.getBlocksize() + ", " +
                    "REPLICATION_SCOPE => " + hcd.getDFSReplication() + "}"
            );//获取列族名称
        }
    }
    
    
    
    //create
    public static void createTable(String strTableName, String strColumnFamilyName) throws IOException
	{
    	if(!isTableNameFormatCorrect(strTableName)) //判断表是否允许创建
    	{
    		return;
    	}
    	if(isTableExist(strTableName)) //如果表存在则直接返回
		{
			
			System.out.println("Table " + strTableName + " does exist");
			return;
		}
    	
    	TableName tableName = TableName.valueOf(strTableName);
		HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
		HColumnDescriptor columnDescriptor = new HColumnDescriptor(strColumnFamilyName);
		tableDescriptor.addFamily(columnDescriptor);		
		admin.createTable(tableDescriptor);		
	}
    
    
    
    
    
    
    
    
    //alter
    public static void alterTable(String strTableName, String newColumnFamily) throws IOException
	{    
		if(!isTableExist(strTableName)) //表不存在则返回false
		{
			System.out.println("Table dose not exists");
			return;
		}
	
		TableName tableName = TableName.valueOf(strTableName);//获取表名对象	    	
		HTableDescriptor tableDescriptor = admin.getTableDescriptor(tableName);//获取表的描述器		
		
		if(isColumnFamilyExists(tableDescriptor, newColumnFamily)) //如果表的列族已存在,则直接返回
		{
			return;
		}
		
		HColumnDescriptor columnDescriptor = new HColumnDescriptor(newColumnFamily);//创建一个新的列族描述器			
		tableDescriptor.addFamily(columnDescriptor);//添加新列族描述器到表描述器		
		admin.modifyTable(tableName, tableDescriptor);	//提交修改表的操作
	 }
    
    public static boolean isColumnFamilyExists(HTableDescriptor tableDescriptor, String strFamiliyName) throws IOException//实现列出所有的表名
	{
		HColumnDescriptor[] hColumnDescriptors = tableDescriptor.getColumnFamilies();
		for(HColumnDescriptor hColumnDescriptor: hColumnDescriptors) 
		{
			if(hColumnDescriptor.getNameAsString().equals(strFamiliyName))
			{
				System.out.println("Column Family already exists");
				return true;
			}
		}
		return false;	
	}
    
    
    
    //delete
    public static void dropTable(String strTableName) throws IOException
   	{
   		if(!isTableAllowDrop(strTableName)) //返回false,表不允许删除
   		{
   			System.out.println("Table dose not allow to be dropped");
   			return;
   		}  	
   		TableName tableName = TableName.valueOf(strTableName);
   		admin.deleteTable(tableName);
   		System.out.println("Table deleted successfully");
   	} 
    public static boolean isTableAllowDrop(String strTableName) throws IOException//判断表是否存在
	{	
		if(!isTableExist(strTableName)) //表不允许被删除返回false
		{
			System.out.println("Table dose not exists");
			return false;
		}	
		TableName tableName = TableName.valueOf(strTableName);
		if(admin.isTableEnabled(tableName)) //判断表是否被禁用,如果是enabled,返回true
		{
			admin.disableTable(tableName);//删除前先将表禁用 ,使其off-line
		}
		return true;
	}
    
    public static void scanTable(String strTableName) throws IOException
   	{	
   		if(!isTableExist(strTableName)) //如果表不存在则直接返回
   		{
   			System.out.println("Table dose not exists");
   			return;
   		}
   		
       	TableName tableName = TableName.valueOf(strTableName); //获取表名对象	
       	Scan scan = new Scan(); //创建扫描器对象   	
       	Table table =  connection.getTable(tableName);//获取操作表的Table对象   	
       	ResultScanner resultScanner = table.getScanner(scan);//获得用于检索数据的ResultScanner对象
       	
       	printScanResult(resultScanner);//输出扫描结果
   	} 
    public static void printScanResult(ResultScanner resultScanner) throws IOException
	{
        for (Result result : resultScanner) 
        { 
//        	System.out.println(result);
        	Cell[] cells = result.rawCells();//返回单元格Cell的数组     	
        	printCellValue(cells);//输出单元格Cell数组的内容
        }
	}
    public static void printCellValue(Cell[] cells) throws IOException//实现列出所有的表名
	{
        for (Cell cell : cells) 
        { 
            System.out.println("rowkey:" + Bytes.toString(CellUtil.cloneRow(cell)) + 
            		         " | column family:" + Bytes.toString(CellUtil.cloneFamily(cell)) + 
            		         " | column:" + Bytes.toString(CellUtil.cloneQualifier(cell)) + 
            		         " | timestamp:" + cell.getTimestamp() +
            		         " | cell value:" + Bytes.toString(CellUtil.cloneValue(cell)));
        }
	}
    
    
    
    //put
    public static void putACellValue(String strTableName, String rowKey, String columnFamily, String column, String value) throws IOException
	{
		if(!isTableExist(strTableName)) //如果表不存在则直接返回
		{
			System.out.println("Table dose not exists");
			return;
		}
    	
		TableName tableName = TableName.valueOf(strTableName);//获取表名对象 		
		if(!isColumnFamilyExists(admin.getTableDescriptor(tableName), columnFamily)) //如果列族不存在则直接返回
		{
			System.out.println("Column Family does not exists");
			return;
		}
    	
    	Table table = connection.getTable(tableName);//获取操作表的Table对象
    	Put put = new Put(Bytes.toBytes(rowKey));//创建一个Put对象,指定行键rowKey
    	put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));//增加一列,指定列族、列修饰符、单元格值
    	table.put(put);	//执行put方法,插入一列
	}
    
    
    
    
    
    
    //getr
    public static void getCellValue(String strTableName, String rowKey) throws IOException
	{	
		if(!isTableExist(strTableName)) //如果表不存在则直接返回
		{
			System.out.println("Table dose not exists");
			return;
		}
    	
    	TableName tableName = TableName.valueOf(strTableName); //获取表名对象
    	Table table = connection.getTable(tableName);//获取操作表的Table对象    	
    	Get get = new Get(Bytes.toBytes(rowKey));//创建一个Get对象,指定行键rowKey 	
    	Result result = table.get(get);//执行get方法,获得指定列的指,返回结果
    	Cell[] cells = result.rawCells();//获取单元格对象的数组
    	
    	printCellValue(cells);//输出单元格数组的内容
	}
    
    //getc
    public static void getCellValue(String strTableName, String rowKey, String columnFamily, String column) throws IOException
	{	
		if(!isTableExist(strTableName)) //如果表不存在则直接返回
		{
			System.out.println("Table dose not exists");
			return;
		}
  	
    	TableName tableName = TableName.valueOf(strTableName); //获取表名对象		
    	if(!isColumnFamilyExists(admin.getTableDescriptor(tableName), columnFamily)) //如果列族不存在则直接返回
		{
			System.out.println("Column Family does not exists");
			return;
		}
    	
    	Table table = connection.getTable(tableName);//获取操作表的Table对象    	
    	Get get = new Get(Bytes.toBytes(rowKey));//创建一个Get对象,指定行键rowKey
    	get.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column));//加入列族名和列名作为查询条件
    	Result result = table.get(get);//执行get方法,获得指定列的指,返回结果
    	Cell[] cells = result.rawCells();//获取单元格对象的数组
    	
    	printCellValue(cells);//输出单元格数组的内容
	}
   
    
    
    //scant
//    public static void scanTable(String strTableName) throws IOException
//   	{	
//   		if(!isTableExist(strTableName)) //如果表不存在则直接返回
//   		{
//   			System.out.println("Table dose not exists");
//   			return;
//   		}
//   		
//       	TableName tableName = TableName.valueOf(strTableName); //获取表名对象	
//       	Scan scan = new Scan(); //创建扫描器对象   	
//       	Table table =  HBaseJavaAPIUtils.connection.getTable(tableName);//获取操作表的Table对象   	
//       	ResultScanner resultScanner = table.getScanner(scan);//获得用于检索数据的ResultScanner对象
//       	
//       	printScanResult(resultScanner);//输出扫描结果
//   	} 
    
  //scanf
    public static void scanTable(String strTableName, String columnFamily) throws IOException
	{	
		if(!isTableExist(strTableName)) //如果表不存在则直接返回
		{
			System.out.println("Table dose not exists");
			return;
		}
		
    	TableName tableName = TableName.valueOf(strTableName); //获取表名对象	
    	if(!isColumnFamilyExists(admin.getTableDescriptor(tableName), columnFamily)) //如果列族不存在则直接返回
		{
			System.out.println("Column Family does not exists");
			return;
		}
    	Scan scan = new Scan(); //创建扫描器对象   	
    	scan.addFamily(Bytes.toBytes(columnFamily));//把要扫描的一个列族增加到扫描器中
    	Table table = connection.getTable(tableName);//获取操作表的Table对象   	
    	ResultScanner resultScanner = table.getScanner(scan);//获得用于检索数据的ResultScanner对象
    	
    	printScanResult(resultScanner);//输出扫描结果
	} 
   
    
    
    
    
    
    //deleter
    public static void deleteCellValue(String strTableName, String rowKey) throws IOException
	{
		if(!isTableExist(strTableName)) //如果表不存在则直接返回
		{
			System.out.println("Table dose not exists");
			return;
		}
    	
		TableName tableName = TableName.valueOf(strTableName); //获取表名对象   	

		Table table = connection.getTable(tableName);//获取操作表的Table对象
    	Delete delete = new Delete(Bytes.toBytes(rowKey));//创建一个Delete对象,指定要删除列的行键rowKey
    	table.delete(delete);//执行delete方法,删除该列
	}
    
    
    
    
    
    
    
   //deletec
    public static void deleteCellValue(String strTableName, String rowKey, String columnFamily, String column) throws IOException
	{
		if(!isTableExist(strTableName)) //如果表不存在则直接返回
		{
			System.out.println("Table dose not exists");
			return;
		}
    	
		TableName tableName = TableName.valueOf(strTableName); //获取表名对象
    	
		if(!isColumnFamilyExists(admin.getTableDescriptor(tableName), columnFamily)) //如果列族不存在则直接返回
		{
			System.out.println("Column Family does not exists");
			return;
		}
    	
		Table table =connection.getTable(tableName);//获取操作表的Table对象
    	Delete delete = new Delete(Bytes.toBytes(rowKey));//创建一个Delete对象,指定要删除列的行键rowKey
    	delete.addColumns(Bytes.toBytes(columnFamily), Bytes.toBytes(column));//在Delete对象中增加要删除的列,指定列族和列名
    	table.delete(delete);//执行delete方法,删除该列
	}
 
}

9.运行测试

help
list
exists xtt
disable xtt
create xtt x
delete xtt
desc xtt
alter xtt x2
put xtt 001 x 635 sl
getr xtt 001
getc xtt 001 x 635
scant xtt
scanf xtt x
deleter xtt 001
deletec xtt 001 x 635
listNameSpaces
listTables hbase
createNameSpace xtt

10.成品

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;

public class hbaseutils {
    public static Configuration configuration;
    public static Connection connection;
    public static Admin admin;

    public static void init() throws IOException  //执行数据库操作之前,把与创建configuration, Connection, Admin相关的代码统一放到静态的初始化方法init(){
        configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "master");
        connection = ConnectionFactory.createConnection(configuration);	//实质得到是Connection接口的间接实现类HConnectionImplementation的一个实例
        admin = connection.getAdmin();//实质得到是Admin接口的直接接实现类HBaseAdmin的一个实例
        System.out.println("Hbase Init Success");
    }

    public static void close() throws IOException
    {
        connection.close();
        admin.close();
    }
    
    
    
    
    //help
    public static void helpinfo() {
    	System.out.println("command"+"\t\t\t"+"sample code");
        System.out.println("list:"+"\t\t\t"+"list");
        System.out.println("exists:"+"\t\t\t"+"exists t1");
        System.out.println("disable:"+"\t\t"+"disable t1");
        System.out.println("desc:"+"\t\t\t"+"desc t1");
        System.out.println("create:"+"\t\t\t"+"create t1 f1");
        System.out.println("alter:"+"\t\t\t"+"alter t1 f1");
        System.out.println("delete:"+"\t\t\t"+"delete t1");
        System.out.println("put:"+"\t\t\t"+"put t1 r1 f1 q1 v1");
        System.out.println("getr:"+"\t\t\t"+"getr t1 r1");
        System.out.println("getc:"+"\t\t\t"+"getc t1 r1 f1 q1");
        System.out.println("scant:"+"\t\t\t"+"scant t1");
        System.out.println("scanf:"+"\t\t\t"+"scanf t1 f1");
        System.out.println("deleter:"+"\t\t"+"deleter t1 r1");
        System.out.println("deletec:"+"\t\t"+"deletec t1 r1 f1 q1");
        System.out.println("listNameSpaces:"+"\t\t"+"listNameSpaces");
        System.out.println("listTables:"+"\t\t"+"listTables namespace");
        System.out.println("createNameSpace:"+"\t"+"createNameSpace namespace");
        System.out.println("help:"+"\t\t\t"+"help");
    }

    
    
    
    
    
    
    //list
    public static void listTables() throws IOException//实现列出所有的表名
    {
        //通过TableName类获取表名
        TableName[] tableNames = admin.listTableNames();
        for(TableName tableName : tableNames)
        {
            System.out.println(new String(tableName.getName()));//把字节数组byte[]转换成String
        }
    }

    
    
    
    
    
    
    //exists
    public static boolean isTableExist(String strTableName) throws IOException//判断表是否存在
    {
        //处理不合法的情况,直接return
        if(!isTableNameFormatCorrect(strTableName))
        {
            return false;
        }

        TableName tableName  = TableName.valueOf(strTableName);
        return admin.tableExists(tableName);
    }
    public static boolean isTableNameFormatCorrect(String strTableName)
    {
//        System.out.println(strTableName.indexOf(":"));
//        System.out.println(strTableName.lastIndexOf(":"));
//        System.out.println(strTableName.length());
        //输入参数的合法性检查,提高方法代码的健壮性
        //包含不只1个冒号||第一个字符是冒号||最后一个字符是冒号
        if(strTableName.indexOf(":") != strTableName.lastIndexOf(":") || //第一个字符是冒号
                strTableName.indexOf(":")==0 || //最后一个字符是冒号
                strTableName.indexOf(":")==strTableName.length()-1)//包含不只1个冒号|
        {
            System.out.println("Input parameter format incorrect");
            return false;
        }
        return true;
    }
    
    
    
    
    
    
    
    
  //desc
    public static void descTable(String strTableName) throws IOException//描述指定表的属性信息
    {
        //处理不合法的情况,直接返回false
        if (!isTableNameFormatCorrect(strTableName)) {
            return;
        }
        if(!isTableExist(strTableName)) //表不允许被删除返回false
		{
			System.out.println("Table dose not exists");
			return ;
		}

        TableName tableName = TableName.valueOf(strTableName);//获取表名对象

        if (admin.isTableEnabled(tableName)) //判断表是否可用
        {
            System.out.println("Table " + strTableName + " is ENABLED");
        } else {
            System.out.println("Table " + strTableName + " is DISABLED");
        }
        HTableDescriptor tableDescriptor = admin.getTableDescriptor(tableName);
//        System.out.println(strTableName);
        HColumnDescriptor[] hcds = tableDescriptor.getColumnFamilies();
        System.out.println("COLUMN FAMILIES DESCRIPTION");
        for (HColumnDescriptor hcd : hcds) {
            System.out.println("{NAME => " + hcd.getNameAsString() + ", " +
                    "BLOOMFILTER => " + hcd.getBloomFilterType() + ", " +
                    "VERSIONS => " + hcd.getMaxVersions() + ", " +
                    "IN_MEMORY => " + hcd.isInMemory() + ", " +
                    "KEEP_DELETED_CELLS => " + hcd.getKeepDeletedCells() + ", " +
                    "DATA_BLOCK_ENCODING => " + hcd.getDataBlockEncoding().name() + ", " +
                    "TTL => " + hcd.getTimeToLive() + ", " +
                    "COMPRESSION => " + hcd.getCompression().getName() + ", " +
                    "MIN_VERSIONS => " + hcd.getMinVersions() + ", " +
                    "BLOCKCACHE => " + hcd.isBlockCacheEnabled() + ", " +
                    "BLOCKSIZE => " + hcd.getBlocksize() + ", " +
                    "REPLICATION_SCOPE => " + hcd.getDFSReplication() + "}"
            );//获取列族名称
        }
    }
    
    
    
    //create
    public static void createTable(String strTableName, String strColumnFamilyName) throws IOException
	{
    	if(!isTableNameFormatCorrect(strTableName)) //判断表是否允许创建
    	{
    		return;
    	}
    	if(isTableExist(strTableName)) //如果表存在则直接返回
		{
			
			System.out.println("Table " + strTableName + " does exist");
			return;
		}
    	
    	TableName tableName = TableName.valueOf(strTableName);
		HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
		HColumnDescriptor columnDescriptor = new HColumnDescriptor(strColumnFamilyName);
		tableDescriptor.addFamily(columnDescriptor);		
		admin.createTable(tableDescriptor);		
	}
    
    
    
    
    
    
    
    
    //alter
    public static void alterTable(String strTableName, String newColumnFamily) throws IOException
	{    
		if(!isTableExist(strTableName)) //表不存在则返回false
		{
			System.out.println("Table dose not exists");
			return;
		}
	
		TableName tableName = TableName.valueOf(strTableName);//获取表名对象	    	
		HTableDescriptor tableDescriptor = admin.getTableDescriptor(tableName);//获取表的描述器		
		
		if(isColumnFamilyExists(tableDescriptor, newColumnFamily)) //如果表的列族已存在,则直接返回
		{
			return;
		}
		
		HColumnDescriptor columnDescriptor = new HColumnDescriptor(newColumnFamily);//创建一个新的列族描述器			
		tableDescriptor.addFamily(columnDescriptor);//添加新列族描述器到表描述器		
		admin.modifyTable(tableName, tableDescriptor);	//提交修改表的操作
	 }
    
    public static boolean isColumnFamilyExists(HTableDescriptor tableDescriptor, String strFamiliyName) throws IOException//实现列出所有的表名
	{
		HColumnDescriptor[] hColumnDescriptors = tableDescriptor.getColumnFamilies();
		for(HColumnDescriptor hColumnDescriptor: hColumnDescriptors) 
		{
			if(hColumnDescriptor.getNameAsString().equals(strFamiliyName))
			{
//				System.out.println("Column Family already exists");
				return true;
			}
		}
		return false;	
	}
    
    
    
    //delete
    public static void dropTable(String strTableName) throws IOException
   	{
   		if(!isTableAllowDrop(strTableName)) //返回false,表不允许删除
   		{
   			System.out.println("Table dose not allow to be dropped");
   			return;
   		}  	
   		TableName tableName = TableName.valueOf(strTableName);
   		admin.deleteTable(tableName);
   		System.out.println("Table deleted successfully");
   	} 
    public static boolean isTableAllowDrop(String strTableName) throws IOException//判断表是否存在
	{	
		if(!isTableExist(strTableName)) //表不允许被删除返回false
		{
			System.out.println("Table dose not exists");
			return false;
		}	
		TableName tableName = TableName.valueOf(strTableName);
		if(admin.isTableEnabled(tableName)) //判断表是否被禁用,如果是enabled,返回true
		{
			admin.disableTable(tableName);//删除前先将表禁用 ,使其off-line
		}
		return true;
	}
    
    public static void scanTable(String strTableName) throws IOException
   	{	
   		if(!isTableExist(strTableName)) //如果表不存在则直接返回
   		{
   			System.out.println("Table dose not exists");
   			return;
   		}
   		
       	TableName tableName = TableName.valueOf(strTableName); //获取表名对象	
       	Scan scan = new Scan(); //创建扫描器对象   	
       	Table table =  connection.getTable(tableName);//获取操作表的Table对象   	
       	ResultScanner resultScanner = table.getScanner(scan);//获得用于检索数据的ResultScanner对象
       	
       	printScanResult(resultScanner);//输出扫描结果
   	} 
    public static void printScanResult(ResultScanner resultScanner) throws IOException
	{
        for (Result result : resultScanner) 
        { 
//        	System.out.println(result);
        	Cell[] cells = result.rawCells();//返回单元格Cell的数组     	
        	printCellValue(cells);//输出单元格Cell数组的内容
        }
	}
    public static void printCellValue(Cell[] cells) throws IOException//实现列出所有的表名
	{
        for (Cell cell : cells) 
        { 
            System.out.println("rowkey:" + Bytes.toString(CellUtil.cloneRow(cell)) + 
            		         " | column family:" + Bytes.toString(CellUtil.cloneFamily(cell)) + 
            		         " | column:" + Bytes.toString(CellUtil.cloneQualifier(cell)) + 
            		         " | timestamp:" + cell.getTimestamp() +
            		         " | cell value:" + Bytes.toString(CellUtil.cloneValue(cell)));
        }
	}
    
    
    
    //put
    public static void putACellValue(String strTableName, String rowKey, String columnFamily, String column, String value) throws IOException
	{
		if(!isTableExist(strTableName)) //如果表不存在则直接返回
		{
			System.out.println("Table dose not exists");
			return;
		}
    	
		TableName tableName = TableName.valueOf(strTableName);//获取表名对象 		
		if(!isColumnFamilyExists(admin.getTableDescriptor(tableName), columnFamily)) //如果列族不存在则直接返回
		{
			System.out.println("Column Family does not exists");
			return;
		}
    	
    	Table table = connection.getTable(tableName);//获取操作表的Table对象
    	Put put = new Put(Bytes.toBytes(rowKey));//创建一个Put对象,指定行键rowKey
    	put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column), Bytes.toBytes(value));//增加一列,指定列族、列修饰符、单元格值
    	table.put(put);	//执行put方法,插入一列
	}
    
    
    
    
    
    
    //getr
    public static void getCellValue(String strTableName, String rowKey) throws IOException
	{	
		if(!isTableExist(strTableName)) //如果表不存在则直接返回
		{
			System.out.println("Table dose not exists");
			return;
		}
    	
    	TableName tableName = TableName.valueOf(strTableName); //获取表名对象
    	Table table = connection.getTable(tableName);//获取操作表的Table对象    	
    	Get get = new Get(Bytes.toBytes(rowKey));//创建一个Get对象,指定行键rowKey 	
    	Result result = table.get(get);//执行get方法,获得指定列的指,返回结果
    	Cell[] cells = result.rawCells();//获取单元格对象的数组
    	
    	printCellValue(cells);//输出单元格数组的内容
	}
    
    //getc
    public static void getCellValue(String strTableName, String rowKey, String columnFamily, String column) throws IOException
	{	
		if(!isTableExist(strTableName)) //如果表不存在则直接返回
		{
			System.out.println("Table dose not exists");
			return;
		}
  	
    	TableName tableName = TableName.valueOf(strTableName); //获取表名对象		
    	if(!isColumnFamilyExists(admin.getTableDescriptor(tableName), columnFamily)) //如果列族不存在则直接返回
		{
			System.out.println("Column Family does not exists");
			return;
		}
    	
    	Table table = connection.getTable(tableName);//获取操作表的Table对象    	
    	Get get = new Get(Bytes.toBytes(rowKey));//创建一个Get对象,指定行键rowKey
    	get.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column));//加入列族名和列名作为查询条件
    	Result result = table.get(get);//执行get方法,获得指定列的指,返回结果
    	Cell[] cells = result.rawCells();//获取单元格对象的数组
    	
    	printCellValue(cells);//输出单元格数组的内容
	}
   
    
    
    //scant
//    public static void scanTable(String strTableName) throws IOException
//   	{	
//   		if(!isTableExist(strTableName)) //如果表不存在则直接返回
//   		{
//   			System.out.println("Table dose not exists");
//   			return;
//   		}
//   		
//       	TableName tableName = TableName.valueOf(strTableName); //获取表名对象	
//       	Scan scan = new Scan(); //创建扫描器对象   	
//       	Table table =  HBaseJavaAPIUtils.connection.getTable(tableName);//获取操作表的Table对象   	
//       	ResultScanner resultScanner = table.getScanner(scan);//获得用于检索数据的ResultScanner对象
//       	
//       	printScanResult(resultScanner);//输出扫描结果
//   	} 
    
  //scanf
    public static void scanTable(String strTableName, String columnFamily) throws IOException
	{	
		if(!isTableExist(strTableName)) //如果表不存在则直接返回
		{
			System.out.println("Table dose not exists");
			return;
		}
		
    	TableName tableName = TableName.valueOf(strTableName); //获取表名对象	
    	if(!isColumnFamilyExists(admin.getTableDescriptor(tableName), columnFamily)) //如果列族不存在则直接返回
		{
			System.out.println("Column Family does not exists");
			return;
		}
    	Scan scan = new Scan(); //创建扫描器对象   	
    	scan.addFamily(Bytes.toBytes(columnFamily));//把要扫描的一个列族增加到扫描器中
    	Table table = connection.getTable(tableName);//获取操作表的Table对象   	
    	ResultScanner resultScanner = table.getScanner(scan);//获得用于检索数据的ResultScanner对象
    	
    	printScanResult(resultScanner);//输出扫描结果
	} 
   
    
    
    
    
    
    //deleter
    public static void deleteCellValue(String strTableName, String rowKey) throws IOException
	{
		if(!isTableExist(strTableName)) //如果表不存在则直接返回
		{
			System.out.println("Table dose not exists");
			return;
		}
    	
		TableName tableName = TableName.valueOf(strTableName); //获取表名对象   	

		Table table = connection.getTable(tableName);//获取操作表的Table对象
    	Delete delete = new Delete(Bytes.toBytes(rowKey));//创建一个Delete对象,指定要删除列的行键rowKey
    	table.delete(delete);//执行delete方法,删除该列
	}
    
    
    
    
    
    
    
   //deletec
    public static void deleteCellValue(String strTableName, String rowKey, String columnFamily, String column) throws IOException
	{
		if(!isTableExist(strTableName)) //如果表不存在则直接返回
		{
			System.out.println("Table dose not exists");
			return;
		}
    	
		TableName tableName = TableName.valueOf(strTableName); //获取表名对象
    	
		if(!isColumnFamilyExists(admin.getTableDescriptor(tableName), columnFamily)) //如果列族不存在则直接返回
		{
			System.out.println("Column Family does not exists");
			return;
		}
    	
		Table table =connection.getTable(tableName);//获取操作表的Table对象
    	Delete delete = new Delete(Bytes.toBytes(rowKey));//创建一个Delete对象,指定要删除列的行键rowKey
    	delete.addColumns(Bytes.toBytes(columnFamily), Bytes.toBytes(column));//在Delete对象中增加要删除的列,指定列族和列名
    	table.delete(delete);//执行delete方法,删除该列
	}
    
   //listNameSpaces
    public static void listNameSpaces() throws IOException//实现列出所有的表名
	{
		NamespaceDescriptor[] namespaceDescriptors = admin.listNamespaceDescriptors();
		for(NamespaceDescriptor namespaceDescriptor: namespaceDescriptors) 
		{
			System.out.println(namespaceDescriptor.getName());//获取名字空间名称的字符串
		}	
	}
  
    
    //查看指定名字空间的表
    public static void listTables(String strNameSpace) throws IOException//实现列出所有的表名
	{	
		//检查名字空间是否存在
		if(!isNameSpacesExists(strNameSpace)) //不存在则直接返回
		{
			return;
		}
				
		TableName[] tableNames = admin.listTableNamesByNamespace(strNameSpace);		
		listTables(tableNames);
	}
    public static boolean isNameSpacesExists(String strNameSpace) throws IOException//实现列出所有的表名
	{
		NamespaceDescriptor[] namespaceDescriptors = admin.listNamespaceDescriptors();
		for(NamespaceDescriptor namespaceDescriptor: namespaceDescriptors) 
		{
			if(namespaceDescriptor.getName().equals(strNameSpace))//判断名字空间名称的字符串是否相等
			{
				return true;
			}
		}
//		System.out.println("NameSpace does not exists");
		return false;
	}
    public static void listTables(TableName[] tableNames) throws IOException//实现列出所有的表名
	{	
		 for(TableName tableName : tableNames)
		 {	
			 System.out.println(tableName.getNameAsString());
		 }
	}	
    
    //createNameSpace
    public static void createNameSpace(String strNameSpace) throws IOException//实现列出所有的表名
	{
		if(isNameSpacesExists(strNameSpace))//如果名字空间存在
		{
			System.out.println("NameSpace already exists");
			return;
		}
		
//		NamespaceDescriptor namespaceDescriptor = new NamespaceDescriptor();//构造方法是私有的,不能直接调用
		NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create(strNameSpace).build();
		admin.createNamespace(namespaceDescriptor);
	}

	public static void disable(String strTableName) throws IOException {
		if(!isTableExist(strTableName)) //如果表不存在则直接返回
		{
			System.out.println("Table dose not exists");
			return;
		}
		TableName tableName = TableName.valueOf(strTableName);
		
		if(admin.isTableEnabled(tableName)) //判断表是否被禁用,如果是enabled,返回true
		{
			admin.disableTable(tableName);//删除前先将表禁用 ,使其off-line
		}
		return ;
		
	}
    
}

import java.io.IOException;
import java.util.Scanner;

public class hbaseshell {
    public static void main(String[] args) throws IOException {

        hbaseutils.init();//执行数据库操作之前,先 初始化,生成配置,建立连接
        String input = "";
        String[] param = {};
        
        while (true) {
            System.out.print("HbaseShell>");
            Scanner scan = new Scanner(System.in);
            input = scan.nextLine();//获取终端的下一行输入,nextLine()回车结束,不会以空格结束
            String tmp="";
            if(input.length()>0) {
            	tmp+=input.charAt(0);
            }
            
          //将各参数之间的多个空格去除,仅保留一个空格
    		for(int i=1;i<input.length();i++) {
    			if(input.charAt(i-1)==' '&&input.charAt(i)==' ') {
    				continue;
    			}
    			tmp+=input.charAt(i);
    		}
    		
            input = tmp.trim();//去掉首尾空格字符
            param = input.split(" ");//对输入按空格分隔,返回字符串列表
          
            if (param.length < 1) {//没有任何输出则跳过
                continue;
            }
   
            switch (param[0]) {
            
                case "help":
                    doCmd(param, 1);
                    break;
                case "list":
                    doCmd(param, 1);
                    break;
                case "exists":
                    doCmd(param, 2);
                    break;
                case "desc":
                    doCmd(param, 2);
                    break;
                case "create":
                    doCmd(param, 3);
                    break;
                case "alter":
                    doCmd(param, 3);
                    break;
                case "delete":
                    doCmd(param, 2);
                    break;
                case "put":
                    doCmd(param, 6);
                    break;
                case "getr":
                    doCmd(param, 3);
                    break;
                case "getc":
                    doCmd(param, 5);
                    break;
                case "scant":
                    doCmd(param, 2);
                    break;
                case "scanf":
                    doCmd(param, 3);
                    break;
                case "deleter":
                    doCmd(param, 3);
                    break;
                case "deletec":
                    doCmd(param, 5);
                    break;
                case "listTables":
                    doCmd(param,2);
                    break;
                case "disable":
                    doCmd(param,2);
                    break;    
                case "listNameSpaces":
                    doCmd(param, 1);
                    break;
                case "createNameSpace":
                    doCmd(param, 2);
                    break;
                case "quit":
                    hbaseutils.close();//执行数据库操作之后,关闭连接,释放资源
                    return;
                case "":
                    break;
                default:
                    System.out.println("invalid command");
            }
        }
    }

    //根据终端输入的不同参数,执行不同的命令
    static void doCmd(String[] param, int len) throws IOException {
        if (param.length == len && len == 1) {
            if (param[0].equals("help")) {
            	hbaseutils.helpinfo();//显示帮助信息
            }
            if (param[0].equals("listNameSpaces")) {
            	hbaseutils.listNameSpaces();//显示帮助信息
            }
            if (param[0].equals("list")) {
                hbaseutils.listTables();//列出默认名字空间default的所有表名
            }
        } else if (param.length == len && len == 2) {
            
            if (param[0].equals("exists")) {
                if (hbaseutils.isTableExist(param[1])) {
                    System.out.println("Table " + param[1] + " does exist");
                } else {
                    System.out.println("Table " + param[1] + " does not exist");
                }
            }
            if (param[0].equals("desc")) {
                hbaseutils.descTable(param[1]);
            }
            if (param[0].equals("listTables")) {
                hbaseutils.listTables(param[1]);
            }
            if (param[0].equals("disable")) {
                hbaseutils.disable(param[1]);
            }
            if (param[0].equals("createNameSpace")) {
                hbaseutils.createNameSpace(param[1]);
            }
            if (param[0].equals("scant")) {
                hbaseutils.scanTable(param[1]);
            }
            if(param[0].equals("delete")) {
            	hbaseutils.dropTable(param[1]);
            }
        } else if (param.length == len && len == 3) {
        	 if (param[0].equals("create")) {
        		 hbaseutils.createTable(param[1], param[2]);
        	 }
        	 if(param[0].equals("alter")) {
        		 hbaseutils.alterTable(param[1], param[2]);
        	 }
        	 if (param[0].equals("scanf")) {
                 hbaseutils.scanTable(param[1],param[2]);
             }
        	 if(param[0].equals("deleter")) {
             	hbaseutils.deleteCellValue(param[1], param[2]);
             }
        	 if(param[0].equals("getr")) {
             	hbaseutils.getCellValue(param[1], param[2]);
             }
        } else if (param.length == len && len == 4) {
        	
        } else if (param.length == len && len == 5) {
        	if(param[0].equals("getc")) {
            	hbaseutils.getCellValue(param[1], param[2], param[3], param[4]);
            }
        	if(param[0].equals("deletec")) {
            	hbaseutils.deleteCellValue(param[1], param[2], param[3], param[4]);
            }
        } else if (param.length == len && len == 6) {
        	if(param[0].equals("put")) {
            	hbaseutils.putACellValue(param[1], param[2], param[3], param[4], param[5]);
            }
        } else {
            System.out.println("invalid format");
        }
    }

    
}

在这里插入图片描述
写的时候感觉挺拉的,用的时候其实还可以,比原有的shell要方便

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值