Search in sources :

Example 6 with Property

use of io.s4.schema.Schema.Property in project core by s4.

the class JoinPE method processEvent.

public void processEvent(Object event) {
    if (eventsToJoin == null) {
        eventsToJoin = new HashMap<String, Object>();
    }
    List<String> fieldNames = eventFields.get(getStreamName());
    if (fieldNames == null) {
        return;
    }
    // we only use the last event that comes through on the given stream
    eventsToJoin.put(getStreamName(), event);
    if (eventsToJoin.keySet().size() == eventFields.keySet().size()) {
        Object newEvent = null;
        try {
            newEvent = outputClass.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        Schema newEventSchema = schemaContainer.getSchema(newEvent.getClass());
        for (String streamName : eventsToJoin.keySet()) {
            Object partialEvent = eventsToJoin.get(streamName);
            Schema partialEventSchema = schemaContainer.getSchema(partialEvent.getClass());
            List<String> includeFields = eventFields.get(streamName);
            if (includeFields.size() == 1 && includeFields.get(0).equals("*")) {
                for (Property partialEventProperty : partialEventSchema.getProperties().values()) {
                    copyField(partialEventProperty.getName(), partialEventSchema, newEventSchema, partialEvent, newEvent);
                }
            } else {
                for (String includeField : includeFields) {
                    copyField(includeField, partialEventSchema, newEventSchema, partialEvent, newEvent);
                }
            }
        }
        dispatcher.dispatchEvent(outputStreamName, newEvent);
        if (logger.isDebugEnabled()) {
            logger.debug("STEP 7 (JoinPE): " + newEvent.toString());
        }
    }
}
Also used : Schema(io.s4.schema.Schema) Property(io.s4.schema.Schema.Property)

Example 7 with Property

use of io.s4.schema.Schema.Property in project core by s4.

the class LoadGenerator method makeRecord.

@SuppressWarnings("unchecked")
private Object makeRecord(JSONObject jsonRecord, Schema schema) {
    Object event = null;
    try {
        event = schema.getType().newInstance();
        for (Iterator it = jsonRecord.keys(); it.hasNext(); ) {
            String propertyName = (String) it.next();
            Property property = schema.getProperties().get(propertyName);
            if (property == null) {
                // not in schema, just continue
                continue;
            }
            Method setterMethod = property.getSetterMethod();
            Object value = jsonRecord.get(propertyName);
            if (value.equals(JSONObject.NULL)) {
                continue;
            }
            setterMethod.invoke(event, makeSettableValue(property, value));
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return event;
}
Also used : Iterator(java.util.Iterator) JSONObject(org.json.JSONObject) Method(java.lang.reflect.Method) Property(io.s4.schema.Schema.Property) JSONException(org.json.JSONException) ParseException(org.apache.commons.cli.ParseException)

Example 8 with Property

use of io.s4.schema.Schema.Property in project core by s4.

the class LoadGenerator method makeArray.

@SuppressWarnings("unchecked")
public Object makeArray(Property property, JSONArray jsonArray) {
    Property componentProperty = property.getComponentProperty();
    Class clazz = componentProperty.getType();
    int size = jsonArray.length();
    Object array = Array.newInstance(clazz, size);
    try {
        for (int i = 0; i < size; i++) {
            Object value = jsonArray.get(i);
            Object adjustedValue = makeSettableValue(componentProperty, value);
            Array.set(array, i, adjustedValue);
        }
    } catch (JSONException je) {
        throw new RuntimeException(je);
    }
    return array;
}
Also used : JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) Property(io.s4.schema.Schema.Property)

Example 9 with Property

use of io.s4.schema.Schema.Property in project core by s4.

the class DefaultPartitioner method getKeyValues.

private List<KeyInfo> getKeyValues(Object record, Schema schema, List<String> keyNameElements, int elementIndex, List<KeyInfo> keyInfoList, KeyInfo keyInfo) {
    String keyElement = keyNameElements.get(elementIndex);
    Property property = schema.getProperties().get(keyElement);
    if (property == null) {
        return null;
    }
    keyInfo.addElementToPath(keyElement);
    Object value = null;
    try {
        value = property.getGetterMethod().invoke(record);
    } catch (Exception e) {
        if (debug) {
            System.out.println("key element is " + keyElement);
            e.printStackTrace();
        }
    }
    if (value == null) {
        // return a null KeyInfo list if we hit a null value
        return null;
    }
    if (property.isList()) {
        List list = (List) value;
        // TODO: handle case where key does not include property of
        // component type
        Schema componentSchema = property.getComponentProperty().getSchema();
        int listLength = list.size();
        for (int i = 0; i < listLength; i++) {
            Object listEntry = list.get(i);
            KeyInfo keyInfoForListEntry = keyInfo.copy();
            keyInfoForListEntry.addElementToPath(i);
            Object partialList = getKeyValues(listEntry, componentSchema, keyNameElements, elementIndex + 1, keyInfoList, keyInfoForListEntry);
            if (partialList == null) {
                return null;
            }
        }
    } else if (property.getSchema() != null) {
        return getKeyValues(value, property.getSchema(), keyNameElements, elementIndex + 1, keyInfoList, keyInfo);
    } else {
        keyInfo.setValue(String.valueOf(value));
        keyInfoList.add(keyInfo);
    }
    return keyInfoList;
}
Also used : Schema(io.s4.schema.Schema) List(java.util.List) ArrayList(java.util.ArrayList) Property(io.s4.schema.Schema.Property)

Example 10 with Property

use of io.s4.schema.Schema.Property 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

Property (io.s4.schema.Schema.Property)10 Schema (io.s4.schema.Schema)6 ArrayList (java.util.ArrayList)4 List (java.util.List)3 JSONException (org.json.JSONException)3 JSONObject (org.json.JSONObject)3 CompoundKeyInfo (io.s4.dispatcher.partitioner.CompoundKeyInfo)1 KeyInfo (io.s4.dispatcher.partitioner.KeyInfo)1 KeyPathElement (io.s4.dispatcher.partitioner.KeyInfo.KeyPathElement)1 KeyPathElementIndex (io.s4.dispatcher.partitioner.KeyInfo.KeyPathElementIndex)1 KeyPathElementName (io.s4.dispatcher.partitioner.KeyInfo.KeyPathElementName)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 ParseException (org.apache.commons.cli.ParseException)1