導出HBase數據到Excel(Java代碼)

1、主要代碼java

 

複製代碼

package com.sgcc.mcsas.bigdata.tool;

import com.sgcc.mcsas.bigdata.service.HBaseServiceImpl;
import com.sgcc.mcsas.bigdata.service.IHBaseService;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.client.Result;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dwxx-120 on 2016/7/12.
 */
public class Export {

    private static IHBaseService service = new HBaseServiceImpl();
    private static final int count = 5;//記錄數
    private static final String fileName = "D:/hbase_export.xls";

    public static void exportExcel(List<String> tableNames) throws Exception {
        //建立excel文件
        File file = new File(fileName);
        if (!file.exists()) {
            file.createNewFile();
        }

        //設定輸出流
        FileOutputStream fos = new FileOutputStream(file);
        HSSFWorkbook book = new HSSFWorkbook();

        for (int k = 0; k < tableNames.size(); k++) {
            String tableName = tableNames.get(k);
            //取部分數據
            List<Result> results = service.getTopSomeRecords(tableName, "05M", count);

            //建立sheet
            HSSFSheet sheet = book.createSheet();
            book.setSheetName(k, tableName.replace(":", "_"));

            //爲空返回
            if (results.size() == 0) {
                System.out.println(tableName + " has no data!");
                continue;
            }

            //生成表頭
            HSSFRow header = sheet.createRow(0);
            HSSFCell header_rowkey = header.createCell(0);

            //rowkey表頭
            header_rowkey.setCellValue("ROWKEY");
            //其餘表頭
            List<Cell> cells0 = results.get(0).listCells();
            for (int i = 0; i < cells0.size(); i++) {
                HSSFCell header_other = header.createCell(i + 1);
                String name = new String(cells0.get(i).getQualifier());
                header_other.setCellValue(name);
            }

            //遍歷查詢的數據
            for (int i = 0; i < results.size(); i++) {
                //一個Result建立一行
                HSSFRow data_row = sheet.createRow(i + 1);
                Result r = results.get(i);

                //設置rowkey的值
                String rowkey = new String(r.getRow());
                HSSFCell data_rowkey = data_row.createCell(0);
                data_rowkey.setCellValue(rowkey);

                //設置其餘值
                List<Cell> cellList = r.listCells();
                for (int j = 0; j < cellList.size(); j++) {
                    HSSFCell data_other = data_row.createCell(j + 1);
                    data_other.setCellValue(new String(cellList.get(j).getValue()));
                }
            }
        }
        //寫入
        book.write(fos);
    }

    public static void main(String args[]) throws Exception {
        //HBase表名
        List<String> list = new ArrayList<String>();
        list.add("mcsas:zxjc_aeolianvibration");
        list.add("mcsas:zxjc_airmoisture");
        list.add("mcsas:zxjc_airpresure");
        list.add("mcsas:zxjc_brakecable");
        list.add("mcsas:zxjc_chargeelectricity");
        list.add("mcsas:zxjc_electromotorstate");
        list.add("mcsas:zxjc_gasinoil");
        list.add("mcsas:zxjc_gispartdischarge");
        list.add("mcsas:zxjc_icethickness");
        list.add("mcsas:zxjc_ideoimage");
        list.add("mcsas:zxjc_ironcoreelectricity");
        list.add("mcsas:zxjc_lightningrod");
        list.add("mcsas:zxjc_linegalloping");
        list.add("mcsas:zxjc_linesag");
        list.add("mcsas:zxjc_linetemperature");
        list.add("mcsas:zxjc_oillevel");
        list.add("mcsas:zxjc_partdischarge");
        list.add("mcsas:zxjc_pollutiondegree");
        list.add("mcsas:zxjc_spectrum");
        list.add("mcsas:zxjc_substationweather");
        list.add("mcsas:zxjc_tinywater");
        list.add("mcsas:zxjc_topoiltemperature");
        list.add("mcsas:zxjc_towerinclination");
        list.add("mcsas:zxjc_weather");
        list.add("mcsas:zxjc_windageyaw");

        exportExcel(list);
    }
}

複製代碼

 

2、HBase取數據主要代碼(getTopSomeRecords)apache

複製代碼

/**
     * 獲取指定位置向下N條記錄
     */
    public List<Result> getTopSomeRecords(String tableName, String startRow,
                                           Integer count) throws HBaseException {
        Connection connection = null;
        Table hTable = null;
        try {
            connection = ConnectionFactory.createConnection(conf);
            hTable = connection.getTable(TableName.valueOf(tableName));
            Scan scan = new Scan();
            List<Result> results = new ArrayList<Result>();

            scan.setCacheBlocks(true);
            scan.setCaching(10000);
            scan.setStartRow(Bytes.toBytes(startRow));

            PageFilter filter = new PageFilter(count);
            scan.setFilter(filter);

            ResultScanner scanner = hTable.getScanner(scan);

            for (Result r : scanner) {
                results.add(r);
            }

            scanner.close();
            return results;
        } catch (IOException e) {
            e.printStackTrace();
            logger.error("獲取數據出錯: ", e);
            throw new HBaseException(HBaseException.UNKNOW_ERROR, "獲取數據出錯: ", e);
        } finally {
            closeHTable(hTable);
            closeConn(connection);
        }
    }

複製代碼

3、備註ide

一、本文中一個表對應一個sheet,最終只有一個Excel文件,裏面含有多個sheetoop

二、本文只是取部分數據,將數據先存入List中,若是數據量太大,會致使內存溢出,另外Excel也不可能存放太多的數據,建議只取部分數據spa

相關文章
相關標籤/搜索