大三每日总结

idea连接hbase,同时利用

phoenix优化代码,实现增删改查操作
package org.example;

import java.sql.*;
import java.util.Properties;
public class PhoenixClient {
    public static void main(String[] args) throws SQLException {
        // 标准的 JDBC 代码
        // 1.添加链接
        String url =
                "jdbc:phoenix:node1,node2,node3:2181";
        // 2. 创建配置
        // 没有需要添加的必要配置 因为 Phoenix 没有账号密码
        Properties properties = new Properties();
        // 3. 获取连接
        Connection connection = DriverManager.getConnection(url,
                properties);
        // 5.编译 SQL 语句
        PreparedStatement preparedStatement =
                connection.prepareStatement("select * from student");
        // 6.执行语句
        ResultSet resultSet = preparedStatement.executeQuery();
        // 7.输出结果
        while (resultSet.next()){
            System.out.println(resultSet.getString(1) + ":" +
                    resultSet.getString(2) + ":" + resultSet.getString(3)+ ":" + resultSet.getString(4));
        }
        // 8.关闭资源
        connection.close();
        // 由于 Phoenix 框架内部需要获取一个 HBase 连接,所以会延迟关闭
        // 不影响后续的代码执行
        System.out.println("hello");
    }
}

 

package org.example;

import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.CompareOperator;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.ColumnValueFilter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.SingleColumnValueExcludeFilter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.FileFilter;
import java.io.IOException;

public class HbaseDML {

    public static Connection connection = HbaseConection.connection;

    public static void putCell(String namespace,String tableName,String rowkey,String columnFamily,String columnName,String value) throws IOException {
        Table table = connection.getTable(TableName.valueOf(namespace, tableName));
        Put put = new Put(Bytes.toBytes(rowkey));
        put.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(columnName),Bytes.toBytes(value));
        try {
            table.put(put);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        table.close();

    }

    public static void getCells(String namespace,String tableName,String rowkey,String columnFamily,String columnName) throws IOException {
        Table table = connection.getTable(TableName.valueOf(namespace, tableName));
        Get get = new Get(Bytes.toBytes(rowkey));
        get.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(columnName));
        get.readAllVersions();

