java查询hbase导出文件_导出HBase数据到Excel(Java代码)

packagecom.sgcc.mcsas.bigdata.tool;importcom.sgcc.mcsas.bigdata.service.HBaseServiceImpl;importcom.sgcc.mcsas.bigdata.service.IHBaseService;importorg.apache.hadoop.hbase.Cell;importorg.apache.hadoop.hbase.client.Result;importorg.apache.poi.hssf.usermodel.HSSFCell;importorg.apache.poi.hssf.usermodel.HSSFRow;importorg.apache.poi.hssf.usermodel.HSSFSheet;importorg.apache.poi.hssf.usermodel.HSSFWorkbook;importjava.io.File;importjava.io.FileOutputStream;importjava.util.ArrayList;importjava.util.List;/*** Created by dwxx-120 on 2016/7/12.*/

public classExport {private static IHBaseService service = newHBaseServiceImpl();private static final int count = 5;//记录数

private static final String fileName = "D:/hbase_export.xls";public static void exportExcel(List tableNames) throwsException {//创建excel文件

File file = newFile(fileName);if (!file.exists()) {

file.createNewFile();

}//设定输出流

FileOutputStream fos = newFileOutputStream(file);

HSSFWorkbook book= newHSSFWorkbook();for (int k = 0; k < tableNames.size(); k++) {

String tableName=tableNames.get(k);//取部分数据

List 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 cells0 = results.get(0).listCells();for (int i = 0; i < cells0.size(); i++) {

HSSFCell header_other= header.createCell(i + 1);

String name= newString(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 = newString(r.getRow());

HSSFCell data_rowkey= data_row.createCell(0);

data_rowkey.setCellValue(rowkey);//设置其他值

List cellList =r.listCells();for (int j = 0; j < cellList.size(); j++) {

HSSFCell data_other= data_row.createCell(j + 1);

data_other.setCellValue(newString(cellList.get(j).getValue()));

}

}

}//写入

book.write(fos);

}public static void main(String args[]) throwsException {//HBase表名

List list = new ArrayList();

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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值