Filtering away Records Containing Null during Data Retrieval
【Question】
I have an array list of records that is from an input csv file. Some records have full columns but some have null. For example:
abc, 1441652452, 8.64015, 52.75034
abc, 1442279677,,
abc, 1442280570, 10.44255,148.78166
Anyways, there could be more than one line of incomplete records consecutively. If you look at the records, at position [2] and [3] lat and longs are being stored accordingly. What i want to do is to calculate the distance between (in the case of my example above) the third record and the fist record. (I already have the formula to calculate the distance between latlngs. Currently, this is what my code consists of:
for (int i = 0; i < oneUserRecord.size(); i++) {
String currentRecord = oneUserRecord.get(i);
String[] current = currentRecord.split(",");
double currentLat = Double.parseDouble(current[3]);
double currentLng = Double.parseDouble(current[4]);
}
What I initially planned to do was to retrieve the next record, and extract the next record's lat and long, and then find out the distance between the next and the current records' lat and longs. However, because of the incomplete records I thus won't be able to extract positions [3] and [4] simply because they don't exist in those strings. So, my question simply put is, how do I check whether positions [3] and [4] of the next record is empty, if it is empty, how do I assign the next record to be the following row of record to have a value for lat and long?
I had a logic (continuing from the code above),
String nextRecord = "";
if ((oneUserRecord.get(i++).split(",")[3]).isEmpty()
|| (oneUserRecord.get(i++).split(",")[4]).isEmpty()) {
nextRecord = oneUserRecord.get(i++);
}
else {
nextRecord = oneUserRecord.get(i + 1);
}
String[] next = nextRecord.split(",");
double nextLat = Double.parseDouble(next[3]);
double nextLng = Double.parseDouble(next[4]);
But it didn't work, still got an out of bounds error. Is there a way I can improve on this, or another method for me to get the result I want?
【Answer】
First you can filter away the lines containing null. This will save you from comparing a line with its next neighbor. This can be done in SPL and the script can be embedded in a Java application for further processing. Here’s the SPL script:
ResultSet rs = st.executeQuery("=file("d:\\source.csv").import@c().select(#3==null || #4==null)"
An SPL script returns a standard ResultSet object for JDBC, which can be traversed with Java code.
SPL Official Website 👉 https://www.scudata.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProcSPL
SPL Learning Material 👉 https://c.scudata.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/cFTcUNs7
Youtube 👉 https://www.youtube.com/@esProc_SPL