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);
}
}
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;
}
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);
}
}
Aggregations