-
Notifications
You must be signed in to change notification settings - Fork 9
Tutorial A
On this page we will demonstrate examples on how you can utilize these libraries.
Will construct a very simple data container. The first step is to define the meta definition. From that point, we can construct the data container, and then add a CDataRow object to the container.
// Create meta-definition
String[] columnNames = new String[] {"account_id", "owner", "balance", "rate"};
Class[] columnTypes = new Class[] {String.class, String.class, Double.class, Double.class};
String[] primaryKeys = new String[] {"account_id"};
CRowMetaData metaDef = new CRowMetaData(columnNames, columnTypes, primaryKeys);
// Create a row of data
CDataRow row = new CDataRow();
row.setRawData(new Object[] {"asdf1234", "Casper", new Double(25000000.0), new Double(0.650434)});
// Create data container , set data
CDataCacheContainer container = new CDataCacheContainer("mytest", metaDef, new HashMap());
container.addData(new CDataRow[] {row});
There is a database adapter (CDataCacheDBAdapter) that effectively performs the work of reading in the meta definition from the java.sql.ResultMetaData?. Then automatically reads in the rows of the java.sql.ResultSet? into the data container. All this is done in less than a few lines of code.
// Perform query, retrieve result set
String sql = "select * from my_table";
Connection conn = // get your connection;
Statement stmt = conn.createStatement();
String[] primaryKeys = {"my_primarykey"};
ResultSet rs = stmt.executeQuery(sql);
// Construct container object (will read meta definition and rows)
CDataCacheContainer container = CDataCacheDBAdapter.loadData(rs, null, primaryKeys, new HashMap());
Note that there are already “get” methods in the container that construct an equality-based match for you, just for convenience (since that’s the most common requirement). However, if you need to perform a search involving multiple filters or more complex filters exposed by the framework, you will need to construct your own collection of filters via the filter clause.
// Assume that we are given a CDataContainer called "container",
// and a meta-definition object called "metaData"
// Create two filters: (1) regex on column cusip, (2) range on column: orig_balance
CDataContainer container = …;
CRowMetaData metaData = …;
// Will match all accounts beginning with "ABC"
RegexFilter cusipMatch = new RegexFilter("account_id", metaData, new String[] {"ABC(.)*"});
// Will match all balances in the range of $8.0-8.5MM, non-inclusive.
RangeFilter rangeMatch = new RangeFilter("balance", metaData, 8000000.0, 8500000.0, false);
// Add both to the filter clause
CDataFilterClause filterClause = new CDataFilterClause();
filterClause.addFilter(regexMatch);
filterClause.addFilter(rangeMatch);
// Perform query on the container, retrieve rowset
CDataRowSet results = container.get(filterClause, null, false);
//
// Assume that we will use the result object from the last example. Just want
// to print out all matching results from our query
// Notice the type-safe retrieval methods (just like java.sql.ResultSet),
// and the scrolling functionality (via .next())
//
while (results.next())
{
System.out.println("account_id: " + results.getString("account_id"));
System.out.println("owner: " + results.getString("owner"));
System.out.println("balance: " + results.getDouble("balance");
System.out.println("rate: " + results.getDouble("rate");
}
Suppose we want to find the total balance of all rows (in the results object). Simply invoke the aggregator class’ aggregation implementations w/ the rowset.
// Find sum of column orig_balances for all matching records.
Double sumRecords = CDataRowSetAggregator.sum(results, "balance");