After retrieving JSON from ES Kafka Mongodb Restful...
Tags: json, java, webdev, programming
Published: 2024-02-24 13:12:56

JSON is a good thing that can carry rich structured data information in a common text format. Many modern technologies prefer to use JSON as a data transmission format, such as Elastic Search, Restful, Kafka, etc. Mongodb, which is more concerned about performance, uses binary JSON.

-----

Structured data is often in bulk and often requires recalculation.

However, JSON related class libraries are not very convenient to use for calculations. JSONpath is fine to parse JSON, but it doesn’t have much computing power. Simple filtering and aggregation are fine, but it cannot handle slightly complex operations such as grouping and summarization. Basically, you have to hard code yourself.

Write it into the database to calculate? It is too heavy. Moreover, JSON often has multiple layers of structured data, and writing it into a relational database requires creating several associated tables, which makes the cost of loading into a database much higher than the calculation itself.

-----

esProc SPL will help you.

esProc SPL is an open source computing engine developed purely in Java, and it is here https://github.com/SPLWare/esProc.

-----

esProc SPL encapsulates json library, and it can parse JSON text into a computable SPL table sequence (SPL’s in-memory structured data object) in just one line:

<table> 
 <thead> 
  <tr>
   <th> </th>
   <th> A</th>
  </tr> 
 </thead> 
 <tbody> 
  <tr>
   <td> 1 </td>
   <td> =file("d:\xml\emp_orders.json").read()</td>
  </tr> 
  <tr>
   <td> 2 </td>
   <td> =json(A1)</td>
  </tr> 
 </tbody> 
</table> 

The SPL table sequence naturally has a multi-layer structure, which means that the field values can be another table sequence, which is naturally in line with JSON’s structure:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ft520p8n9u81q00q4752.png)

Once converted into an SPL table sequence, the calculation itself is a strength of esProc. Filtering, grouping, and join are never a problem, and most of the calculation objectives can be implemented in one line:

```
Filter：T.select(Amount>1000 && Amount<=3000 && like(Client,"*s*"))
Sort：T.sort(Client,-Amount)
Distinct：T.id(Client)
Group：T.groups(year(OrderDate);sum(Amount))
Join：join(T1:O,SellerId; T2:E,EId)
TopN：T.top(-3;Amount)
TopN in group：T.groups(Client;top(3,Amount))
```

There are many of these contents, and we won’t expand on them here. Interested friends can refer to the relevant materials on the esProc SPL official website.

eSProc SPL has encapsulated many common access interfaces for JSON data sources.

Restful: Plain text JSON, and it can generate JSON text back after calculation

<table> 
 <thead> 
  <tr>
   <th> </th>
   <th> A</th>
  </tr> 
 </thead> 
 <tbody> 
  <tr>
   <td> 1 </td>
   <td> =httpfile("http://127.0.0.1:6868/restful/emp_orders").read()</td>
  </tr> 
  <tr>
   <td> 2 </td>
   <td> =json(A1)</td>
  </tr> 
  <tr>
   <td> 3 </td>
   <td> =A2.conj(Orders).select(Amount&gt;1000 &amp;&amp; Amount&lt;=2000 &amp;&amp; like@c(Client,"*business*"))</td>
  </tr> 
  <tr>
   <td> 4 </td>
   <td> =json(A3)</td>
  </tr> 
 </tbody> 
</table> 

Elastic Search: it can directly write JSON constants in SPL code and participate in transmission and calculation

<table> 
 <thead> 
  <tr>
   <th> </th>
   <th> A</th>
  </tr> 
 </thead> 
 <tbody> 
  <tr>
   <td> 1 </td>
   <td> &gt;apikey="Authorization:ApiKey a2x6aEF……KZ29rT2hoQQ=="</td>
  </tr> 
  <tr>
   <td> 2 </td>
   <td> '{<br>"counter" : 1,<br>"tags" : ["red"]<br>,"beginTime":"2022-01-03"<br>,"endTime":"2022-02-15"<br>}</td>
  </tr> 
  <tr>
   <td> 3 </td>
   <td> =es_rest("https://localhost:9200/index1/_doc/1", "PUT",A2;"Content-Type: application/x-ndjson",apikey)</td>
  </tr> 
  <tr>
   <td> 4 </td>
   <td> =json(A3.Content)</td>
  </tr> 
 </tbody> 