        try {
            Result result = null;
            result = table.get(get);
            Cell[] cells = result.rawCells();
            for(Cell cell : cells)
            {
                String value = new String(CellUtil.cloneValue(cell));
                System.out.println(value);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        table.close();
    }

    public static void scanRows(String namespace,String tableName,String startRow,String stopRow) throws IOException {
        Table table = connection.getTable(TableName.valueOf(namespace, tableName));
        Scan scan = new Scan();
        scan.withStartRow(Bytes.toBytes(startRow));
        scan.withStopRow(Bytes.toBytes(stopRow));
        ResultScanner scanner = null;
        try {
            scanner = table.getScanner(scan);
            for(Result result : scanner)
            {
                Cell[] cells = result.rawCells();
                for(Cell cell : cells)
                {
                    System.out.print(new String(CellUtil.cloneRow(cell)) + "-" + new String(CellUtil.cloneFamily(cell))+ "-" + new String(CellUtil.cloneQualifier(cell))+ "-" + new String(CellUtil.cloneValue(cell)) + "\t");

                }
                System.out.println();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        table.close();
    }

//    public static void filterScan(String namespace,String tableName,String startRow,String stopRow,String cloumnFamily,String cloumnName,String value) throws IOException {
//        Table table = connection.getTable(TableName.valueOf(namespace, tableName));
//        Scan scan = new Scan();
//        scan.withStartRow(Bytes.toBytes(startRow));
//        scan.withStopRow(Bytes.toBytes(stopRow));
//        FilterList filterList = new FilterList();
//        ColumnValueFilter columnValueFilter = new ColumnValueFilter(Bytes.toBytes(cloumnFamily), Bytes.toBytes(cloumnName), CompareOperator.EQUAL, Bytes.toBytes(value));
//        SingleColumnValueExcludeFilter singleColumnValueExcludeFilter = new SingleColumnValueExcludeFilter(Bytes.toBytes(cloumnFamily), Bytes.toBytes(cloumnName), CompareOperator.EQUAL, Bytes.toBytes(value));
//        filterList.addFilter(singleColumnValueExcludeFilter);
//        scan.setFilter(filterList);
//
//        try {
//            ResultScanner scanner = table.getScanner(scan);
//            for(Result result : scanner)
//            {
//                Cell[] cells = result.rawCells();
//                for(Cell cell : cells)
//                {
//                    System.out.print(new String(CellUtil.cloneRow(cell)) + "-" + new String(CellUtil.cloneFamily(cell))+ "-" + new String(CellUtil.cloneQualifier(cell))+ "-" + new String(CellUtil.cloneValue(cell)) + "\t");
//
//                }
//                System.out.println();
//            }
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//
//        table.close();
//    }
public static void filterScan(String namespace, String tableName,
                              String startRow, String stopRow, String columnFamily, String
                                      columnName, String value) throws IOException {
    // 1. 获取 table
    Table table =
            connection.getTable(TableName.valueOf(namespace, tableName));
    // 2. 创建 scan 对象
    Scan scan = new Scan();
    // 如果此时直接调用 会直接扫描整张表
    // 添加参数 来控制扫描的数据
    // 默认包含
    scan.withStartRow(Bytes.toBytes(startRow));
    // 默认不包含
    scan.withStopRow(Bytes.toBytes(stopRow));
    // 可以添加多个过滤
    FilterList filterList = new FilterList();
    // 创建过滤器
    // (1) 结果只保留当前列的数据
    ColumnValueFilter columnValueFilter = new ColumnValueFilter(
            // 列族名称
            Bytes.toBytes(columnFamily),
            // 列名
            Bytes.toBytes(columnName),
            // 比较关系
            CompareOperator.EQUAL,
            //
            Bytes.toBytes(value)
    );
    // (2) 结果保留整行数据
    // 结果同时会保留没有当前列的数据
    SingleColumnValueFilter singleColumnValueFilter = new
            SingleColumnValueFilter(
            // 列族名称
            Bytes.toBytes(columnFamily),
            // 列名
            Bytes.toBytes(columnName),
            // 比较关系
            CompareOperator.EQUAL,
            //
            Bytes.toBytes(value)
    );
    // 本身可以添加多个过滤器
    filterList.addFilter(columnValueFilter);
    // 添加过滤
    scan.setFilter(filterList);
    try {
        // 读取多行数据 获得 scanner
        ResultScanner scanner = table.getScanner(scan);
        // result 来记录一行数据 cell 数组
        // ResultScanner 来记录多行数据 result 的数组
        for (Result result : scanner) {
            Cell[] cells = result.rawCells();
            for (Cell cell : cells) {
                System.out.print(new
                        String(CellUtil.cloneRow(cell)) + "-" + new
                        String(CellUtil.cloneFamily(cell)) + "-" + new
                        String(CellUtil.cloneQualifier(cell)) + "-" + new
                        String(CellUtil.cloneValue(cell)) + "\t");
            }
            System.out.println();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    // 3. 关闭 table
    table.close();
}
    /**
     * 删除 column 数据
     *
     * @param nameSpace
     * @param tableName
     * @param rowKey
     * @param family
     * @param column
     * @throws IOException
     */
    public static void deleteColumn(String nameSpace, String tableName,
                                    String rowKey, String family, String column) throws IOException {
        // 1.获取 table
        Table table = connection.getTable(TableName.valueOf(nameSpace,
                tableName));
        // 2.创建 Delete 对象
        Delete delete = new Delete(Bytes.toBytes(rowKey));
        // 3.添加删除信息
        // 3.1 删除单个版本
//
        delete.addColumn(Bytes.toBytes(family),Bytes.toBytes(column));
        // 3.2 删除所有版本
        delete.addColumns(Bytes.toBytes(family),
                Bytes.toBytes(column));
        // 3.3 删除列族
// delete.addFamily(Bytes.toBytes(family));
        // 3.删除数据
        table.delete(delete);
        // 5.关闭资源
        table.close();
    }
    public static void main(String[] args) throws IOException {
//        putCell("atguigu","student","2001","info","name","lisi");
//        putCell("atguigu","student","2001","info","name","wangwu");
//        getCells("atguigu","student","2001","info","name");
//        putCell("atguigu","student","2002","info","name","zhangsan");
//        scanRows("atguigu","student","2001","2003");
        filterScan("atguigu","student","2001","2003","info","name","zhangsan");
        //deleteColumn("atguigu","student","2001","info","name");
        System.out.println("其他代码");
        HbaseConection.closeConnection();

    }
}

 

posted @ 2023-09-13 17:37  摆烂达人  阅读(7)  评论(0编辑  收藏  举报