HBase学习记录-API

delete.addColumns()和delete.addColumn()的区别

/** * delete.addColumns(); * delete.addColumn(); * 区别: * addColumn是删除某一个列簇里的最新时间戳版本。 *          传时间戳,则删除指定版本 * addColumns是删除某个列簇里的所有时间戳版本。其如果修改数据后, 不进行flush或没到flush事件,则会出现旧数据的出现;造成数据未修改彻底未删除彻底 *           传时间戳,可删除指定版本,如果没有指定版本的数据则删除小于等于 * 所以大部分用addColumn() **/建表时避免多个版本,避免后期需要删除API代码
package com.hadoop100.test;

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;

/**
 * @author : GuoSpringStrong
 * @date : Created in 2022/2/22 16:22
 * @description :
 */
public class TestAPI {
    private static Connection connection;
    private static Admin admin;

    //静态代码块,一些配置信息
    static {
        try {
            //获取配置信息
            Configuration conf = HBaseConfiguration.create();
            conf.set("hbase.zookeeper.quorum", "hadoop102,hadoop103,hadoop104");

            //创建连接对象
            connection = ConnectionFactory.createConnection(conf);

            //创建admin对象
            admin = connection.getAdmin();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //关闭资源
    public static void close() {
        if (admin != null) {
            try {
                admin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (connection != null) {
            try {
                connection.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

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

        return admin.tableExists(TableName.valueOf(tableName));
    }

    //2 创建表
    public static void createTable(String tableName, String... columnFamily) throws IOException {
        //判断是否存在列族信息
        if (columnFamily.length )
            System.out.println("请设置列族信息!");
        //判断表是否存在
        if (isTableExist(tableName)) {
            System.out.println(tableName + "已存在");
            return;
        } else System.out.println(tableName + "不存在");

        //创建表描述器
        HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
        //添加列族信息
        for (String cf : columnFamily) {
            //创建列族描述器
            HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf);
            hTableDescriptor.addFamily(hColumnDescriptor);
        }

        //创建表
        admin.createTable(hTableDescriptor);
        System.out.println("表" + tableName + "创建成功!");

    }

    //3 创建命名空间
    public static void createNameSpace(String nameSpace) {
        //创建命名空间描述器
        NamespaceDescriptor nameSpaceDescriptor = NamespaceDescriptor.create(nameSpace).build();
        //创建命名空间
        try {
            admin.createNamespace(nameSpaceDescriptor);
            System.out.println(nameSpace + " 命名空间已创建!");
        } catch (NamespaceExistException e) {
            System.out.println(nameSpace + " 命名空间已存在!");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //4 删除表
    public static void dropTable(String tableName) throws IOException {
        //判断表是否存在
        if (!isTableExist(tableName)) {
            System.out.println(tableName + "不存在!");
            return;
        }

        //使表下线
        admin.disableTable(TableName.valueOf(tableName));
        //删除表
        admin.deleteTable(TableName.valueOf(tableName));
        System.out.println(tableName + "表删除成功");

    }

    //5 插入数据
    public static void putData(String tableName, String rowKey, String colInfo, String colName, String value) throws IOException {
        //获取表
        Table table = connection.getTable(TableName.valueOf(tableName));

        //创建Put对象,存放数据
        Put put = new Put(Bytes.toBytes(rowKey));
        //向put对象中添加列信息:列族、列名、值
        put.addColumn(Bytes.toBytes(colInfo), Bytes.toBytes(colName), Bytes.toBytes(value));

        //插入数据
        table.put(put);

        //关闭资源
        table.close();

    }

    //6 获取数据
    public static void getData(String tableName, String rowKey, String colInfo, String colName) throws IOException {
        //获取表
        Table table = connection.getTable(TableName.valueOf(tableName));

        //创建Get对象,获取数据
        Get get = new Get(Bytes.toBytes(rowKey));
        //获取指定列族
        //get.addFamily(Bytes.toBytes(colInfo));
        //获取指定列
        //get.addColumn(Bytes.toBytes(colInfo), Bytes.toBytes(colName));

        //获取数据
        Result result = table.get(get);

        //解析result并打印
        for (Cell cell : result.rawCells()) {
            //打印
            System.out.print("行键:" + Bytes.toString(CellUtil.cloneRow(cell)) + "\t");
            System.out.print("列族:" + Bytes.toString(CellUtil.cloneFamily(cell)) + "\t");
            System.out.print("列名:" + Bytes.toString(CellUtil.cloneQualifier(cell)) + "\t");
            System.out.print("值:" + Bytes.toString(CellUtil.cloneValue(cell)) + "\n");
        }

        table.close();
    }

    //7 获取数据(scan扫描)
    public static void scanTable(String tableName) throws IOException {
        //获取表
        Table table = connection.getTable(TableName.valueOf(tableName));

        //获取数据
        ResultScanner results = table.getScanner(new Scan());

        //解析数据
        for (Result result : results) {
            for (Cell cell : result.rawCells()) {
                //打印
                System.out.print("行键:" + Bytes.toString(CellUtil.cloneRow(cell)) + "\t");
                System.out.print("列族:" + Bytes.toString(CellUtil.cloneFamily(cell)) + "\t");
                System.out.print("列名:" + Bytes.toString(CellUtil.cloneQualifier(cell)) + "\t");
                System.out.print("值:" + Bytes.toString(CellUtil.cloneValue(cell)) + "\n");

            }
            System.out.println();
        }

        table.close();

    }

    //8 删除数据
    public static void delectData(String tableName, String rowKey, String colInfo, String colName) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));

        //创建删除对象
        Delete delete = new Delete(Bytes.toBytes(rowKey));

        //删除指定列
        delete.addColumn(Bytes.toBytes(colInfo), Bytes.toBytes(colName));
        /**
         * delete.addColumns();
         * delete.addColumn();
         * 区别:
         * addColumn是删除某一个列簇里的最新时间戳版本。
         *          传时间戳,则删除指定版本
         * addColumns是删除某个列簇里的所有时间戳版本。其如果修改数据后, 不进行flush或没到flush事件,则会出现旧数据的出现;造成数据未修改彻底未删除彻底
         *           传时间戳,可删除指定版本,如果没有指定版本的数据则删除小于等于*/

        //删除列族
        delete.addFamily(Bytes.toBytes(colInfo));

        //执行删除
        table.delete(delete);

        table.close();
    }

    public static void main(String[] args) throws IOException {
        //判断表是否存在
        //System.out.println(isTableExist("stu2"));

        //创建命名空间
        //createNameSpace("0425");

        //创建表
        //createTable("0425:stu2", "info1", "info2");

        //插入数据
        //putData("0425:stu2", "1001", "info1", "sex", "male");

        //获取数据
        getData("stu", "1001", "personal_info", "");

        //扫描表
        scanTable("stu");

        //删除表
        //dropTable("stu2");

        //关闭资源
        close();
    }
}

undefined

undefined

Original: https://www.cnblogs.com/springstrong/p/15928543.html
Author: SpringStrong
Title: HBase学习记录-API

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/565259/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球