Time Series Java API example

416 views
Skip to first unread message

Simon White

unread,
Jul 11, 2015, 7:04:48 PM7/11/15
to orient-...@googlegroups.com
I searched this forum for 'time series' and found three unanswered about how to create a time series for timestamped data as described in http://orientdb.com/docs/last/orientdb.wiki/Time-series-use-case.html

I daresay this is a common scenario and many are using it, however it is a difficult concept for newbies to grasp!

Via the java API I have created five classes, to capture years to hours and point to my log entry record...

        //log entry
        OrientVertexType logentry= graph.getVertexType("logentry");
        if (logentry== null)
        {
            logentry= graph.createVertexType("logentry");
            //bunch of other properties
        }
       
        //hour link logentry
        OrientVertexType hour = graph.getVertexType("hour");
        if (hour == null)
        {
            hour = graph.createVertexType("hour");
            hour.createProperty("logs", OType.LINKMAP, logentry);
        }
       
        //day link hour
        OrientVertexType day = graph.getVertexType("day");
        if (day == null)
        {
            day = graph.createVertexType("day");
            day.createProperty("hour", OType.LINKMAP, hour);
        }
       
        //month link day
        OrientVertexType month = graph.getVertexType("month");
        if (month == null)
        {
            month = graph.createVertexType("month");
            month.createProperty("day", OType.LINKMAP, day);
        }
       
        //year link month
        OrientVertexType year = graph.getVertexType("year");
        if (year == null)
        {
            year = graph.createVertexType("year");
            year.createProperty("month", OType.LINKMAP, month);
        }

Next I have written simple loops to generate instances of the year, month, day and hour verticies (12 x month 31 x days etc...)

But now how do I enter a logentry?

alessand...@gmail.com

unread,
Jul 13, 2015, 11:04:30 AM7/13/15
to orient-...@googlegroups.com
Hi Simon, 
I have created an example.

OrientVertexType log= graph.createVertexType("log");
log.createProperty("priority", OType.STRING);
       
Map<String, OrientVertex> logs = new HashMap<>();
OrientVertex log1=graph.addVertex("class:log");
OrientVertex log2=graph.addVertex("class:log");
log1.setProperty("priority", "high");
log2.setProperty("priority", "medium");
logs.put("log1", log1);
logs.put("log2", log2);
Map<String, OrientVertex> logs2 = new HashMap<>();
OrientVertex log3=graph.addVertex("class:log");
OrientVertex log4=graph.addVertex("class:log");
log3.setProperty("priority", "medium");
log4.setProperty("priority", "low");
logs2.put("log1", log3);
logs2.put("log2", log4);
//link log and hour        
OrientVertexType hour = graph.createVertexType("hour");
hour.createProperty("logs", OType.LINKSET, log);
OrientVertex ora10=graph.addVertex("class:hour");
OrientVertex ora20 = graph.addVertex("class:hour");
ora10.setProperty("logs",logs);
ora20.setProperty("logs",logs2);
Map<String, OrientVertex> ore = new HashMap<>();
ore.put("10", ora10);
ore.put("20", ora20);
// link day with hour
OrientVertexType day = graph.createVertexType("day");
day.createProperty("hour", OType.LINKMAP, hour);
OrientVertex day25=graph.addVertex("class:day");
day25.setProperty("hour",ore);
Map<String, OrientVertex> days = new HashMap<>();
days.put("25", day25);
// link month with day
OrientVertexType month = graph.createVertexType("month");
month.createProperty("day", OType.LINKMAP, day);
OrientVertex month3=graph.addVertex("class:month");
month3.setProperty("day",days);
Map<String, OrientVertex> months = new HashMap<>();
months.put("3", month3);
// link year with month
OrientVertexType year = graph.createVertexType("year");
year.createProperty("month", OType.LINKMAP, month);
OrientVertex year2015=graph.addVertex("class:year");
year2015.setProperty("month",months);

you can try this query: select month[3].day[25].hour[10].logs from year

Regards,
Alessandro 
Message has been deleted
Message has been deleted

Simon White

unread,
Jul 13, 2015, 7:02:09 PM7/13/15
to orient-...@googlegroups.com
Thank you so much for that, a comprehensive and very helpful example!

That said - my question still stands - this example is wonderful assuming I have a history of logs I want to insert into the database all at once, but of course log entries are being generated all the time. Can I insert a new one into the structure above at runtime? via sql or java API?

I am guessing I can insert an empty hashmap and populate that later?

        OrientVertexType logVertex= graph.createVertexType("log");
        logVertex.createProperty("priority", OType.STRING);
       
        Map<String, OrientVertex> logs = new HashMap<>(); //empty map to populate later
       
        //---- link log and hour ----          
        OrientVertexType hourVertex = graph.createVertexType("hour");
        hourVertex.createProperty("logs", OType.LINKSET, logVertex);
       
        Map<String, OrientVertex> hours = new HashMap<>();
       
        //add all the hours in a day
        for(int i = 0; i < 24; i++)
        {
            OrientVertex hour = graph.addVertex("class:hour");
            hour.setProperty("logs", logs);
            hours.put(Integer.toString(i), hour);
        }

