CreateOtherTable.java 5.09 KB
Newer Older
wang-jinfeng committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
package mobvista.prd.datasource.source.mapreduce;

import mobvista.prd.datasource.dao.ExportExcelDAO;
import mobvista.prd.datasource.dao.impl.ExportExcelDAOImpl;
import mobvista.prd.datasource.main.GenerateTagReport;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.*;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 生成月报表
 * Created by Administrator on 2017/6/16 0016.
 */
public class CreateOtherTable {
    private static final Logger log = Logger.getLogger(CreateOtherTable.class);

    private List<String> croessColumns = new ArrayList<>();
    private List<String> croessHeadColumns = new ArrayList<String>();
    private List<String> newAddColumns = new ArrayList<String>();
    private List<String> newAddHeadColumns = new ArrayList<String>();
    private List<String> dauColumns = new ArrayList<String>();
    private List<String> dauHeadColumns = new ArrayList<String>();

    private List<String> idsColumns = new ArrayList<>();
    private List<String> idsHeadColumns = new ArrayList<String>();

    public void init () {
        croessHeadColumns.add("数据源");
        croessHeadColumns.add("设备量");

        croessColumns.add("dimension_type2");
        croessColumns.add("video_value");


        newAddHeadColumns.add("数据源");
        newAddHeadColumns.add("设备量");

        newAddColumns.add("dimension_type2");
        newAddColumns.add("video_value");


        dauHeadColumns.add("数据源");
        dauHeadColumns.add("设备量");

        dauColumns.add("dimension_type2");
        dauColumns.add("video_value");


        idsHeadColumns.add("ID类型");
        idsHeadColumns.add("DAU");
        idsHeadColumns.add("新增");

        idsColumns.add("dimension_type2");
        idsColumns.add("dau");
        idsColumns.add("new");

    }

    public int run (String[] args) throws Exception {
        if (args.length < 2) {
            log.info("Usage: GenerateTagReport <date> <output>");
            return 1;
        }

        init();

        OutputStream out = null;
        try {
            String date = args[0];
            String output = args[1];
            Path reportFile = new Path(output);
            FileSystem fs = FileSystem.get(URI.create(output), new Configuration());
            out = fs.create(reportFile);
//            out = new FileOutputStream(output);

            ExportExcelDAO dao = new ExportExcelDAOImpl();
            List<Map<String, Object>> croessList = dao.findSourcesData(date, "DMP交叉数据");
            List<Map<String, Object>> newAddList = dao.findSourcesData(date, "DMP新增数据");
            List<Map<String, Object>> dauList = dao.findSourcesData(date, "DMP日活数据");
            List<Map<String, Object>> idsList = dao.findIdsData(date);


            HSSFWorkbook workbook = new HSSFWorkbook();
            buildExcel(workbook, "交叉数据", croessHeadColumns, croessColumns, croessList);
            buildExcel(workbook, "新增数据", newAddHeadColumns, newAddColumns, newAddList);
            buildExcel(workbook, "日活数据", dauHeadColumns, dauColumns, dauList);
            buildExcel(workbook, "各ID数据", idsHeadColumns, idsColumns, idsList);

            workbook.write(out);
        } catch (Exception e) {
            throw e;
        } finally {
            if (out != null) {
                out.close();
            }
        }

        return 0;
    }

    public void buildExcel(HSSFWorkbook workbook, String sheetName, List<String> headColumns, List<String> columns,
                           List<Map<String, Object>> dataColumns) {

        HSSFSheet sheet = workbook.createSheet(sheetName);
        sheet.setColumnWidth(0, 5000);
        sheet.setColumnWidth(1, 5000);
        sheet.setColumnWidth(2, 5000);
        sheet.setColumnWidth(3, 5000);

        HSSFFont headFont = workbook.createFont();
        headFont.setBold(true);
        headFont.setFontHeightInPoints((short) 12);

        HSSFCellStyle headStyle = workbook.createCellStyle();
        headStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        headStyle.setFont(headFont);

        HSSFRow head = sheet.createRow(0);
        for (int i = 0; i < headColumns.size(); i++) {
            HSSFCell cell = head.createCell(i);
            cell.setCellStyle(headStyle);
            cell.setCellValue(headColumns.get(i));
        }

        Map<String, Object> map = null;
        for (int i=0; i< dataColumns.size(); i++) {
            map = dataColumns.get(i);
            HSSFRow row = sheet.createRow(i + 1);
            for (int j=0; j<columns.size(); j++) {
                HSSFCell cell = row.createCell(j);
                cell.setCellValue(String.valueOf(map.get(columns.get(j))));
            }
        }
    }


    public static void main(String[] args) {
        try {
            new CreateOtherTable().run(args);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }
}