栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

HBase 在idea中对表(ddl)和数据(dml)的相应操作

HBase 在idea中对表(ddl)和数据(dml)的相应操作

Hbase 在idea中对表(ddl)和数据(dml)的相应操作 新建Idea—项目工程名为HbaseDemo1 1.pom.xml文件(在文件末尾加入如下代码,导入依赖)

        
            org.apache.hbase
            hbase-server
            2.3.6
        
    
        
            org.apache.hbase
            hbase-client
            2.3.6
        
    
        
            org.slf4j
            slf4j-log4j12
            1.7.30
        

2.Test1.java文件 对表ddl进行操作(创建表,删除表)

(1)导入相应的包

package com.mnlg.hbase;
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.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;


public class Test1 {
    private static Configuration configuration;
    private static Connection connection;
    private static Admin admin;

(2)静态代码块

    static {//静态代码块里面静态方法要定义成静态属性
        try {
            //1、创建configuration,获取配置
            configuration = HbaseConfiguration.create();
            //2、设置端口
            configuration.set("hbase.zookeeper.quorum", "XJ202,XJ203,XJ204");
            //3、获取连接对象
            connection = ConnectionFactory.createConnection(configuration);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

(3)获取Admin对象

    
    public static Admin getAdmin() throws IOException {
        //对表的增删改查用admin
        admin = connection.getAdmin();
        return admin;
    }

(4)判断表格是否存在


	//1、判断表是否存在
    public static boolean isTableExists(String tableName) throws IOException {

//      对表增删改查用admin,对数据增删改查用table
//      Admin admin = connection.getAdmin();
        admin = getAdmin();
        boolean b = admin.tableExists(TableName.valueOf(tableName));
//      admin.close();
//      connection.close();
//      System.out.println(b);
        return b;
    }

(5)创建表

    public static void createTable(String tableName, String ... cfs) throws IOException {
//		  前三行获取连接
//        Configuration configuration = HbaseConfiguration.create();
//        configuration.set("hbase.zookeeper.quorum", "XJ202,XJ203,XJ204");
//        Connection connection = ConnectionFactory.createConnection(configuration);
//        Admin admin = connection.getAdmin();

        admin = getAdmin();
        if (isTableExists(tableName)) {
            System.out.println(tableName + "表已经存在,创建失败");
            return;
        } else if (cfs == null || cfs.length == 0) {//1、至少定义一个列族
            System.out.println("列族信息不能为空,至少输入一个列族");
            return;
        } else {//2、创建表描述器
            TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName));
            //3、添加列族定义
            for (String cf : cfs) {//4、定义列族描述器
                ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(cf));
                tableDescriptorBuilder.addColumnFamily(columnFamilyDescriptorBuilder.build());
            }
			//5、创建表
            admin.createTable(tableDescriptorBuilder.build());
            
        }
    }

(6)删除表

    public static void deleteTable(String tableName) throws IOException {
//        Configuration configuration = HbaseConfiguration.create();
//        configuration.set("hbase.zookeeper.quorum","XJ202,XJ203,XJ204");
//        Connection connection = ConnectionFactory.createConnection(configuration);
//        admin = connection.getAdmin();

        admin = getAdmin();
        //1、判断表是否存在
        if (isTableExists(tableName)){
            //2、将表下线
            admin.disableTable(TableName.valueOf(tableName));
            //3、删除表
            admin.deleteTable(TableName.valueOf(tableName));
         }else{
            System.out.println("表不存在");
        }
    }
//        admin.close();

(7)定义资源关闭方法

    public static void close() throws IOException {
        //1、关闭Admin
        if (admin != null){
            admin.close();
        }
        //2、关闭连接
        if (connection != null){
            connection.close();
        }
    }
对数据dml进行操作(增加,修改,删除,查看数据[get和scan])

(1)putTableData的新增和修改

