use of org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor in project apex-malhar by apache.
the class SerdeFieldsDescriptor method deserializeObject.
@Override
public synchronized Object deserializeObject(byte[] object, MutableInt offset) {
Map<String, Type> fieldToType = Maps.newHashMap();
int length = GPOUtils.deserializeInt(object, offset);
int startIndex = offset.intValue();
while (startIndex + length > offset.intValue()) {
Type type = Type.values()[GPOUtils.deserializeInt(object, offset)];
String value = GPOUtils.deserializeString(object, offset);
fieldToType.put(value, type);
}
return new FieldsDescriptor(fieldToType);
}
use of org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor in project apex-malhar by apache.
the class GPOUtils method serializeJSONObject.
/**
* This utility method serializes the given fields from the given {@link GPOMutable} object into JSON using the
* given resultFormatter.
* @param gpo The {@link GPOMutable} to serialize.
* @param fields The fields from the given {@link GPOMutable} object to serialize.
* @param resultFormatter The result formatter to use when formatting the output data during serialization.
* @return The serialized {@link GPOMutable} object.
* @throws JSONException
*/
public static JSONObject serializeJSONObject(GPOMutable gpo, Fields fields, ResultFormatter resultFormatter) throws JSONException {
JSONObject jo = new JSONObject();
FieldsDescriptor fd = gpo.getFieldDescriptor();
for (String field : fields.getFields()) {
Type fieldType = fd.getType(field);
GPOType gpoType = GPOType.GPO_TYPE_ARRAY[fieldType.ordinal()];
gpoType.serializeJSONObject(jo, gpo, field, resultFormatter);
}
return jo;
}
use of org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor in project apex-malhar by apache.
the class GPOUtils method serializedLength.
/**
* Computes the number of bytes required to serialize the given {@link GPOMutable} object. Excluding the
* object fields in the {@link GPOMutable}.
* @param gpo The {@link GPOMutable} object to compute a serialized size for.
* @return The serialized size for the given {@link GPOMutable} object.
*/
public static int serializedLength(GPOMutable gpo) {
int arrayLength = 0;
FieldsDescriptor fd = gpo.getFieldDescriptor();
List<Type> types = fd.getTypesList();
for (int typeIndex = 0; typeIndex < types.size(); typeIndex++) {
Type type = types.get(typeIndex);
switch(type) {
case STRING:
{
for (String val : gpo.getFieldsString()) {
arrayLength += Type.INTEGER.getByteSize();
arrayLength += val.getBytes().length;
}
break;
}
case OBJECT:
{
// Don't include objects.
break;
}
default:
{
arrayLength += fd.getTypeToFields().get(type).size() * type.getByteSize();
}
}
}
return arrayLength;
}
use of org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor in project apex-malhar by apache.
the class JDBCDimensionalOutputOperator method setStatementParameters.
/**
* Sets the parameters on the {@link java.sql.PreparedStatement} based on the
* values in the given {@link Aggregate}.
*
* @param aggregate
* The {@link Aggregate} whose values will be set on the
* corresponding {@link java.sql.PreparedStatement}.
*/
private void setStatementParameters(Aggregate aggregate) {
EventKey eventKey = aggregate.getEventKey();
int ddID = eventKey.getDimensionDescriptorID();
int aggID = eventKey.getAggregatorID();
LOG.info("Setting statement params {} {}", ddID, aggID);
FieldsDescriptor keyFD = schema.getDimensionsDescriptorIDToKeyDescriptor().get(ddID);
FieldsDescriptor aggFD = schema.getDimensionsDescriptorIDToAggregatorIDToOutputAggregatorDescriptor().get(ddID).get(aggID);
GPOMutable key = eventKey.getKey();
key.setFieldDescriptor(keyFD);
GPOMutable value = aggregate.getAggregates();
value.setFieldDescriptor(aggFD);
int qCounter = 1;
PreparedStatement ps = ddIDToAggIDToStatement.get(ddID).get(aggID);
try {
qCounter = setParams(ps, key, qCounter, true);
setParams(ps, value, qCounter, false);
ps.addBatch();
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
}
use of org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor in project apex-malhar by apache.
the class JDBCDimensionalOutputOperator method setup.
@Override
public void setup(Context.OperatorContext context) {
super.setup(context);
LOG.info("Done setting up super");
aggregatorRegistry.setup();
// Create prepared statements
schema = new DimensionalConfigurationSchema(eventSchema, aggregatorRegistry);
List<FieldsDescriptor> keyFDs = schema.getDimensionsDescriptorIDToKeyDescriptor();
for (int ddID = 0; ddID < keyFDs.size(); ddID++) {
LOG.info("ddID {}", ddID);
FieldsDescriptor keyFD = keyFDs.get(ddID);
Int2ObjectMap<FieldsDescriptor> aggIDToAggFD = schema.getDimensionsDescriptorIDToAggregatorIDToOutputAggregatorDescriptor().get(ddID);
Map<Integer, PreparedStatement> aggIDToStatement = ddIDToAggIDToStatement.get(ddID);
if (aggIDToStatement == null) {
aggIDToStatement = Maps.newHashMap();
ddIDToAggIDToStatement.put(ddID, aggIDToStatement);
}
for (Map.Entry<String, String> aggTable : tableNames.get(ddID).entrySet()) {
int aggID = aggregatorRegistry.getIncrementalAggregatorNameToID().get(aggTable.getKey());
LOG.info("aggID {}", aggID);
FieldsDescriptor aggFD = aggIDToAggFD.get(aggID);
List<String> keyNames = keyFD.getFieldList();
keyNames.remove(DimensionsDescriptor.DIMENSION_TIME_BUCKET);
LOG.info("List fields {}", keyNames);
List<String> aggregateNames = aggFD.getFieldList();
LOG.info("List fields {}", aggregateNames);
String tableName = aggTable.getValue();
String statementString = buildStatement(tableName, keyNames, aggregateNames);
try {
aggIDToStatement.put(aggID, store.getConnection().prepareStatement(statementString));
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
}
}
}
Aggregations