Reply
Highlighted
Explorer
Posts: 16
Registered: ‎08-13-2013

Jasper Reports with cloudera using PHP

Hello,

 

I am trying to connect Hive jasper report with my PHP Application. My hiveserver is running on 192.168.3.101:10001.

 

Here is my program which connect hive with PHP application:

 

<?php
require_once("Jasper/java/Java.inc");

class JasperReports {

public function report_generator($jrxml, $arr) {
$format = $arr['format'];
try {

$jasperxml = new java("net.sf.jasperreports.engine.xml.JRXmlLoader");
$jasperDesign = $jasperxml->load(realpath($jrxml));
/*$query = new java("net.sf.jasperreports.engine.design.JRDesignQuery");
$query->setText($sql);
$jasperDesign->setQuery($query);*/
$compileManager = new JavaClass("net.sf.jasperreports.engine.JasperCompileManager");
$report = $compileManager->compileReport($jasperDesign);
} catch (JavaException $ex) {
print $ex;
}

$fillManager = new JavaClass("net.sf.jasperreports.engine.JasperFillManager");

$params = new Java("java.util.HashMap");
foreach ($arr as $key => $value) {
$params->put($key, $value);
}

 

$class = new JavaClass("java.lang.Class");
$class->forName("org.apache.hadoop.hive.jdbc.HiveDriver");
$driverManager = new JavaClass("java.sql.DriverManager");

 

//db username and password
$conn = $driverManager->getConnection("jdbc:hive://192.168.3.101:10001/default", "", "");
$jasperPrint = $fillManager->fillReport($report, $params, $conn);

$exporter = new java("net.sf.jasperreports.engine.JRExporter");

 

switch ($format) {
case 'xls':
$outputPath = realpath(".") . "\\" . "report.xls";

try {
$exporter = new java("net.sf.jasperreports.engine.export.JRXlsExporter");
$exporter->setParameter(java("net.sf.jasperreports.engine.export.JRXlsExporterParameter")->IS_ONE_PAGE_PER_SHEET, java("java.lang.Boolean")->TRUE);
$exporter->setParameter(java("net.sf.jasperreports.engine.export.JRXlsExporterParameter")->IS_WHITE_PAGE_BACKGROUND, java("java.lang.Boolean")->FALSE);
$exporter->setParameter(java("net.sf.jasperreports.engine.export.JRXlsExporterParameter")->IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, java("java.lang.Boolean")->TRUE);
$exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->JASPER_PRINT, $jasperPrint);
$exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->OUTPUT_FILE_NAME, $outputPath);
} catch (JavaException $ex) {
print $ex;
}

header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=report.xls");
break;
case 'csv':
$outputPath = realpath(".") . "\\" . "report.csv";

try {
$exporter = new java("net.sf.jasperreports.engine.export.JRCsvExporter");
$exporter->setParameter(java("net.sf.jasperreports.engine.export.JRCsvExporterParameter")->FIELD_DELIMITER, ",");
$exporter->setParameter(java("net.sf.jasperreports.engine.export.JRCsvExporterParameter")->RECORD_DELIMITER, "\n");
$exporter->setParameter(java("net.sf.jasperreports.engine.export.JRCsvExporterParameter")->CHARACTER_ENCODING, "UTF-8");
$exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->JASPER_PRINT, $jasperPrint);
$exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->OUTPUT_FILE_NAME, $outputPath);
} catch (JavaException $ex) {
print $ex;
}

header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=report.csv");
break;
case 'docx':
$outputPath = realpath(".") . "\\" . "report.docx";

try {
$exporter = new java("net.sf.jasperreports.engine.export.ooxml.JRDocxExporter");
$exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->JASPER_PRINT, $jasperPrint);
$exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->OUTPUT_FILE_NAME, $outputPath);


} catch (JavaException $ex) {
print $ex;
}

header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment; filename=report.docx");
break;
case 'html':
$outputPath = realpath(".") . "\\" . "report.html";

try {
$exporter = new java("net.sf.jasperreports.engine.export.JRHtmlExporter");
$exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->JASPER_PRINT, $jasperPrint);
$exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->OUTPUT_FILE_NAME, $outputPath);


} catch (JavaException $ex) {
print $ex;
}
break;
case 'pdf':
$outputPath = realpath(".") . "\\" . "XeReport.pdf";

$exporter = new java("net.sf.jasperreports.engine.export.JRPdfExporter");
$exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->JASPER_PRINT, $jasperPrint);
$exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->OUTPUT_FILE_NAME, $outputPath);

header("Content-type: application/pdf");
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-Disposition: inline; filename=report.pdf");
break;
case 'ods':
$outputPath = realpath(".") . "\\" . "report.ods";

try {
$exporter = new java("net.sf.jasperreports.engine.export.oasis.JROdsExporter");
$exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->JASPER_PRINT, $jasperPrint);
$exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->OUTPUT_FILE_NAME, $outputPath);
} catch (JavaException $ex) {
print $ex;
}