</table>
 
Mongodb: It is also OK for binary JSON

<table> 
 <thead> 
  <tr>
   <th> </th>
   <th> A</th>
  </tr> 
 </thead> 
 <tbody> 
  <tr>
   <td> 1 </td>
   <td> =mongo_open("mongodb://127.0.0.1:27017/mymongo")</td>
  </tr> 
  <tr>
   <td> 2 </td>
   <td> =mongo_shell(A1,"{'find':'orders',filter:{OrderID: {$gte: 50}},batchSize:100}")</td>
  </tr> 
  <tr>
   <td> 3 </td>
   <td> =A2.cursor.firstBatch.select(Amount&gt;1000 &amp;&amp; Amount&lt;=2000 &amp;&amp; like@c(Client,"*business*"))</td>
  </tr> 
  <tr>
   <td> 4 </td>
   <td> =mongo_close(A1)</td>
  </tr> 
 </tbody> 
</table> 

Kafka: SPL also encapsulates the interfaces for writing to these data sources, forming an IO loop

<table> 
 <thead> 
  <tr>
   <th> </th>
   <th> A</th>
  </tr> 
 </thead> 
 <tbody> 
  <tr>
   <td> 1 </td>
   <td> =kafka_open("/kafka/my.properties", "topic1")</td>
  </tr> 
  <tr>
   <td> 2 </td>
   <td> =kafka_poll(A1)</td>
  </tr> 
  <tr>
   <td> 3 </td>
   <td> =A2.derive(json(value):v).new(key, v.fruit, v.weight)</td>
  </tr> 
  <tr>
   <td> 4 </td>
   <td> =kafka_send(A1, "A100", json(A3))</td>
  </tr> 
  <tr>
   <td> 5 </td>
   <td> =kafka_close(A1)</td>
  </tr> 
 </tbody> 
</table> 

For Mongodb, Kafka and other data sources that may return large amounts of data, esProc SPL also provides cursor objects and methods that can read batch by batch and process while reading. We won’t provide detailed examples here. Interested friends can also go to the official website to check the information.

-----

Usually, JSON data does not exist independently, but also exchanges data with other data sources and performs mixed calculations. esProc SPL is certainly not invented only to deal with JSON, but it is a professional computing engine that can support a wide range of data sources:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xt88jzhovqlbif6fk8uh.png)

These data sources can all be read as table sequence and cursor by SPL, making it very easy to implement mixed computation and exchange data.

-----

Then, how can the code written in esProc SPL be integrated into the application?

Very simple, esProc provides a standard JDBC driver, allowing Java programs to call SPL code just like executing database SQL.


```
Class.forName("com.esproc.jdbc.InternalDriver");
Connection conn =DriverManager.getConnection("jdbc:esproc:local://");
Statement statement = conn.createStatement();
ResultSet result = statement.executeQuery("=json(file(\"Orders.csv\")).select(Amount>1000 && like(Client,\"*s*\")
```

More complex SPL scripts can be saved as files, just like calling stored procedures:

```
Class.forName("com.esproc.jdbc.InternalDriver");
Connection conn =DriverManager.getConnection("jdbc:esproc:local://");
CallableStatement statement = conn.prepareCall("call queryOrders()");
statement.execute();
```
-----

As pure Java developed software, esProc SPL can be seamlessly integrated into Java applications, just like the code written by application programmers themselves, enjoying the advantages of mature Java frameworks together. SPL itself has well-established process control statements, such as for loops and if branches, and also supports subroutine calls. Using only SPL can achieve very complex business logic, directly forming a complete business unit, without the need for upper-level Java code to cooperate. The main program simply calls the SPL script.

Store SPL scripts as files and place them outside of the main application program. Code modifications can be made independently and immediately take effect, unlike Java code that needs to be recompiled after code modifications, and the entire application needs to be shut down and restarted. This can achieve **hot swap of business logic**, especially suitable for supporting frequently changing businesses, which is also where JSON is widely used.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g8nf0qvk9w1kp610qpqc.png)