Search code examples
tuplesapache-stormesper

Esper-Wrong sequence of attributes in query results


I am new to Esper and i am working on Storm-Esper collaboration.Through my main class,i send queries to a bolt which contains esper while the esper-bolt sends the tuple which contain the results to a printer bolt.My problem is that,although the result of a query is correct as for the values,the attribute values are not in the correct order.For example,i have a query which selects attributes from a pilot's table: name,surname,airline and i should have the result in the same order.However i get:name,airline,surname.I have tried everything concerning group by and order by.I suppose it must be an Esper's fault when creating the event's map which contains the attributes-values.I paste the main class code and the esper bolt code where the map is processed.Any idea why is that happening is most welcome!

**mainclass**
.addStatements(("insert into pilotStream " +
                "select * " +
                "from Log.win:time(120 second) A "))
.addStatements(("insert into employeeStream " +
                "select * " +
                "from Emp.win:time(120 second) A "))
.addStatements(("insert into CombinedEvent "+
                "select tick.pilotName as p_name , " +
                "tick.pilotSurname as p_surname , " +
                "tick.airline as p_airline " +
                "from pilotStream.win:time(120 second) as tick, " +
                "employeeStream.win:time(120 second) as rom "+ 
                "where tick.airline = rom.employeeAirline "+
              ))
**espebolt**
Map<String, Object> emap = (Map<String, Object>) newEvent.getUnderlying();
                    String Event_name = newEvent.getEventType().getName();
                    //System.out.println(Event_name);

                    for (Map.Entry<String, Object> entry : emap.entrySet()) {

                    //  String key =  entry.getKey();
                        String val = String.valueOf(entry.getValue()) ;
                        //System.out.println(key+" :"+val);
                        //System.out.println(val);

                        values.add(val);
                    }

                    collector.emit(Event_name, toTuple(newEvent, values, false));
                    values.removeAll(values);

The result should be : source: Esper-Print:2, stream: CombinedEvent, id: {}, [John, Snow, Lufthansa] Instead,i get:source: Esper-Print:2, stream: CombinedEvent, id: {}, [John, Lufthansa, Snow]

P.S.The toTuple functions simply gets the values of the attributes through the values list of strings and puts them into a tuple which is emitted to printerbolt.In the espebolt code there is some printing in comments which helped me see that the problem is in the map which esper creates internally.


Solution

  • By default Esper generates Map events. This can be changed into object-array events when setting a configuration or with annotations. Map events use "HashMap" and not "LinkedHashMap". The "HashMap" is not ordered when iterating the key-value pairs but takes much less memory. Object-array is ordered. For ordered access to Map events there is the "EventType" that you can get from a statement which returns you the property names in order.