use of org.apache.apex.malhar.lib.appdata.schemas.Fields 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.Fields 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.Fields in project apex-malhar by apache.
the class DimensionsDescriptorTest method equalsAndHashCodeTest.
@Test
public void equalsAndHashCodeTest() {
DimensionsDescriptor ddA = new DimensionsDescriptor(new CustomTimeBucket(TimeBucket.MINUTE, 5L), new Fields(Sets.newHashSet("a", "b")));
DimensionsDescriptor ddB = new DimensionsDescriptor(new CustomTimeBucket(TimeBucket.MINUTE, 5L), new Fields(Sets.newHashSet("a", "b")));
Assert.assertTrue(ddB.equals(ddA));
}
use of org.apache.apex.malhar.lib.appdata.schemas.Fields in project apex-malhar by apache.
the class DimensionsDescriptor method initialize.
/**
* Initializes the dimensions combination with the given aggregation string.
*
* @param aggregationString
* The aggregation string with which to initialize this dimensions
* combination.
*/
private void initialize(String aggregationString) {
String[] fieldArray = aggregationString.split(DELIMETER_SEPERATOR);
Set<String> fieldSet = Sets.newHashSet();
for (String field : fieldArray) {
String[] fieldAndValue = field.split(DELIMETER_EQUALS);
String fieldName = fieldAndValue[0];
if (fieldName.equals(DIMENSION_TIME_BUCKET)) {
throw new IllegalArgumentException(DIMENSION_TIME_BUCKET + " is an invalid time.");
}
if (!fieldName.equals(DIMENSION_TIME)) {
fieldSet.add(fieldName);
}
if (fieldName.equals(DIMENSION_TIME)) {
if (timeBucket != null) {
throw new IllegalArgumentException("Cannot specify time in a dimensions " + "descriptor when a timebucket is also " + "specified.");
}
if (fieldAndValue.length == 2) {
timeBucket = TimeBucket.TIME_UNIT_TO_TIME_BUCKET.get(TimeUnit.valueOf(fieldAndValue[1]));
}
}
}
fields = new Fields(fieldSet);
}
use of org.apache.apex.malhar.lib.appdata.schemas.Fields in project apex-malhar by apache.
the class AggregatorAverage method aggregate.
@Override
public GPOMutable aggregate(GPOMutable... aggregates) {
Preconditions.checkArgument(aggregates.length == getChildAggregators().size(), "The number of arguments " + aggregates.length + " should be the same as the number of child aggregators " + getChildAggregators().size());
GPOMutable sumAggregation = aggregates[SUM_INDEX];
GPOMutable countAggregation = aggregates[COUNT_INDEX];
FieldsDescriptor fieldsDescriptor = sumAggregation.getFieldDescriptor();
Fields fields = fieldsDescriptor.getFields();
GPOMutable result = new GPOMutable(AggregatorUtils.getOutputFieldsDescriptor(fields, this));
long count = countAggregation.getFieldsLong()[0];
for (String field : fields.getFields()) {
Type type = sumAggregation.getFieldDescriptor().getType(field);
switch(type) {
case BYTE:
{
double val = ((double) sumAggregation.getFieldByte(field)) / ((double) count);
result.setField(field, val);
break;
}
case SHORT:
{
double val = ((double) sumAggregation.getFieldShort(field)) / ((double) count);
result.setField(field, val);
break;
}
case INTEGER:
{
double val = ((double) sumAggregation.getFieldInt(field)) / ((double) count);
result.setField(field, val);
break;
}
case LONG:
{
double val = ((double) sumAggregation.getFieldLong(field)) / ((double) count);
result.setField(field, val);
break;
}
case FLOAT:
{
double val = sumAggregation.getFieldFloat(field) / ((double) count);
result.setField(field, val);
break;
}
case DOUBLE:
{
double val = sumAggregation.getFieldDouble(field) / ((double) count);
result.setField(field, val);
break;
}
default:
{
throw new UnsupportedOperationException("The type " + type + " is not supported.");
}
}
}
return result;
}
Aggregations