大数据 | 实验一:大数据系统基本实验 | 熟悉常用的HBase操作
文章目录
- 📚HBase安装
-
- 🐇安装HBase
- 🐇伪分布式模式配置
- 🐇测试运行HBase
- 🐇HBase java API编程环境配置
- 📚实验目的
- 📚实验平台
- 📚实验内容
-
- 🐇HBase Shell + 编程命令实现以下指定功能
-
- 🥕HBase Shell部分
- 🥕编程实现
- 🐇HBase 数据库操作
-
- 🥕学生表
- 🥕课程表
- 🥕选课表
- 🐇编程实现功能
📚HBase安装
前期准备:下载hbase-2.4.10-bin.tar.gz, HBase下载
🐇安装HBase
先把HBase拖进虚拟机,参考实验零的相关操作
⭐解压到目录下
sudo tar zxvf hbase-2.4.10-bin.tar.gz -C /usr/local
cd /usr/local #进入该目录
mv hbase hbase-2.4.10 hbase #重命名为 hbase 文件名规范
sudo chown -R hdoop ./hbase #将 hbase 目录权限赋予 hadop 用户
⭐配置hbase环境变量
vim ~/.bashrc
- 在~/.bashrc 中找到原来配置的 PATH,并添加下列代码
:/usr/local/hbase/bin
。 - PATH最终效果:
export PATH=${JAVA_HOME}/bin:/usr/local/hbase/bin/:$PATH
⭐更新配置,并测试是否安装成功
source ~/.bashrc #使新配置的环境变量生效
/usr/local/hbase/bin/hbase version #检测是否安装成功,查看hbase 版本
🐇伪分布式模式配置
⭐配置/usr/local/hbase/conf/hbase-env.sh
vim /usr/local/hbase/conf/hbase-env.sh #打开文件
添加如下代码
export JAVA_HOME=/usr/lib/jvm/java
export HBASE_CLASSPATH=/usr/local/hbase/conf
export HBASE_MANAGES_ZK=true
⭐配置/usr/local/hbase/conf/hbase-site.xml
vim /usr/local/hbase/conf/hbase-site.xml #打开文件
进行如下配置修改
<configuration><property><name>hbase.rootdir</name><value>hdfs://localhost:9000/hbase</value></property><property><name>hbase.cluster.distributed</name><value>true</value></property><property><name>hbase.unsafe.stream.capability.enforce</name><value>false</value></property>
</configuration>
🐇测试运行HBase
⭐启动Hadoop:start-dfs.sh
⭐启动HBase
cd /usr/local/hbase
bin/start-hbase.sh
⭐进入Shell界面:bin/hbase shell
HBase停止命令(
我一般只挂起不停止,emmm
exit
#退出 Shell 界面bin/stop-hbase.sh
#停止 Hbase
🐇HBase java API编程环境配置
该部分参考博客,这一步主要是导入相关的JAVA包,不然之后相关头文件都报错。
⭐启动Eclipse,Eclipse的相关配置见实验零
⭐创建java Project HBaseExample
- 右键new一个project
- 在“JRE”选项卡中选中第2项“Use a project specific JRE”,然后点击界面底部的“Next”按钮。
- 在弹出的界面中(如下图所示),用鼠标点击“Libraries”选项卡,然后,点击界面右侧的“Add External JARs…”按钮。
- 在弹出的“JAR Selection”界面中(如下图所示),进入到“/usr/local/hbase/lib”目录(在
+ Other Locations
里找),选中该目录下的所有jar文件(注意,不要选中client-facing-thirdparty、ruby、shaded-clients和zkcli这四个目录),然后,点击界面上方的“Open”按钮。
- 点完Open后会退出去,然后,再点界面右侧的“Add External JARs…”按钮。在“JAR Selection”界面中(如下图所示),点击进入到“client-facing-thirdparty”目录下。
- 全选中再点Open。
- 最后,再点击界面(如下图所示)底部的“Finish”按钮。
📚实验目的
1)理解 HBase 在 Hadoop 体系结构中的角色。
2)熟练使用 HBase 操作常用的 shell 命令。
3)熟悉 HBase 操作常用的 Java API。
📚实验平台
1)操作系统:Linux;
2)Hadoop 版本:3.2.2;
3)HBase 版本:1.2.6;
4)JDK 版本:1.8;
5)Java IDE:Eclipse。
📚实验内容
🐇HBase Shell + 编程命令实现以下指定功能
- 启动Hadoop:
start-dfs.sh
- 启动HBase
-cd /usr/local/hbase
-bin/start-hbase.sh
- 进入shell界面:
bin/hbase shell
🥕HBase Shell部分
🔔列出 HBase 所有的表的相关信息,例如表名
用HBase Shell创建表时出错,参考解决博客
🔔在终端打印出指定的表的所有记录数据
🔔向已经创建好的表添加和删除指定的列族或列
🔔清空指定的表的所有记录数据
🔔统计表的行数
为更明显,本题是在第4题还没进行的时候截的图
🥕编程实现
package hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.ClusterConnection;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.Scanner;
public class basic_hbase
{public static Configuration configuration;public static Connection connection;public static Admin admin;public static long ts;static Scanner sc=new Scanner(System.in);public static void main(String[] args) throws IOException{while (true){System.out.println("1.列出所有表的信息;");System.out.println("2.打印指定表的所有记录数据;");System.out.println("3.向创建好的表添加或删除指定的列族或列;");System.out.println("4.清空指定表的所有记录数据;");System.out.println("5.统计表的行数;");System.out.println("输入你的选择:");int no=sc.nextInt();if (no==1){First();}else if(no==2){System.out.println("输入你要查询的表:");String tablename=sc.next();Second(tablename);}else if (no==3){System.out.println("请输入要操作的表名");String tablename=sc.next();System.out.println("请输入要操作的表的行键");String rowKey=sc.next();System.out.println("请输入你要操作的列族名:");String colFamily=sc.next();System.out.println("输入你要操作的列名:");String col=sc.next();System.out.println("输入你要操作的参数值:");String val=sc.next();Third(tablename,rowKey,colFamily,col,val);}else if (no==4){System.out.println("输入你要操作的表:");String tablename=sc.next();Fourth(tablename);System.out.println("成功清空!");}else if (no==5){System.out.println("输入你要操作的表:");String tablename=sc.next();fift(tablename);}}}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 First() throws IOException{//列出所有表的信息;init();HTableDescriptor hTableDescriptor[]=admin.listTables();for (HTableDescriptor s:hTableDescriptor ){System.out.println(s.getNameAsString());}close();}public static void Second(String tablename) throws IOException{//打印指定表的所有记录数据;init();Table table=connection.getTable(TableName.valueOf(tablename));Scan scan=new Scan();ResultScanner res=table.getScanner(scan);for (Result result:res){showCell(result);}}public static void Third(String tableName, String row, String column,String c ,String val) throws IOException{//向创建好的表添加或删除指定的列族或列;System.out.println("1、添加列;2、删除列");int no=sc.nextInt();if (no==1){insertRow(tableName,row,column,c,val);}else if (no==2){deleteRow(tableName,row,column,c);}}public static void Fourth(String tablename) throws IOException{//清空指定表的所有记录数据;init();TableName tableName=TableName.valueOf(tablename);admin.disableTable(tableName);admin.deleteTable(tableName);close();}public static void fift(String tablename) throws IOException{//统计表的行数;init();Table table=connection.getTable(TableName.valueOf(tablename));Scan scan=new Scan();ResultScanner scanner=table.getScanner(scan);int n=0;for (Result result=scanner.next();result!=null;result=scanner.next()){n++;}System.out.println("行数有"+n);scanner.close();close();}public static void insertRow(String tableName, String row, String column,String c ,String val) throws IOException {init();Table table=connection.getTable(TableName.valueOf(tableName));Put put=new Put(row.getBytes());put.addColumn(column.getBytes(), c.getBytes(), val.getBytes());table.put(put);System.out.println("成功添加!");table.close();close();}public static void deleteRow(String tableName, String row, String column,String c) throws IOException{init();Table table=connection.getTable(TableName.valueOf(tableName));System.out.println("1、删除列族;2、删除列名");Scanner sc=new Scanner(System.in);int no=sc.nextInt();Delete delete=new Delete(row.getBytes());if (no==1){delete.addFamily(Bytes.toBytes(column));System.out.println("成功删除"+column+"这个列族");}else if(no==2){delete.addColumn(Bytes.toBytes(column), Bytes.toBytes(c));System.out.println("成功删除"+c+"这个列名");}table.delete(delete);table.close();close();}public static void showCell(Result result){Cell[] cells = result.rawCells();for(Cell cell:cells){System.out.println("RowName(行键):"+new String(CellUtil.cloneRow(cell))+" ");System.out.println("Timetamp(时间戳):"+cell.getTimestamp()+" ");System.out.println("column Family(列族):"+new String(CellUtil.cloneFamily(cell))+" ");System.out.println("column Name(列名):"+new String(CellUtil.cloneQualifier(cell))+" ");System.out.println("value:(值)"+new String(CellUtil.cloneValue(cell))+" ");}}
}
🐇HBase 数据库操作
🥕学生表
🔔建学生表
🔔学生表数据添加
🥕课程表
🔔建课程表
🔔课程表数据添加
🥕选课表
🔔建选课表
🔔选课表数据添加
🐇编程实现功能
package hbase;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.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.client.Delete;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
@SuppressWarnings("deprecation")
public class hbase
{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();}if(null != connection){connection.close();}}catch (IOException e){e.printStackTrace();}}/*** 建表。参数tableName为表的名称,字符串数组fields为存储记录各个域名称的数组。* 要求当HBase已经存在名为tableName的表时,先删除原有的表,然后再创建新的表 field:列族* @param myTableName 表名* @param colFamily 列族名* @throws IOException*/public static void createTable(String tableName,String[] fields) throws IOException {init();TableName tablename = TableName.valueOf(tableName);if(admin.tableExists(tablename)){System.out.println("表已存在,将执行删除原表,重建新表!");admin.disableTable(tablename);admin.deleteTable(tablename);//删除原来的表}HTableDescriptor hTableDescriptor = new HTableDescriptor(tablename);for(String str:fields){HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);hTableDescriptor.addFamily(hColumnDescriptor);}admin.createTable(hTableDescriptor);System.out.println("表已创建成功"); close();}/*** 向表 tableName、行 row(用 S_Name 表示)和字符串数组 fields 指定的单元格中添加对应的数据 values。* 其中,fields 中每个元素如果对应的列族下还有相应的列限定符的话,用“columnFamily:column”表示。* 例如,同时向“Math”、“Computer Science”、“English”三列添加成绩时,* 字符串数组 fields 为{“Score:Math”, ”Score:Computer Science”, ”Score:English”},* 数组values 存储这三门课的成绩。*/public static void addRecord(String tableName,String rowKey,String []fields,String [] values) throws IOException {init();Table table = connection.getTable(TableName.valueOf(tableName));for (int i = 0; i < fields.length; i++) {Put put = new Put(rowKey.getBytes());String [] cols = fields[i].split(":");if(cols.length==1){//因为当输入的是单列族,split仅读出一个字符字符串,即cols仅有一个元素put.addColumn(cols[0].getBytes(), "".getBytes(), values[i].getBytes());}else {put.addColumn(cols[0].getBytes(), cols[1].getBytes(), values[i].getBytes());}table.put(put);}table.close();close();}/*** 根据表名查找表信息*/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();}/*** 格式化输出* @param result*/public static void showCell(Result result){Cell[] cells = result.rawCells();for(Cell cell:cells){System.out.println("RowName(行键):"+new String(CellUtil.cloneRow(cell))+" ");System.out.println("Timetamp(时间戳):"+cell.getTimestamp()+" ");System.out.println("column Family(列族):"+new String(CellUtil.cloneFamily(cell))+" ");System.out.println("column Name(列名):"+new String(CellUtil.cloneQualifier(cell))+" ");System.out.println("value:(值)"+new String(CellUtil.cloneValue(cell))+" ");System.out.println();}}/*** 浏览表 tableName 某一列的数据,如果某一行记录中该列数据不存在,则返回 null。* 要求当参数 column 为某一列族名称时,如果底下有若干个列限定符,则要列出每个列限定符代表的列的数据;* 当参数 column 为某一列具体名称(例如“Score:Math”)时,只需要列出该列的数据。* @param tableName* @param column* @throws IOException*/public static void scanColumn (String tableName,String column) throws IOException{init();Table table = connection.getTable(TableName.valueOf(tableName));Scan scan = new Scan();String [] cols = column.split(":");if(cols.length==1){//如果参数只有一部分,那么就将这个参数作为列族来扫描scan.addFamily(Bytes.toBytes(column));}else {//否则就将这个参数作为具体的列名进行扫描scan.addColumn(Bytes.toBytes(cols[0]),Bytes.toBytes(cols[1]));}ResultScanner scanner = table.getScanner(scan);for (Result result = scanner.next(); result !=null;result = scanner.next()) {//扫描输出showCell(result);}table.close();close();}/*** 修改表 tableName,行 row(可以用学生姓名 S_Name 表示),列 column 指定的单元格的数据。* @throws IOException*/public static void modifyData(String tableName,String rowKey,String column,String value) throws IOException{init();Table table = connection.getTable(TableName.valueOf(tableName));//用rowKey参数设置要插入数据的行键Put put = new Put(rowKey.getBytes());String [] cols = column.split(":");if(cols.length==1){//如果参数只有一部分,则将该部分做为列族名,并设置列名为空字符串put.addColumn(column.getBytes(),"".getBytes() , value.getBytes());//qualifier:列族下的列名}else {//否则第一部分为列族名,第二部分为列名put.addColumn(cols[0].getBytes(),cols[1].getBytes() , value.getBytes());//qualifier:列族下的列名}//将数据插入到表中table.put(put);table.close();close();}/*** 删除表 tableName 中 row 指定的行的记录。根据行键删* @throws IOException*/public static void deleteRow(String tableName,String rowKey) throws IOException{init();Table table = connection.getTable(TableName.valueOf(tableName));//使用rowKey参数设置要删除数据的行键Delete delete = new Delete(rowKey.getBytes());table.delete(delete);table.close();close();}/*** @param args* @throws IOException*/public static void main(String[] args) throws IOException {// TODO Auto-generated method stubhbase test_Two = new hbase();boolean flag =true;while(flag){System.out.println("------------------------------------------------提供以下功能----------------------------------------------");System.out.println(" 1- createTable(创建表,提供表名、列族名) ");System.out.println(" 2- addRecord(向已知表名、行键、列簇的表添加值) ");System.out.println(" 3- ScanColumn(浏览表某一列的数据) ");System.out.println(" 4- modifyData(修改某表行,某一列,指定的单元格的数据) ");System.out.println(" 5- deleteRow(删除某表某行的记录) ");System.out.println("------------------------------------------------------------------------------------------------------------------");Scanner scan = new Scanner(System.in);String choose1=scan.nextLine();switch (choose1) {case "1":{System.out.println("请输入要创建的表名");String tableName=scan.nextLine();System.out.println("请输入要创建的表的列族个数");int Num=scan.nextInt();String [] fields = new String[Num];System.out.println("请输入要创建的表的列族");for(int i=0;i< fields.length;i++){scan = new Scanner(System.in);fields[i]=scan.nextLine();}System.out.println("正在执行创建表的操作");test_Two.createTable(tableName,fields);break;}case "2":{System.out.println("请输入要添加数据的表名");String tableName=scan.nextLine();System.out.println("请输入要添加数据的表的行键");String rowKey=scan.nextLine();System.out.println("请输入要添加数据的表的列的个数");int num =scan.nextInt();String fields[]=new String[num];System.out.println("请输入要添加数据的表的列信息 共"+num+"条信息");for(int i=0;i< fields.length;i++){BufferedReader in3= new BufferedReader(new InputStreamReader(System.in));fields[i] = in3.readLine();}System.out.println("请输入要添加的数据信息 共"+num+"条信息");String values[]=new String[num];for(int i=0;i< values.length;i++){BufferedReader in2 = new BufferedReader(new InputStreamReader(System.in));values[i] = in2.readLine();}System.out.println("原表信息");test_Two.getData(tableName);System.out.println("正在执行向表中添加数据的操作........\\n");test_Two.addRecord(tableName, rowKey, fields, values);System.out.println("\\n添加后的表的信息........");test_Two.getData(tableName);break;}case "3":{System.out.println("请输入要查看数据的表名");String tableName=scan.nextLine();System.out.println("请输入要查看数据的列名");String column=scan.nextLine();System.out.println("查看的信息如下:........\\n");test_Two.scanColumn(tableName, column);break;}case "4":{System.out.println("请输入要修改数据的表名");String tableName=scan.nextLine();System.out.println("请输入要修改数据的表的行键");String rowKey=scan.nextLine();System.out.println("请输入要修改数据的列名");String column=scan.nextLine();System.out.println("请输入要修改的数据信息 ");String value=scan.nextLine();System.out.println("原表信息如下:........\\n");test_Two.getData(tableName);System.out.println("正在执行向表中修改数据的操作........\\n");test_Two.modifyData(tableName, rowKey, column, value);System.out.println("\\n修改后的信息如下:........\\n");test_Two.getData(tableName);break;}case "5":{System.out.println("请输入要删除指定行的表名");String tableName=scan.nextLine();System.out.println("请输入要删除指定行的行键");String rowKey=scan.nextLine();System.out.println("原表信息如下:........\\n");test_Two.getData(tableName);System.out.println("正在执行向表中删除数据的操作........\\n");test_Two.deleteRow(tableName, rowKey);System.out.println("\\n删除后的信息如下:........\\n");test_Two.getData(tableName);break;}default:{System.out.println(" 你的操作有误 !!! ");break;}}System.out.println(" 你要继续操作吗? 是-true 否-false ");flag=scan.nextBoolean();}System.out.println(" 程序已退出! ");}
}