Search in sources :

Example 1 with DataQuerySnapshot

use of org.apache.apex.malhar.lib.appdata.schemas.DataQuerySnapshot in project apex-malhar by apache.

the class DataQuerySnapshotDeserializer method deserializeHelper.

/**
 * This is a helper deserializer method.
 * @param json The JSON to deserialize.
 * @param context The context information to use when deserializing the query.
 * @return The deserialized query. If the given json contains some invalid content this
 * method may return null.
 * @throws Exception
 */
private Message deserializeHelper(String json, Object context) throws Exception {
    JSONObject jo = new JSONObject(json);
    // Validate fields
    if (!SchemaUtils.checkValidKeys(jo, FIRST_LEVEL_FIELD_COMBINATIONS)) {
        throw new IOException("Invalid keys");
    }
    // // Query id stuff
    String id = jo.getString(DataQuerySnapshot.FIELD_ID);
    String type = jo.getString(DataQuerySnapshot.FIELD_TYPE);
    if (!type.equals(DataQuerySnapshot.TYPE)) {
        LOG.error("Found type {} in the query json, but expected type {}.", type, DataQuerySnapshot.TYPE);
        return null;
    }
    // / Countdown
    long countdown = -1L;
    boolean hasCountdown = jo.has(DataQuerySnapshot.FIELD_COUNTDOWN);
    if (hasCountdown) {
        countdown = jo.getLong(DataQuerySnapshot.FIELD_COUNTDOWN);
    }
    // //Data
    Map<String, String> schemaKeys = null;
    Set<String> fieldsSet = Sets.newHashSet();
    if (jo.has(DataQuerySnapshot.FIELD_DATA)) {
        JSONObject data = jo.getJSONObject(DataQuerySnapshot.FIELD_DATA);
        if (!SchemaUtils.checkValidKeys(data, DATA_FIELD_COMBINATIONS)) {
            LOG.error("Error validating {} field", DataQuerySnapshot.FIELD_DATA);
            throw new IOException("Invalid keys");
        }
        if (data.has(DataQuerySnapshot.FIELD_SCHEMA_KEYS)) {
            schemaKeys = SchemaUtils.extractMap(data.getJSONObject(DataQuerySnapshot.FIELD_SCHEMA_KEYS));
        }
        if (data.has(DataQuerySnapshot.FIELD_FIELDS)) {
            // // Fields
            JSONArray jArray = data.getJSONArray(DataQuerySnapshot.FIELD_FIELDS);
            for (int index = 0; index < jArray.length(); index++) {
                String field = jArray.getString(index);
                if (!fieldsSet.add(field)) {
                    LOG.error("The field {} was listed more than once, this is an invalid query.", field);
                }
            }
        }
    }
    Fields fields = new Fields(fieldsSet);
    if (!hasCountdown) {
        return new DataQuerySnapshot(id, fields, schemaKeys);
    } else {
        return new DataQuerySnapshot(id, fields, countdown, schemaKeys);
    }
}
Also used : Fields(org.apache.apex.malhar.lib.appdata.schemas.Fields) JSONObject(org.codehaus.jettison.json.JSONObject) JSONArray(org.codehaus.jettison.json.JSONArray) DataQuerySnapshot(org.apache.apex.malhar.lib.appdata.schemas.DataQuerySnapshot) IOException(java.io.IOException)

Example 2 with DataQuerySnapshot

use of org.apache.apex.malhar.lib.appdata.schemas.DataQuerySnapshot in project apex-malhar by apache.

the class DataQuerySnapshotValidator method validate.

@Override
public boolean validate(Message query, Object context) {
    DataQuerySnapshot gdqt = (DataQuerySnapshot) query;
    SnapshotSchema schema = (SnapshotSchema) ((SchemaRegistry) context).getSchema(gdqt.getSchemaKeys());
    Set<String> fields = schema.getValuesDescriptor().getFields().getFields();
    if (!fields.containsAll(gdqt.getFields().getFields())) {
        LOG.error("Some of the fields in the query {} are not one of the valid fields {}.", fields, gdqt.getFields().getFields());
        return false;
    }
    if (gdqt.getFields().getFields().isEmpty()) {
        gdqt.setFieldsVal(new Fields(fields));
    }
    return true;
}
Also used : Fields(org.apache.apex.malhar.lib.appdata.schemas.Fields) DataQuerySnapshot(org.apache.apex.malhar.lib.appdata.schemas.DataQuerySnapshot) SnapshotSchema(org.apache.apex.malhar.lib.appdata.schemas.SnapshotSchema)

Example 3 with DataQuerySnapshot

use of org.apache.apex.malhar.lib.appdata.schemas.DataQuerySnapshot in project apex-malhar by apache.

the class AbstractAppDataSnapshotServer method processQuery.

/**
 * process the query send.
 * provide this method to give sub class a chance to override.
 * @param queryJSON
 */
protected void processQuery(String queryJSON) {
    LOG.debug("query {}", queryJSON);
    Message query = null;
    try {
        query = queryDeserializerFactory.deserialize(queryJSON);
    } catch (IOException ex) {
        LOG.error("Error parsing query: {}", queryJSON);
        LOG.error("{}", ex);
        return;
    }
    if (query instanceof SchemaQuery) {
        SchemaResult schemaResult = schemaRegistry.getSchemaResult((SchemaQuery) query);
        if (schemaResult != null) {
            LOG.debug("queueing {}", schemaResult);
            schemaQueue.add(schemaResult);
        }
    } else if (query instanceof DataQuerySnapshot) {
        queryProcessor.enqueue((DataQuerySnapshot) query, null, null);
    }
}
Also used : SchemaResult(org.apache.apex.malhar.lib.appdata.schemas.SchemaResult) Message(org.apache.apex.malhar.lib.appdata.schemas.Message) DataQuerySnapshot(org.apache.apex.malhar.lib.appdata.schemas.DataQuerySnapshot) IOException(java.io.IOException) SchemaQuery(org.apache.apex.malhar.lib.appdata.schemas.SchemaQuery)

Aggregations

DataQuerySnapshot (org.apache.apex.malhar.lib.appdata.schemas.DataQuerySnapshot)3 IOException (java.io.IOException)2 Fields (org.apache.apex.malhar.lib.appdata.schemas.Fields)2 Message (org.apache.apex.malhar.lib.appdata.schemas.Message)1 SchemaQuery (org.apache.apex.malhar.lib.appdata.schemas.SchemaQuery)1 SchemaResult (org.apache.apex.malhar.lib.appdata.schemas.SchemaResult)1 SnapshotSchema (org.apache.apex.malhar.lib.appdata.schemas.SnapshotSchema)1 JSONArray (org.codehaus.jettison.json.JSONArray)1 JSONObject (org.codehaus.jettison.json.JSONObject)1