0 votes
asked in MicroStream for Java by (350 points)
edited by

Full Question, because Title alows only 60 character: sad

How to handle groupings with microstream DB like perhaps for relational DB: select groups, count(*) from table group by groups?

If I use a rellational database like perhaps MariaDB i can use grouping by creating a query like

select groups, count(*) from table group by groups;

But how to handle this with a microstream db?
Will it be required to generate the code manually, 1st to figure out which groups I still have in the table and in a second round to do a loop to count for each group the amount of available data?
Or, is there a function available to use grouping for similar issues?

1 Answer

+2 votes
answered by (3.4k points)
selected by
 
Best answer

Hello,

Query your data is basically no Microstream topic but a Java topic since Microstream works with standard java object structures. All operations you perform on your data can be done by plain java code. Therefore, Microstream does not provide any special query languages like SQL.

Java 8 provides the Streams API for example. With this API you can do sum's count's and a lot more.

https://docs.oracle.com/javase/8/docs/api/?java/util/stream/Stream.html

For grouping with Java Streams there is an example on https://stackoverflow.com/questions/25441088/group-by-counting-in-java-8-stream-api

commented by (350 points)
edited by
Thank you for this answer! It helped fine.
And with help by - Thanks Bernhard :-)
I tryed following code with success!
import java.time.Month;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.rieders.finmgmtbasic.dbmodel.msTUmsatzClassification;
import com.rieders.finmgmtbasic.dbstorage.dataRoot;
import com.rieders.finmgmtbasic.dbstorage.dbHandler;

import one.microstream.storage.types.EmbeddedStorageManager;

public class TestUmsByGroup
{
    public static void main(final String[] args)
    {
        final EmbeddedStorageManager storage = dbHandler.getStoragemanager();

        if(storage.root() == null)
        {
            System.out.println("No existing Database found ");
        }
        else
        {

            final dataRoot root = (dataRoot)storage.root();

            final List<msTUmsatzClassification> myIntList = new ArrayList<>(root.allUmsClassifications);
   
            final Map<Integer, Map<Integer, Map<Integer, Double>>> aggregatedObjects =
                myIntList
                    .parallelStream()
                    .collect(
                        Collectors.groupingBy(TestUmsByGroup::getJahr,                                                                        
                            Collectors.groupingBy(TestUmsByGroup::getMonth,
                                Collectors.groupingBy(msTUmsatzClassification::getL1Id,
                                    Collectors.summingDouble(msTUmsatzClassification::getDbetrag)))));

            aggregatedObjects.forEach((year, monthMap) -> {
                monthMap.forEach((month, typeMap) -> {
                    typeMap.forEach((type, sum) -> {
                        System.out.println(year + "/" + Month.of(month) + "/" + type + " = " + sum);
                    });
                });
            });

            storage.shutdown();
        }

    }

    // static helperMonat
    private static int getMonth(final msTUmsatzClassification msTUmsatzClassification)
    {
        final Calendar cal = Calendar.getInstance();
        cal.setTime(msTUmsatzClassification.getDatum());
        return cal.get(Calendar.MONTH) + 1;
    }
   
    // static helperjahr
    private static int getJahr(final msTUmsatzClassification msTUmsatzClassification)
    {
        final Calendar cal = Calendar.getInstance();
        cal.setTime(msTUmsatzClassification.getDatum());
        return cal.get(Calendar.YEAR);
    }

}
Notes: Every question must be a separate forum post. Headline: Formulate your question shortly and precisely. Thank you!
Powered by Question2Answer
...