header("Content-type: application/vnd.oasis.opendocument.spreadsheet");
header("Content-Disposition: attachment; filename=report.ods");
break;
case 'odt':
$outputPath = realpath(".") . "\\" . "report.odt";

try {
$exporter = new java("net.sf.jasperreports.engine.export.oasis.JROdtExporter");
$exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->JASPER_PRINT, $jasperPrint);
$exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->OUTPUT_FILE_NAME, $outputPath);
} catch (JavaException $ex) {
print $ex;
}

header("Content-type: application/vnd.oasis.opendocument.text");
header("Content-Disposition: attachment; filename=report.odt");
break;
case 'txt':
$outputPath = realpath(".") . "\\" . "report.txt";

try {
$exporter = new java("net.sf.jasperreports.engine.export.JRTextExporter");
$exporter->setParameter(java("net.sf.jasperreports.engine.export.JRTextExporterParameter")->PAGE_WIDTH, 120);
$exporter->setParameter(java("net.sf.jasperreports.engine.export.JRTextExporterParameter")->PAGE_HEIGHT, 60);
$exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->JASPER_PRINT, $jasperPrint);
$exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->OUTPUT_FILE_NAME, $outputPath);
} catch (JavaException $ex) {
print $ex;
}

header("Content-type: text/plain");
break;
case 'rtf':
$outputPath = realpath(".") . "\\" . "report.rtf";

try {
$exporter = new java("net.sf.jasperreports.engine.export.JRRtfExporter");
$exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->JASPER_PRINT, $jasperPrint);
$exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->OUTPUT_FILE_NAME, $outputPath);
} catch (JavaException $ex) {
print $ex;
}

header("Content-type: application/rtf");
header("Content-Disposition: attachment; filename=report.rtf");
break;
case 'pptx':
$outputPath = realpath(".") . "\\" . "report.pptx";
try {
$exporter = new java("net.sf.jasperreports.engine.export.ooxml.JRPptxExporter");
$exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->JASPER_PRINT, $jasperPrint);
$exporter->setParameter(java("net.sf.jasperreports.engine.JRExporterParameter")->OUTPUT_FILE_NAME, $outputPath);
} catch (JavaException $ex) {
print $ex;
}

header("Content-type: aapplication/vnd.ms-powerpoint");
header("Content-Disposition: attachment; filename=report.pptx");
break;
}
$exporter->exportReport();

readfile($outputPath);
//unlink($outputPath);
}
}


?>

 

When I am trying to run above program, it throws error:

 

Fatal error: Uncaught [[o:Exception]:"java.lang.Exception: Invoke failed: [[c:JasperFillManager]]->fillReport((o:JasperReport)[o:JasperReport], (i:Map)[o:HashMap], (i:Connection)[o:HiveConnection]). Cause: java.lang.ClassCastException: org.apache.hadoop.hive.jdbc.HiveConnection cannot be cast to com.jaspersoft.hadoop.hive.connection.HiveConnection VM: 1.7.0_13@http://java.oracle.com/" at: #-20 com.jaspersoft.hadoop.hive.query.HiveQueryExecuter.<init>(HiveQueryExecuter.java:48) #-19 com.jaspersoft.hadoop.hive.query.HiveQueryExecuterFactory.createQueryExecuter(HiveQueryExecuterFactory.java:49) #-18 net.sf.jasperreports.engine.util.JRQueryExecuterUtils$WrappingQueryExecuterFactory.createQueryExecuter(JRQueryExecuterUtils.java:155) #-17 net.sf.jasperreports.engine.fill.JRFillDataset.createQueryDatasource(JRFillDataset.java:1083) #-16 net.sf.jasperreports.engine.fill.JRFillDataset.initDatasource(JRFillDataset.java:667) #-15 net.sf.jasperreports.engine.fill.JRBaseFiller.setParameters(JRBaseFiller.java:1258) #-14 net.sf.jasperre in D:\workspace\sdp\Jasper\java\Java.inc on line 195

 

I can't understand why I am getting this error.Please let me know whether I am going wrong somewhere in jars or anything wrong in my program.

 

I have com-jaspersoft-ireport-haddop-hive.jar in my Tomcat(webapps/lib) folder. When I run this program it makes a connection with cloudera but can't get ouput & throws some error in fillReport statement.

 

Please let me know that either I have to add some more jar or make some changes in my code.

 

Thanks & Regards 

Surbhi SIngh

Posts: 1,903
Kudos: 435
Solutions: 307
Registered: ‎07-31-2013

Re: Jasper Reports with cloudera using PHP

Does Jasper Reports use HS2 or HS1 drivers? Both classes may be called "HiveConnection" but their implementation and package differs. Perhaps its only supportive of HS2's HiveConnection, and a HS1 JDBC class is being passed to it instead.

The class cast error comes from Jasper's components, so it may even be better to ask the Jasper Reports community on what they support and what your problem may be.