大数据基础-HBase Shell命令及Java API编程

HBase Shell命令及Java API编程

编程实现以下指定功能,并用Hadoop提供的HBase Shell命令完成相同任务:
(1)列出HBase所有的表的相关信息,例如表名
(2)在终端打印出指定的表的所有记录数据
(3)向已经创建好的表添加和删除指定的列族或列
(4)清空指定的表的所有记录数据
(5)统计表的行数

一、启动HBase

启动时需要先启动hadoop,在启动hbase;而关闭时则需要先关闭hbase,在关闭hadoop
在这里插入图片描述
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200413173948928.

二、创建项目

创建一个Java Project
在这里插入图片描述
引入Jar包
在这里插入图片描述
创建类(包名默认的即可)
在这里插入图片描述

三、Java代码

我是将所有的代码都写在一个类里面的,其实最好是建一个菜单类,然后每一题创建一个类,直接在菜单类中调用就可,这样做结构比较清晰。

1、列出HBase所有的表的相关信息,例如表名

 public static void First() throws IOException{
    	init();
    	HTableDescriptor hTableDescriptor[]=admin.listTables();
    	for (HTableDescriptor s:hTableDescriptor ){
    		System.out.println(s.getNameAsString());
    	}
    	close();
    }

2、在终端打印出指定的表的所有记录数据

 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);
    	}
    }

3、向已经创建好的表添加和删除指定的列族或列

 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);
    	}
    }

4、清空指定的表的所有记录数据

public static void Fourth(String tablename) throws IOException{
    	init();
    	HBaseAdmin admin1=new HBaseAdmin(configuration);
    	HTableDescriptor ht=admin1.getTableDescriptor(Bytes.toBytes(tablename));
    	TableName tableName=TableName.valueOf(tablename);
    	admin.disableTable(tableName);
    	admin.deleteTable(tableName);
    	admin.createTable(ht);
    	close();
    }

5、统计表的行数

 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 Configuration configuration;
    public static Connection connection;
    public static Admin admin;
    public static long ts;
    static Scanner sc=new Scanner(System.in);

②、main函数写成菜单,然后调用其他方法

 public static void main(String[] args) throws IOException{
    	while (true){
    		System.out.println("\n1、列出所有表的信息;");
    		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 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("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
            System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");
        }
    }

整体代码(代码全写在一个类,看着挺累的。。)

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;
import java.util.Scanner;
public class Fifth {
	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("\n1、列出所有表的信息;");
    		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();
    	HBaseAdmin admin1=new HBaseAdmin(configuration);
    	HTableDescriptor ht=admin1.getTableDescriptor(Bytes.toBytes(tablename));
    	TableName tableName=TableName.valueOf(tablename);
    	admin.disableTable(tableName);
    	admin.deleteTable(tableName);
    	admin.createTable(ht);
    	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("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
            System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");
        }
    }
}

四、运行代码结果

1、列出HBase所有的表的相关信息,例如表名

(1)、终端中查看存在的表
在这里插入图片描述
(2)、运行代码
在这里插入图片描述

2、在终端打印出指定的表的所有记录数据

(1)、运行eclipse后的结果
在这里插入图片描述
(2)终端中查看Score表的信息
在这里插入图片描述

3、向已经创建好的表添加和删除指定的列族或列

(1)增加
①、eclipse运行新增一列
在这里插入图片描述
②、终端中查看是否新增成功
在这里插入图片描述
(2)删除
①、此处就只演示删除一个列限定符的结果
在这里插入图片描述
②、检查终端,是否成功删除这个列限定符
在这里插入图片描述

4、清空指定的表的所有记录数据

(1)、eclipse运行结果
在这里插入图片描述
(2)、检查终端中Score这个表中数据是否清空
在这里插入图片描述

5、统计表的行数

(1)运行eclipse
在这里插入图片描述
(2)终端中检查结果
在这里插入图片描述

发布了5 篇原创文章 · 获赞 6 · 访问量 660

猜你喜欢

转载自blog.csdn.net/miss_bear/article/details/105494510