hbase做分頁

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

controller:

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: jja
 * @Description:
 * @Date: 2020/2/22 12:36
 */
@RestController
public class HbaseController {

    @Autowired
    HbaseService hbaseService;


    @GetMapping("/hello")
    public String print() {
        return "ok";
    }


    /**
     * 獲取所有的命名空間
     * @return 命名空間的list
     */
    @GetMapping("/getAllNameSpaces")
    public List<String> getAllNameSpaces() {
         List<String> allNamespacesList = new ArrayList<>();

        try {
            allNamespacesList = this.hbaseService.getAllNamespaces();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return allNamespacesList;
    }


    /**
     * 獲取某個命名空間下的表名
     * @param nameSpace 命名空間
     * @return 命名空間的list
     */
    @PostMapping("/nameSpace")
    public List<String> getAllTableNamesOfNameSpace(String nameSpace) {
        List<String> allTableNamesOfNameSpaceList = new ArrayList<>();
        try {
            allTableNamesOfNameSpaceList = this.hbaseService.getAllTableNamesOfNameSpace(nameSpace);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return allTableNamesOfNameSpaceList;
    }


    /**
     * 根據tablename查詢數據
     * @param tableName 表名
     * @param pageMun 每頁多少條
     * @param pageSize 第幾頁
     * @return 數據
     * @throws IOException
     */
    @PostMapping("/getPageData")
    public Map<String, Map<String, String>> getPageData(String tableName, int pageMun, int pageSize) throws IOException {
         Map<String, Map<String, String>> stringMapMap = new HashMap<>();
        try {
            stringMapMap = this.hbaseService.getPageData(tableName, pageMun,pageSize);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringMapMap;
    }
}

service:

package com.example.demo;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.PageFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.springframework.stereotype.Service;
import sun.nio.cs.ext.IBM037;

import java.io.IOException;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Author: jja
 * @Description:
 * @Date: 2020/2/22 12:48
 */
@Service("hbaseServive")
public class HbaseService {

    private static Connection connection = null;
    private static Admin admin = null;
    private static Configuration configuration;

    /**
     * 靜態代碼塊,用於初始化Hbase連接
     */
    static {
        try {
            //獲取配置信息
            configuration = HBaseConfiguration.create();
            configuration.set("hbase.zookeeper.quorum", "192.168.1.100");
            configuration.set("hbase.zookeeper.property.clientPort", "2181");

            //創建連接
            connection = ConnectionFactory.createConnection(configuration);

            //創建Admin
            admin = connection.getAdmin();

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

    /**
     * 獲取HBase連接
     *
     * @return
     */
    public Connection getConnection() {
        return connection;
    }

    /**
     * 獲取Admin實例
     *
     * @return
     */
    public Admin getAdmin() {
        return admin;
    }

    public Table getTable(String tableName) throws IOException {
        Table table = connection.getTable(TableName.valueOf(tableName));
        return table;
    }

    /**
     * 判斷表名稱是否存在
     *
     * @param tableName 表名稱
     * @return 是否存在
     * @throws IOException
     */
    public boolean isTableExist(String tableName) throws IOException {
        return admin.tableExists(TableName.valueOf(tableName));
    }

    /**
     * 創建表
     *
     * @param tableName 表名稱
     * @param cfs       可變 列簇
     * @throws IOException
     */
    public void createTable(String tableName, String... cfs) throws IOException {
        //判斷是否存在列簇信息
        if (cfs.length <= 0) {
            System.out.println("請設置列簇信息");
            return;
        }
        if (isTableExist(tableName)) {
            System.out.println(tableName + "表已經存在!");
            return;
        }
        //創建表描述器
        HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
        //循環添加列簇

        for (String cf : cfs) {
            HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf);
            hTableDescriptor.addFamily(hColumnDescriptor);
        }
        admin.createTable(hTableDescriptor);

    }

    /**
     * 刪除表
     *
     * @param tableName 表名稱
     * @throws IOException
     */
    public void dropTable(String tableName) throws IOException {
        //判斷表是否存在
        if (!isTableExist(tableName)) {
            System.out.println(tableName + "表不存在");
            return;
        }
        //使表下線
        admin.disableTable(TableName.valueOf(tableName));
        //刪除表
        admin.deleteTable(TableName.valueOf(tableName));

    }

    /**
     * 創建命名空間
     *
     * @param ns 命名空間
     */
    public void createNameSpace(String ns) {
        //創建命名空間描述器
        NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create(ns).build();
        try {
            admin.createNamespace(namespaceDescriptor);
            // 列出所有存在的命名空間
//             NamespaceDescriptor[] namespaceDescriptors = admin.listNamespaceDescriptors();
//            for (NamespaceDescriptor descriptor : namespaceDescriptors) {
//                String name = descriptor.getName();
//                System.out.println(name+ "------------------");
//            }
        } catch (NamespaceExistException e) {
            System.out.println("命名空間" + ns + "已經存在");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 列出所有的命名空間
     *
     * @return 命名空間列表
     * @throws Exception
     */
    List<String> getAllNamespaces() throws Exception {
        NamespaceDescriptor[] namespaceDescriptors = admin.listNamespaceDescriptors();
        List<String> namespaceList = new ArrayList<>();
        for (NamespaceDescriptor descriptor : namespaceDescriptors) {
            namespaceList.add(descriptor.getName());
        }
//        for (String namespace : namespaceList) {
//            System.out.println("namespace: " + namespace);
//        }
        return namespaceList;
    }

    /**
     * 獲取所有的表名稱 支持正則
     *
     * @return list 的表名稱
     * @throws Exception
     */
    private List<String> getAllTables() throws Exception {

        NamespaceDescriptor[] namespaceDescriptors = admin.listNamespaceDescriptors();
        // 可以進行正則匹配
        HTableDescriptor[] hTableDescriptors = admin.listTables();
        List<String> allTableNames = new ArrayList<>();
        for (HTableDescriptor hTableDescriptor : hTableDescriptors) {
            // 獲取名稱
            allTableNames.add(hTableDescriptor.getNameAsString());
        }

        return allTableNames;
    }

    /****
     * 使用scan查詢所有數據
     * @param tableName
     */
    Map<String, Map<String, String>> getPageData(String tableName, int pageNum, int pageSize) throws IOException {

        final long l1 = System.currentTimeMillis();
        int i = 0;
        HTable table = new HTable(configuration, Bytes.toBytes(tableName));
        Map<String, Map<String, String>> map = new HashMap<>();
        HashMap<String, String> familyName = new HashMap<>();
        try {

            // 展示所有的列簇 可省略
            HTableDescriptor hTableDescriptor = table.getTableDescriptor();
            for (int ii = 0; ii < hTableDescriptor.getColumnFamilies().length; ii++) {
                familyName.put("familyName_" + ii, hTableDescriptor.getColumnFamilies()[ii].getNameAsString());
            }

            //Scan所有數據
            Scan scan = new Scan();

            Integer firstPage = pageNum * pageSize;
            Integer endPage = pageNum * (pageSize + 1);

            scan.setMaxVersions(100);

            ResultScanner rss = table.getScanner(scan);
            loop:
            while (rss.next() != null) {

                for (KeyValue kv : rss.next().raw()) {

                    if (i >= firstPage && i < endPage) {

                        HashMap<String, String> valueMap = new HashMap<>();

                        valueMap.put("rowKey", new String(kv.getRow()));
                        valueMap.put("family", new String(kv.getFamily()));
                        valueMap.put("qualifier", new String(kv.getQualifier()));
                        valueMap.put("timestamp", String.valueOf(kv.getTimestamp()));
                        valueMap.put("value", new String(kv.getValue()));
                        map.put(kv.getRow() + "_" + String.valueOf(kv.getTimestamp()), valueMap);
                    }
                    i++;
                    if (i >= endPage) {
                        break loop;
                    }
                }
            }


            rss.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        map.put("familyName", familyName);

        i = 0;
        System.out.println("time" + (System.currentTimeMillis() - l1));
        return map;
    }

    /**
     * 獲取某個nameSpace下的所有tableName
     *
     * @param nameSpace 命名空間
     * @return 表名
     * @throws Exception
     */
    List<String> getAllTableNamesOfNameSpace(String nameSpace) throws Exception {
        HTableDescriptor[] hTableDescriptors = admin.listTableDescriptorsByNamespace(nameSpace);

        List<String> tableNames = new ArrayList<>();
        for (HTableDescriptor hTableDescriptor : hTableDescriptors) {
            tableNames.add(hTableDescriptor.getNameAsString());
        }

        return tableNames;
    }


    /**
     * 查詢數據
     *
     * @param tableName    表名稱
     * @param rowKey       行鍵
     * @param columnFamily 列簇名稱
     * @param column       列名
     * @throws IOException
     */
    public void getData(String tableName, String rowKey, String columnFamily, String column) throws IOException {
        //獲取表名稱
        Table table = connection.getTable(TableName.valueOf(tableName));
        //創建get對象
        // 將每個rowKey傳遞進去進行 查詢
        final byte[] row = Bytes.toBytes(rowKey);

        Get get = new Get(row);


        get.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column));
        get.setMaxVersions(100);


        Result result = table.get(get);
        String value;
        String rowkey;
        String qualifierName;
        String familyName;
        long timestamp;
        final List<Cell> cells = result.listCells();
        for (Cell cell : cells) {

            value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
            rowkey = Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
            qualifierName = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
            familyName = Bytes.toString(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
            timestamp = cell.getTimestamp();
            // rowKey
            System.out.println("rowkey: " + rowkey);

            // 列簇
            System.out.println("familyName: " + familyName);

            // 列名
            System.out.println("qualifierName: " + qualifierName);

            // 值
            System.out.println("value: " + value);

            // 時間戳 版本
            System.out.println("timestamp: " + timestamp);
        }
    }

    public List<String> queryTableTestBatch(List<String> rowkeyList) throws IOException {
        List<Get> getList = new ArrayList();
        String tableName = "student1";
        List<String> list = new ArrayList<>();
        Table table = connection.getTable(TableName.valueOf(tableName));// 獲取表
        for (String rowkey : rowkeyList) {//把rowkey加到get裏,再把get裝到list中
            final byte[] row = Bytes.toBytes(rowkey);
            final Get get = new Get(row);
            get.setMaxVersions(100);
            getList.add(get);
        }
        final Result[] results = table.get(getList);//重點在這,直接查getList<Get>
        for (Result result : results) {//對返回的結果集進行操作
            for (Cell cell : result.rawCells()) {
                String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
                String rowkey = Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
                String qualifierName = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
                String familyName = Bytes.toString(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
                long timestamp = cell.getTimestamp();
                System.out.println("rowkey: " + rowkey);
                System.out.println("familyName: " + familyName);
                System.out.println("qualifierName: " + qualifierName);
                System.out.println("value: " + value);
                System.out.println("timestamp: " + timestamp);
                list.add(value);
            }
        }
        return list;
    }


    /**
     * 獲取一個表名的所有原始信息
     *
     * @param tableName 表名稱
     * @throws Exception
     */
    public void getNoDealData(String tableName) throws Exception {
        Table table = connection.getTable(TableName.valueOf(tableName));
        Scan scan = new Scan();
        scan.setMaxVersions(10);
        ResultScanner scanner = table.getScanner(scan);
        for (Result result : scanner) {
            System.out.println("scan: " + result);
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章