//1.putTableData新增和修改
    

	//向表中插入数据
    public static void putTableData(String tableName,String rowKey,String cf,String cn,String value) throws IOException {
        //1、获取表对象(对数据进行操作用table)
        Table table = connection.getTable(TableName.valueOf(tableName));

        //2、创建Put对象
        Put put = new Put(Bytes.toBytes(rowKey));//put里面放字节byte数据类型,没办法放String类型

        //函数参数不同toBytes中有很多数据类型是函数的重载,【函数的重载(多态的体现)】【函数的重写,子类重写父类】
        put.addColumn(Bytes.toBytes(cf),Bytes.toBytes(cn),Bytes.toBytes(value));
		//在同一个rowKey中插入多条数据可以再 put.addColumn(***);
        
        //3、插入数据,批量插入可以用 table.put(List)
        table.put(put);
        table.close();//关闭表连接
    }

(2)deleteTableData删除

    
    public static void deleteTableData(String tableName,String rowKey,String cf,String cn) throws IOException {
        //1、获取表对象
        Table table = connection.getTable(TableName.valueOf(tableName));
        //2、创建Delete对象(只传rowKey相当于命令行中的deleteall)
        Delete delete = new Delete(Bytes.toBytes(rowKey));//ctrl(alt)+t看看括号里面需要什么参数,需要byte类型参数
        //3、删除指定列的所有版本
        delete.addColumn(Bytes.toBytes(cf),Bytes.toBytes(cn));
        
        table.delete(delete);//删除数据
        table.close();//关闭表连接
    }

(3)getRowData查看一行数据,打印(get方式)

    
    public static void getRowData(String tableName,String rowKey) throws IOException {
        //1、获取表对象
        Table table = connection.getTable(TableName.valueOf(tableName));
        //2、创建Get对象
        Get get = new Get(Bytes.toBytes(rowKey));

        //3、查询数据(与前两个不同在于增加了result)
        Result result = table.get(get);//一个result就是一行数据,后面要把他变成一个个的单元格

        

        //调用showRowData函数,实现打印数据
        showRowData(result);
        table.close();

    }


//把result单独封装出来了

    
    public static void showRowData(Result result){
        
        Cell[] cells = result.rawCells();//返回了数组Cell[],遍历数组
        
        //4、解析result获取数据,一个rowKey对应多个Cell
        for (Cell cell : cells){
            String rk = Bytes.toString(CellUtil.cloneRow(cell));//等于后面的内容起名为rk,并进行数据类型转换
            String cf = Bytes.toString(CellUtil.cloneFamily(cell));//列族
            String cn = Bytes.toString(CellUtil.cloneQualifier(cell));//列
            String vl = Bytes.toString(CellUtil.clonevalue(cell));//值

            //直接输入sout回车生成此行
            System.out.println("rk:" + rk + "tcf:" + cf + "tcn:" + cn + "tvl:" + vl);
        }
    }

(4)scan全表扫描(scan方式查询数据)

    
    public static void scanTableData(String tableName,String startRow,String stopRow) throws IOException {		 //1、获取表对象
        Table table = connection.getTable(TableName.valueOf(tableName));

//      String startRow = "1001";
//      String stopRow = "1004";前两行增加了String startRow,String stopRow就不需要这两行了
        
        //2、创建Scan对象
        Scan scan = new Scan(Bytes.toBytes(startRow),Bytes.toBytes(stopRow));//左闭右开区间[1001,1004)

        //Scan scan = new Scan();//括号后不爆红说明有空参
        
		//3、扫描表
        ResultScanner resultScanner = table.getScanner(scan);//resultScanner是Result的集合,resultScanner是一个迭代器
        
        //4、resultScanner获取数据
        for (Result result : resultScanner){
            showRowData(result);
        }
        table.close();
    }

(5)main方法

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

            boolean b = isTableExists("student");
//          System.out.println(b);
//          createTable("student","info1","info2");

//          if (!b){
//             createTable("student","info");
//          }
//          deleteTable("student");

//          putTableData("student","1001","info1","name","wdy");
//          putTableData("student","1001","info1","age","18");
//
//          deleteTableData("student","1001","info1","name");

            getRowData("student","1001");

//          scanTableData("student");
            close();
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/487506.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号