实验:熟悉常用的HBase操作
1实验目的
- 理解HBase在Hadoop体系结构中的角色;
- 熟练使用HBase操作常用的Shell命令;
2 实验平台
操作系统:Linux
Hadoop版本:3.1.3
HBase版本:2.2.2
JDK版本:1.8
3 实验内容和要求
1.用Hadoop提供的HBase Shell命令实现以下指定功能:
准备工作:
cd /usr/local/hadoop
./sbin/start-dfs.sh
cd /usr/local/hbase
./bin/start-hbase.sh
进入shell:
hbase shell
(1)列出HBase所有的表的相关信息,例如表名;
Shell
命令
list
java代码
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException;
public class test1{
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
public static void init(){
configuration = HBaseConfiguration.create();
configuration.set("Hbase.rootdir","hdfs://localhost:9000/hbase");
try{
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
}catch (IOException e){
e.printStackTrace();
}
}
public static void close(){
try {
if(admin != null) {
admin.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
public static void getData()throws IOException{
TableName Htable[] = admin.listTableNames();
for (TableName name:Htable){
System.out.println(name.getNameAsString());
}
}
public static void main(String[] args)throws IOException{
init();
getData();
close();
}
}
(2)在终端打印出指定的表的所有记录数据;
scan 'Student'
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 test2 {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
public static void init(){
configuration = HBaseConfiguration.create();
configuration.set("Hbase.rootdir","hdfs://localhost:9000/hbase");
try{
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
}catch (IOException e){
e.printStackTrace();
}
}
public static void close(){
try {
if(admin != null) {
admin.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
public static void printTableData(String tableName) throws IOException{
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
scan.getAllowPartialResults();
ResultScanner resultScanner = table.getScanner(scan);
for(Result result:resultScanner){
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.print("行键:" + Bytes.toString(CellUtil.cloneRow(cell)));
System.out.print("\t列族" + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.print("\t列:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("\t值:" + Bytes.toString(CellUtil.cloneValue(cell)));
System.out.println("\t时间戳:" + cell.getTimestamp());
}
}
table.close();
}
public static void main(String[] args)throws IOException{
init();
printTableData("student");
close();
}
}
(3)向已经创建好的表添加和删除指定的列族或列;
请先在
Shell
中创建表
s1
,作为示例表,命令如下:
create 's1','score'
然后,可以在
s1
中添加数据,命令如下:
put 's1','zhangsan','score:Math','69'
之后,可以执行如下命令删除指定的列:
delete 's1','zhangsan','score:Math'
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 test3{
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
public static void init(){
configuration = HBaseConfiguration.create();
configuration.set("Hbase.rootdir","hdfs://localhost:9000/hbase");
try{
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
}catch (IOException e){
e.printStackTrace();
}
}
public static void close(){
try {
if(admin != null) {
admin.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
//向表添加数据
public static void insterRow(String tableName, String RowKey, String columnFamily, String column, String value) throws IOException{
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(RowKey.getBytes());
put.addColumn(columnFamily.getBytes(),column.getBytes(),value.getBytes());
table.put(put);
table.close();
close();
}
//删除数据
public static void deleRow(String tableName, String RowKey, String columnFamily, String column) throws IOException{
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(Bytes.toBytes(RowKey));
delete.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column));
//删除指定的列族
//delete.addFamily(Bytes.toBytes(columnFamily));
table.delete(delete);
table.close();
close();
}
public static void main(String[] args)throws IOException{
init();
insterRow("s1","zhangsan","score","Math","69");//添加指定列
deleRow("s1","zhangsan","score","Math");//删除指定列
close();
}
}
(4)清空指定的表的所有记录数据;
清空custor表的所有记录数据
truncate 's1'
Java
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException;
public class test4 {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
public static void init(){
configuration = HBaseConfiguration.create();
configuration.set("Hbase.rootdir","hdfs://localhost:9000/hbase");
try{
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
}catch (IOException e){
e.printStackTrace();
}
}
public static void close(){
try {
if(admin != null) {
admin.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
//清空指定的表的所有记录数据
public static void clearRows(String tableName) throws IOException{
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
scan.getAllowPartialResults();
ResultScanner resultScanner = table.getScanner(scan);
for(Result result:resultScanner){
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
Delete delete = new Delete(CellUtil.cloneRow(cell));
table.delete(delete);
}
}
System.out.println("删除完毕");
}
public static void main(String[] args)throws IOException{
init();
clearRows("s1");
close();
}
}
(5)统计表的行数。
统计Student表的行数:
count 's1'
根据(3)再建回表s1,(可自行决定)
create 's1','score'
put 's1','zhangsan','score:Math','69'
delete 's1','zhangsan','score:Math'
(二)
HBase
数据库操作
1.
现有以下关系型数据库中的表和数据,要求将其转换为适合于
HBase
存储的表并插入数
据:
(1)创建Student表:
create 'Student','S_No','S_Name','S_Sex','S_Age'
添加记录数据:
put 'Student','s001','S_No','2015001'
put 'Student','s001','S_Name','Zhangsan'
put 'Student','s001','S_Sex','male'
put 'Student','s001','S_Age','23'
put 'Student','s002','S_No','2015002'
put 'Student','s002','S_Name','Mary'
put 'Student','s002','S_Sex','female'
put 'Student','s002','S_Age','22'
put 'Student','s003','S_No','2015003'
put 'Student','s003','S_Name','Lisi'
put 'Student','s003','S_Sex','male'
put 'Student','s003','S_Age','24'
(2)创建Couse表:
create 'Course','C_No','C_Name','C_Credit'
添加记录数据:
put 'Course','c001','C_No','123001'
put 'Course','c001','C_Name','Math'
put 'Course','c001','C_Credit','2.0'
put 'Course','c002','C_No','123002'
put 'Course','c002','C_Name','Computer'
put 'Course','c002','C_Credit','5.0'
put 'Course','c003','C_No','123003'
put 'Course','c003','C_Name','English'
put 'Course','c003','C_Credit','3.0'
(3)创建SC表
create 'SC','SC_Sno','SC_Cno','SC_Score'
添加记录数据:
put 'SC','sc001','SC_Sno','2015001'
put 'SC','sc001','SC_Cno','123001'
put 'SC','sc001','SC_Score','86'
put 'SC','sc002','SC_Sno','2015001'
put 'SC','sc002','SC_Cno','123003'
put 'SC','sc002','SC_Score','69'
put 'SC','sc003','SC_Sno','2015002'
put 'SC','sc003','SC_Cno','123002'
put 'SC','sc003','SC_Score','77'
put 'SC','sc004','SC_Sno','2015002'
put 'SC','sc004','SC_Cno','123003'
put 'SC','sc004','SC_Score','99'
put 'SC','sc005','SC_Sno','2015003'
put 'SC','sc005','SC_Cno','123001'
put 'SC','sc005','SC_Score','98'
put 'SC','sc006','SC_Sno','2015003'
put 'SC','sc006','SC_Cno','123002'
put 'SC','sc006','SC_Score','95'
2.
请编程实现以下功能:
(1)
createTable(String tableName, String[] fields)
创建表,参数
tableName
为表的名称,字符串数组
fields
为存储记录各个字段名称的数
组。要求当
HBase
已经存在名为
tableName
的表的时候,先删除原有的表,然后再创建新的
表。