“大数据技术原理与应用”课程实验报告
题目:实验三:熟悉常
用的 HBase 操作
姓名:朱小凡
日期:2022/3/29
1、实验环境:
设备名称 LAPTOP-9KJS8HO6
处理器 Intel(R) Core(TM) i5-10300H CPU @ 2.50GHz 2.50 GHz
机带 RAM16.0 GB (15.8 GB 可用)
主机操作系统 Windows 10 家庭中文版
虚拟机操作系统 ubuntukylin-16.04
Hadoop 版本 3.1.3
JDK 版本 1.8
Java IDE:Eclipse
系统类型 64 位操作系统, 基于 x64 的处理器
笔和触控 没有可用于此显示器的笔或触控输入
2、实验内容与完成情况:
1.编程实现以下指定功能,并用 Hadoop 提供的 HBase Shell 命令完
成相同任务
(1) 列出 HBase 所有的表的相关信息,例如表名。
a.Shell 命令
HBase Shell:List
图 1.列出表名
b.java 命令
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
public class test1 {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
/**
* @param args
* 建立连接
*/
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();
}
if(null != connection){
connection.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
/*
* 查看已有表
* @throws IOException
*/
public static void listTables() throws IOException{
init();
@SuppressWarnings("deprecation")
HTableDescriptor hTableDescriptors [] = admin.listTables();
for(HTableDescriptor hTableDescriptor : hTableDescriptors){
System.out.println(hTableDescriptor.getNameAsString());
}
close();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
test1 t = new test1();
try{
System.out.println("以下为 Hbase 数据库中所存的表信息");
t.listTables();
}catch (IOException e){
e.printStackTrace();
}
}
}
图 2.列出表名(java)
(2)在终端打印出指定的表的所有记录数据。
a.Shell 命令
scan 'student'
图 3.打印指定表数据
b.java 代码
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException;
import java.util.Scanner;
public class Test_2 {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
/**
* @param args
*/
//建立连接
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();
}
if(null != connection){
connection.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
/**
* 根据表名查找表信息
*/
public static void getData(String tableName)throws IOException{
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
for(Result result:scanner)
{
showCell((result));
}
close();
}