Skip to content

Integrate SPL in Android apps

esProcSPL edited this page Jul 11, 2024 · 1 revision

Structured data process is also needed in mobile apps. Android operating system supports JAVA, and this article introduces how to integrate SPL in Android apps for structured data calculation.

We illustrate two examples of SPL calculation with Android Studio, the commonly used android development tool:

  1. Read the text data in jars of the app installation package to calculate them and then display the results on the screen.

  2. Read the data file of JSON format from Internet to calculate them, display the results on the screen and meanwhile save the results in the phone.

1. Create an app and authorize it

First of all, execute Android Studio to create a new APP project spldemo, the package name of which is com.raqsoft.spldemo. The app needs be authorized to access the internet and save files to the phone, which will be involved in Example 2. You can skip the following step if your app requires no authorizations like those. Open the app/src/main/AndroidManifest.xml file of the project, and add the authorizations of accessing the internet, reading and writing the SD card:

The app needs the phone user’s agreement to execute, so a method of applying for the user’s authorization needs to be written to call onCreate in MainActivity.java.

The following authorization interface will show up when launching the app, and the app will have the authority to read and write the files in SD card after the user clicks “allow”.

(Please note: different screenshots may be shown in different language environments, and this is taken from an android mobile phone in Chinese)

2. Write the required files

2.1 SPL configuration files

The configuration files needs to be loaded to set a few environmental parameters before SPL starts to work. For more details of configuration files, please refer to related materials, and the raqsoftConfig.xml configuration files in this example are as follows:

<?xml version="1.0" encoding="UTF-8"?>
<Config Version="2">
    <Runtime>
        <DBList>
        </DBList>
        <Esproc>
            <charSet>UTF-8</charSet>
            <dfxPathList>
                <dfxPath></dfxPath>
            </dfxPathList>
            <dateFormat>yyyy-MM-dd</dateFormat>
            <timeFormat>HH:mm:ss</timeFormat>
            <dateTimeFormat>yyyy-MM-dd HH:mm:ss</dateTimeFormat>
            <bufSize>65536</bufSize>
            <localHost/>
            <localPort>0</localPort>
            <parallelNum>1</parallelNum>
            <cursorParallelNum>1</cursorParallelNum>
            <blockSize>1048576</blockSize>
            <nullStrings>nan,null,n/a</nullStrings>
            <fetchCount>9999</fetchCount>
        </Esproc>
        <Logger>
            <Level>INFO</Level>
        </Logger>
    </Runtime>
    <JDBC>
          <Load></Load> 
    </JDBC>
</Config>

2.2 Example 1

This example reads the data of the text file and returns the results.

Write the required text file scores.txt, and some of the data are:

Write the SPL script calcLocalTxt.dfx as follows:

A B
1 =file@s("scores.txt").import@t() //read the text data as a sequence table
2 =A1.groups(Class;avg(English):English_avg, max(Maths):Maths_max) //group them by class, and count the average English grades and the highest math scores of each class
3 return A2 //return the results of A2

The @s option in A1 means to find scores.txt file in classpath of JAVA firstly, for this file will be put in jars.

2.3 Example 2

Read the text data in JSON format from Internet, then filter and sort them. Write the results in text files on the SD card and return the calculation results.

Write the SPL script calcHttpJson.dfx to which set a parameter orderAmount:

A B
1 =httpfile("http://service.raqsoft.com.cn/order.json").read() //read the http file data as a string
2 =json(A1) //convert the string in JSON format to a sequence table
3 =A2.select(Amount>orderAmount).sort(-Amount) //select the orders whose amounts are larger than the orderAmount parameter, and sort them in descendant order
4 =file("orders.csv").export@tc(A3) //export the results to orders.csv file
5 return A3 //return the results of A3

The sequence table of A2 is as follows:

Finally, package the above configurations, data, and scripts in a jar file named spldemo.jar:

3. Deploy jars required by SPL

Select the jars in the lib directory of the esProc install root directory, and copy them to libs directory in the APP project. SPL has many jars which does not need to be all copied. Apart from necessary esproc-bin-xxxx.jar, other jars are selected based on the functionality of your app. For the functionality of jars, please refer to related materials of SPL. This example only needs to copy json-1.0.0.jar file and spldemo.jar of the app because of JSON data.

4. Write APP program

The following program is written in MainActivity.java.

4.1 Initialization of SPL

Write initSPL, the initialization method of SPL, and call it in onCreate.

The third line loads the SPL configuration files, and the forth line gets the destination path of the app storing files in SD card. In sixth line, the path is set as the main path of SPL, which is relative to the relative path of the file function in SPL script. For example, file("orders.csv") in Example 2 stores orders.csv in the main path.

4.2 The method of executing dfx script

Write executeDfx to call SPL JDBC for script execution and return the java.sql.ResultSet object. This example converts the object into text strings to show them on the screen. Your app can return and operate ResultSet afterward as needed, for example, getting the data to operate other calculations, generating html pages, drawing a data table or figure and so on.

The method parameter dfx is the name of the script file (without extension), and params, which are listed in the order of parameters, are the parameter values of the script. The resultSetToString method converts the object of result set ResultSet into a string.

It is not necessary to write dfx script when invoking SPL. SPL statements can be directly used when the code is not complex, for instance:

Statement st = con.createStatement ();

ResultSet rs = st.executeQuery("=file@s("scores.txt").import@t().groups(Class;avg(English):English_avg, max(Maths):Maths_max)");

You can even perform SQL queries on the data file as follows:

ResultSet rs = st.executeQuery("select Class, avg(English) as English_avg, max(Maths) as Maths_max from {file@s("scores.txt").import@t()} group by Class");

4.3 Interface and functionality design

Place two buttons on the top of the screen, below which place a multi-line text widget to show the calculation results:

LOCAL TXT button execute Example 1, and its method of click event is calcLocalText:

HTTP JSON button execute Example 2, and its method of click event is calcHttpJson:

The script of Example 2 has a parameter which is set as 1,000 in this method.

4.4 APP execution

Execute the app and click LOCAL TXT button, which results in:

Click HTTP JSON button, and the results (in landscape) are as:

You can see in the file manager that orders.csv file are generated in main path.

(Please note: different screenshots may be shown in different language environments, and this is taken from an android mobile phone in Chinese)

Clone this wiki locally