alessand...@gmail.com

unread,
Jul 14, 2015, 3:53:46 AM7/14/15
to orient-...@googlegroups.com
Hi Simon,
of course you can add a new log to a certain hour 

After create the log 
insert into log(priority) values ('very high')

you can update the hour and add the log that you have created
update #12:0 add logs= [#11:4]

#12:0 is the rid of the hour
#11:4 is the rid of the log

Regards,
Alessandro

Simon White

unread,
Jul 14, 2015, 8:37:44 AM7/14/15
to orient-...@googlegroups.com
Thank you again, great answer, I am sorry I am not doing very well with this!

I think actually I have still populated my Vertex incorrectly, should I be doing nested loops so that essentially I generate a unique vertex for every unique hour in the year .. ??

Then what is the syntax to add the log entry...

#12:0 is the rid of the year

#11:4 is the rid of the log

Something like ...
update hour add log [#11:4] where @rid = (select month[1].day[4].hour[6] from #12:0)

but that is not correct!

Simon White

unread,
Jul 14, 2015, 8:55:46 AM7/14/15
to orient-...@googlegroups.com
So close... missed the equals!!

update hour add log =[#11:4] where @rid = (select month[1].day[4].hour[6] from #67:0)

Is this right?

Luigi Dell'Aquila

unread,
Jul 14, 2015, 9:15:20 AM7/14/15
to orient-...@googlegroups.com
Hi Simon, 
I think you should use IN instead of = in the where condition:

update hour add log =[#11:4] where @rid IN (select month[1].day[4].hour[6] from #67:0)

--

---
You received this message because you are subscribed to the Google Groups "OrientDB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to orient-databa...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Simon White

unread,
Jul 14, 2015, 11:09:00 AM7/14/15
to orient-...@googlegroups.com
Absolutely right Luigi, thank you for the tip!

Simon White

unread,
Jul 14, 2015, 11:22:09 AM7/14/15
to orient-...@googlegroups.com

To summarise:    *WORKING* TIME SERIES EXAMPLE

1. Create linked Vertex with properties to linked hashmaps as per Alessandro's original post

2. Populate instances of days, weeks, etc
        //add a few year vertex
        for(int yy = 2015; yy < 2020; yy++) //will allow minus 2015 to the orid to query
        {
            //all months this year
            Map<String, OrientVertex> months = new HashMap<>();
           
            //add all the months
            for(int mm = 0; mm < 12; mm++) //developer gothcha - java months are 0 based!
            {
                //all days this month
                Map<String, OrientVertex> days = new HashMap<>();
               
                //add all the days in a month
                for(int dd = 1; dd < 32; dd++)
                {
                    //all hours this day

                    Map<String, OrientVertex> hours = new HashMap<>();
                   
                    //add all the hours in a day
                    for(int i = 0; i < 24; i++)
                    {
                        OrientVertex hour = graph.addVertex("class:hour");
                        hour.setProperty("log", new HashMap<>()); //new empty hashmap to hold the logs
                        hour.setProperty("label", i + ":00 to " + i + ":59");
                        hours.put(Integer.toString(i), hour);
                    }
                   
                    //add days
                    OrientVertex day = graph.addVertex("class:day");
                    //link log with hour
                    day.setProperty("hour", hours);
                    day.setProperty("label", dd + "");
                    days.put(Integer.toString(dd), day);
                }
               
                //add months
                OrientVertex month = graph.addVertex("class:month");
                //link day with hour
                month.setProperty("day", days);
                month.setProperty("label", new DateFormatSymbols().getMonths()[mm]);
                months.put(Integer.toString(mm), month);
            }
           
            //add years
            OrientVertex year = graph.addVertex("class:year");
            year.setProperty("label", Integer.toString(yy));
            //link year with month ----
            year.setProperty("month", months);
        }

3. At runtime add logs with:


insert into log(priority) values ('very high')
update hour add log =[#11:0] where @rid IN (select month[1].day[4].hour[6] from #12:0)                        (thanks Luigi)

4. Query logs with

select month[10].day[12].hour[16].log from #12:0                                                                           

or select expand (logentry) from (select month[10].day[12].hour[16].log as logentry from #12:0)

(#12:0 is the rid of the year, #11:0 is the rid of the log)


Luca Garulli

unread,
Jul 14, 2015, 2:07:09 PM7/14/15
to orient-...@googlegroups.com
Hi Simon,
Would you like to contribute on extending the documentation for further users? 



Best Regards,

Founder & CEO


or select expand (logentry) from (select month[10].day[12].hour[16].log as logentry from #1756:0)


(#12:0 is the rid of the year, #11:0 is the rid of the log)

hartmut bischoff

unread,
Jul 16, 2015, 8:12:50 AM7/16/15
to orient-...@googlegroups.com
As I see it, only simple document-queries are used in this example.
So why is it build with vertices instead of ordenary classes?
I prefer to  derive anything from a given base-class, eg "TS" then using "V"
Reply all
Reply to author
Forward
0 new messages