Search in sources :

Example 16 with FieldsDescriptor

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);
}
Also used : Type(org.apache.apex.malhar.lib.appdata.schemas.Type) FieldsDescriptor(org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor)

Example 17 with FieldsDescriptor

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;
}
Also used : Type(org.apache.apex.malhar.lib.appdata.schemas.Type) JSONObject(org.codehaus.jettison.json.JSONObject) FieldsDescriptor(org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor)

Example 18 with FieldsDescriptor

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;
}
Also used : Type(org.apache.apex.malhar.lib.appdata.schemas.Type) FieldsDescriptor(org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor)

Example 19 with FieldsDescriptor

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);
    }
}
Also used : GPOMutable(org.apache.apex.malhar.lib.appdata.gpo.GPOMutable) SQLException(java.sql.SQLException) EventKey(org.apache.apex.malhar.lib.dimensions.DimensionsEvent.EventKey) FieldsDescriptor(org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor) PreparedStatement(java.sql.PreparedStatement)

Example 20 with FieldsDescriptor

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);
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) FieldsDescriptor(org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor) DimensionalConfigurationSchema(org.apache.apex.malhar.lib.appdata.schemas.DimensionalConfigurationSchema) PreparedStatement(java.sql.PreparedStatement) Map(java.util.Map) Int2ObjectMap(it.unimi.dsi.fastutil.ints.Int2ObjectMap)

Aggregations

FieldsDescriptor (org.apache.apex.malhar.lib.appdata.schemas.FieldsDescriptor)24 Type (org.apache.apex.malhar.lib.appdata.schemas.Type)19 Test (org.junit.Test)10 Map (java.util.Map)5 GPOMutable (org.apache.apex.malhar.lib.appdata.gpo.GPOMutable)4 MutableInt (org.apache.commons.lang3.mutable.MutableInt)4 JSONObject (org.codehaus.jettison.json.JSONObject)3 PreparedStatement (java.sql.PreparedStatement)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 EventKey (org.apache.apex.malhar.lib.dimensions.DimensionsEvent.EventKey)2 Int2ObjectMap (it.unimi.dsi.fastutil.ints.Int2ObjectMap)1 Set (java.util.Set)1 Serde (org.apache.apex.malhar.lib.appdata.gpo.Serde)1 DimensionalConfigurationSchema (org.apache.apex.malhar.lib.appdata.schemas.DimensionalConfigurationSchema)1 Fields (org.apache.apex.malhar.lib.appdata.schemas.Fields)1