Let’s make a simple CloudTurbine sink application to read data previously output by CTsource. You may want to reference the CloudTurbine Javadoc documentation as you work through this tutorial. Let’s assume the source is named CTsource and it is located under a CTdata directory; that is, at ./CTdata/CTsource. CTsource contains 100 seconds worth of data at 1 second intervals on each of three channels: “c0”, “c1.f32” and “c2.txt”. Our simple sink class will read all data from the “c1.f32” channel (single precision floats) and “c2.txt” channel (text).
Start off by importing the CloudTurbine classes (found in “CTlib.jar”).
0 1 2 |
import cycronix.ctlib.*; |
Define variables containing the root folder name (CTdata) and the name of the CloudTurbine source (CTsource, located under the CTdata root folder). Create a CTreader object which points to the root folder.
0 1 2 3 4 5 |
String rootFolder = "CTdata"; String sourceName = "CTsource"; CTreader ctr = new CTreader(rootFolder); |
All of the available channel names in a source can be obtained using the listChans method.
0 1 2 3 4 5 |
ArrayList<String> chanNames = ctr.listChans(sourceName); for (String chan : chanNames) { System.err.println("Found channel: " + chan); } |
The oldest and newest times for any data in the source are obtained as follows. Times are represented as seconds since epoch (midnight on January 1, 1970).
0 1 2 3 4 |
double oldestTime = ctr.oldTime(sourceName); double newestTime = ctr.newTime(sourceName); System.err.println("\noldest time = " + oldestTime + ", newest time = " + newestTime); |
Do the following to get all data on channel “c1.f32”. Similar code is used to get the string data for channel “c2.txt”.
0 1 2 3 4 5 6 |
CTdata data = ctr.getData(sourceName, "c1.f32", 0., (newestTime-oldestTime), "oldest"); double[] dt = data.getTime(); float[] dd = data.getDataAsFloat32(); System.err.println("\nData for channel c1.f32"); for (int i=0; i<dd.length; i++) System.err.println("time: " + String.format("%12.1f",dt[i]) + "\tdata: "+dd[i]); |
The call to getData used above deserves some attention. The method is shown below.
0 1 2 |
public CTdata getData(String source, String chan, double tget, double tdur, String tmode) |
The first two arguments are the names of the source and channel we want to fetch data from. The third argument, tget, is the data start time. The fourth argument, tdur, is the duration of data to fetch (typically seconds or milliseconds). The last argument, tmode, is the time reference, which defines how to use tget and tdur to fetch data. Tmode can be one of “oldest”, “newest”, “after”, or “absolute”. Let’s consider “oldest” (which our code uses) and “absolute”. First, consider the simplest case, “absolute”: tget must be the actual, absolute start time (typically seconds or milliseconds since epoch); getData will return data from tget to (tget+tdur). When the mode is “oldest”, tget is interpreted as a relative offset from the oldest data in the source. Let’s say a source’s data goes from epoch time 1488830000 to 1488831000. If we use mode=”oldest”, tget=100 and duration=200, the call to getData will return data in the range (oldest time + 100) = 1488830100 to (oldest time + 100 + 200) = 1488830300. In our code snippet above, since mode is “oldest”, tget is 0 and tdur is (newestTime-oldestTime), the call to getData will return data in the range (oldestTime+0) to (oldestTime+0+(newestTime-oldestTime)), in other words from oldestTime to newestTime, i.e. all the data!
The full listing for SimpleSink.java is provided below.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
import java.io.File; import java.util.ArrayList; import cycronix.ctlib.*; public class SimpleSink { public static void main(String[] args) { String rootFolder = "CTdata"; String sourceName = "CTsource"; CTreader ctr = new CTreader(rootFolder); // List channel names ArrayList<String> chanNames = ctr.listChans(sourceName); for (String chan : chanNames) { System.err.println("Found channel: " + chan); } // Get time range double oldestTime = ctr.oldTime(sourceName); double newestTime = ctr.newTime(sourceName); System.err.println("\noldest time = " + oldestTime + ", newest time = " + newestTime); // Get data on channel "c1.f32" CTdata data = ctr.getData(sourceName, "c1.f32", 0., (newestTime-oldestTime), "oldest"); double[] dt = data.getTime(); float[] dd = data.getDataAsFloat32(); System.err.println("\nData for channel c1.f32"); for (int i=0; i<dd.length; i++) System.err.println("time: " + String.format("%12.1f",dt[i]) + "\tdata: "+dd[i]); // Get data on channel "c2.txt" data = ctr.getData(sourceName, "c2.txt", 0., (newestTime-oldestTime), "oldest"); dt = data.getTime(); String[] strData = data.getDataAsString('s'); System.err.println("\nData for channel c2.txt"); for (int i=0; i<strData.length; i++) System.err.println("time: " + String.format("%12.1f",dt[i]) + "\tdata: " + strData[i]); } } |
To compile SimpleSink, you need to include the CloudTurbine library, CTlib.jar, on the javac classpath. To run SimpleSink, the java classpath needs to include CTlib.jar as well as the folder where SimpleSink.class is located.
Run CTsource first to produce the sample data set. Let’s say the CTsource data is located in CloudTurbine/CTdata/CTsource; you would want to run SimpleSink from the CloudTurbine directory (i.e., so it can dive into CTdata/CTsource to fetch the sample data). A truncated version of the SimpleSink output is given below.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
Found channel: c0 Found channel: c1.f32 Found channel: c2.txt oldest time = 1.46E9, newest time = 1.460000099E9 Data for channel c1.f32 time: 1460000000.0 data: 100.0 time: 1460000001.0 data: 101.0 time: 1460000002.0 data: 102.0 ... more data ... time: 1460000097.0 data: 197.0 time: 1460000098.0 data: 198.0 time: 1460000099.0 data: 199.0 Data for channel c2.txt time: 1460000000.0 data: 0 time: 1460000001.0 data: 1 time: 1460000002.0 data: 2 ... more data ... time: 1460000097.0 data: 97 time: 1460000098.0 data: 98 time: 1460000099.0 data: 99 |