. Connecting to ElasticSearch in BIRT

1). Using BIRT with a Scripted Data Set

Here is a simple example of BIRT connecting to ElasticSearch using a Scriped Data Set. This could also illustrate connecting to a REST-based data source with a JSON result set.

The three events were added:

Data Source > Open

Make a query request to ElasticSearch.

Save the results in a JSON variable.

logger = java.util.logging.Logger.getLogger("birt.report.logger");
importPackage(Packages.java.io);
importPackage(Packages.java.net);

var inStream = new URL("http://localhost:9200/_search?q=title:the").openStream();
var inStreamReader = new InputStreamReader(inStream);
var bufferedReader = new BufferedReader(inStreamReader);
var line;
var result = "";
while ((line = bufferedReader.readLine()) != null)
	result += line;
inStream.close();

var json = JSON.parse(result);
vars["HTMLJSON"] = json;

Data Set > Open

Reset the row counter to zero

recNum = 0;

Data Set > Fetch

if (recNum >= vars["HTMLJSON"].hits.total)
	return false;

row["MovieTitle"] = vars["HTMLJSON"].hits.hits[recNum]._source.title;
row["MovieYear"] = vars["HTMLJSON"].hits.hits[recNum]._source.year;
row["MovieDirector"] = vars["HTMLJSON"].hits.hits[recNum]._source.director;

recNum++;
//logger.warning (row["hits"]);
return true;

Create a row of data from ElasticSearch for each of hits.

Set the output column field values from the JSON result set.

Stop when total hits are reached.

Place the helper JavaScript, “json.js”, into the root of the project folder.

Note: If you are using BIRT 4.5.0, it has built-in JSON support, so you don’t need the helper JavaScript file, “json.js”, in the root of your project folder. The difference between the two designs in the ZIP is that the one for pre-BIRT 4.5.0, there is an external Resources reference to “json.js”, and the one for 4.5.0 does not have it. If you don’t have it in pre-BIRT 4.5.0, when attempting to run the report, you will see an error like … ReferenceError: “JSON” is not defined.

2). Using esProc as a data source in BIRT

Here is the esProc SPL script.


A
1 =es_open("http://localhost:9200","user":"un1234")
2 =es_get(A1,"/_cat/health")
3 =es_post(A1,"/_all/_search?q=_id:5")

You want to use the data under hits in the report, add the expression in cell A4: = A3.hits.hits

imagepng

For more examples of parsing JSON, see JSON data calculation and importing into a database - esProc

Note: Download the ElasticSearch client jars (…elasticsearch-6.2.1.jar, elasticsearch-cli-6.2.1.jar, elasticsearch-core-6.2.1.jar, elasticsearch-rest-client-6.2.1.jar…) for connecting to the ElasticSearch server from elsticsearch official website and put them in esProc external library folder. The directory containing files of this external library is: [installation directory]\esProc\extlib\ElasticsearchCli. The Raqsoft core jar for this external library is elasticCli.jar

For detail SPL integration with BIRT, see How to Call an SPL Script in BIRT. If you have any questions or comments, please leave them below.