Search in sources :

Example 6 with EventWrapper

use of io.s4.collector.EventWrapper in project core by s4.

the class GenericJsonClientStub method eventWrapperFromBytes.

@Override
public EventWrapper eventWrapperFromBytes(byte[] v) {
    try {
        // interpret v as a JSON string
        String s = new String(v, Charset.forName("UTF8"));
        JSONObject json = new JSONObject(s);
        String streamName = json.getString("stream");
        String className = json.getString("class");
        Class<?> clazz;
        try {
            clazz = Class.forName(className);
        } catch (ClassNotFoundException e) {
            throw new ObjectBuilder.Exception("bad class name for json-encoded object: " + className, e);
        }
        String[] keyNames = null;
        JSONArray keyArray = json.optJSONArray("keys");
        if (keyArray != null) {
            keyNames = new String[keyArray.length()];
            for (int i = 0; i < keyNames.length; ++i) {
                keyNames[i] = keyArray.optString(i);
            }
        }
        String jevent = json.getString("object");
        Object obj = GsonUtil.get().fromJson(jevent, clazz);
        return new EventWrapper(streamName, keyNames, obj);
    } catch (JSONException e) {
        logger.error("problem with event JSON", e);
    } catch (ObjectBuilder.Exception e) {
        logger.error("failed to build object from JSON", e);
    }
    return null;
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) ObjectBuilder(io.s4.client.util.ObjectBuilder) EventWrapper(io.s4.collector.EventWrapper)

Example 7 with EventWrapper

use of io.s4.collector.EventWrapper in project core by s4.

the class EventClock method update.

public void update(EventWrapper eventWrapper) {
    long eventTime = -1;
    String streamName = eventWrapper.getStreamName();
    String fieldName = eventClockStreamsMap.get(streamName);
    if (fieldName != null) {
        Object event = eventWrapper.getEvent();
        Schema schema = schemaContainer.getSchema(event.getClass());
        Property property = schema.getProperties().get(fieldName);
        if (property != null && (property.getType().equals(Long.TYPE) || property.getType().equals(Long.class))) {
            try {
                eventTime = (Long) property.getGetterMethod().invoke(event);
                updateTime(eventTime);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }
}
Also used : Schema(io.s4.schema.Schema) Property(io.s4.schema.Schema.Property)

Aggregations

EventWrapper (io.s4.collector.EventWrapper)5 CompoundKeyInfo (io.s4.dispatcher.partitioner.CompoundKeyInfo)3 ArrayList (java.util.ArrayList)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 ObjectBuilder (io.s4.client.util.ObjectBuilder)1 Partitioner (io.s4.dispatcher.partitioner.Partitioner)1 VariableKeyPartitioner (io.s4.dispatcher.partitioner.VariableKeyPartitioner)1 PrototypeRequest (io.s4.message.PrototypeRequest)1 Response (io.s4.message.Response)1 SinglePERequest (io.s4.message.SinglePERequest)1 Schema (io.s4.schema.Schema)1 Property (io.s4.schema.Schema.Property)1 EventClock (io.s4.util.clock.EventClock)1 BufferedReader (java.io.BufferedReader)1 FileReader (java.io.FileReader)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 HashMap (java.util.HashMap)1 List (java.util.List)1