1

I'm investigating the use of cqengine as the cost of re querying the database would be too costly, but I'm really struggling to retrieve the data I need.

IndexedCollection<PnLRow> rows = CQEngine.newInstance();

rows.addIndex(NavigableIndex.onAttribute(PnLRow.STRATEGY));
rows.addIndex(NavigableIndex.onAttribute(PnLRow.SUBSTRATEGY));

rows.add(new PnLRow("Commodities", "Directional", "Brent", "COMMODITIES", 1, 1, 1, 1, 1, 1, 1, 1));
rows.add(new PnLRow("Commodities", "Directional", "Brent", "FX", 1, 1, 1, 1, 1, 1, 1, 1));
rows.add(new PnLRow("Commodities", "Directional", "Copper", "COMMODITIES", 1, 1, 1, 1, 1, 1, 1, 1));
rows.add(new PnLRow("Commodities", "Directional", "Copper", "FX", 1, 1, 1, 1, 1, 1, 1, 1));
rows.add(new PnLRow("FX", "Directional", "CAD", "FX", 1, 1, 1, 1, 1, 1, 1, 1));
rows.add(new PnLRow("FX", "Directional", "CHF", "FX", 1, 1, 1, 1, 1, 1, 1, 1));

In SQL I'd like to

select strategy, sum(num1),sum(num2)...etc
from tblname
group by strategy

Every example I come across, shows me how to do this for one field

Map<String, Double> grpx = rows.stream().collect(Collectors.groupingBy(PnLRow::getStratName, Collectors.summingDouble(PnLRow::getDaily)));

Which returns

{FX=2.0, Commodities=4.0}

Is it possible to do it for all fields, or am I approaching this completely incorrectly

Edit:

Example output I'd like would be

{ {fx=2.0,2.0,2.0,2.0,2.0,2.0,2.0,2.0}, {Commodities=4.0,4.0,4.0,4.0,4.0,4.0,4.0,4.0} }
Nick
  • 1,315
  • 9
  • 16
  • Your question mostly relates to how to group and sum objects with lambdas in general, and not to CQEngine per-se. I'm not an expert on lambdas so I'll just answer regarding the CQEngine part: You need to use rows.retrieve() to make CQEngine execute a query. Above, you're actually bypassing the query engine and its indexes, and accessing the collection directly. There's a bit of discussion on the CQEngine site about how to perform queries to get a ResultSet. To group and sum the results, you can then use JDK 8 StreamSupport to convert the ResultSet to a stream. – npgall Aug 06 '14 at 17:19
  • See: [Grouping and Aggregation in CQEngine with Java 8 Lambda Expressions](https://code.google.com/p/cqengine/#Java_version_8_or_later_-_lambda_expressions). – npgall Aug 06 '14 at 17:36
  • Can you give an example of the output you'd like to see? I don't understand what you mean by "do it for all fields." – Stuart Marks Aug 07 '14 at 13:18
  • @StuartMarks example output added – Nick Aug 07 '14 at 14:27
  • @Nick The one-arg overload of `groupingBy` will accumulate a list of values instead if summing them. Is that what you want? – Stuart Marks Aug 07 '14 at 15:26
  • @StuartMarks No, don't worry. When I get time I will look at npgall's suggestion – Nick Aug 07 '14 at 15:54

0 Answers0