Search in sources :

Example 1 with AggregationElement

use of org.apache.rya.api.function.aggregation.AggregationElement in project incubator-rya by apache.

the class FluoQueryMetadataDAOIT method aggregationMetadataTest_noGroupByVarOrders.

@Test
public void aggregationMetadataTest_noGroupByVarOrders() {
    final FluoQueryMetadataDAO dao = new FluoQueryMetadataDAO();
    // Create the object that will be serialized.
    final AggregationMetadata originalMetadata = AggregationMetadata.builder("nodeId").setVarOrder(new VariableOrder("totalCount")).setParentNodeId("parentNodeId").setChildNodeId("childNodeId").addAggregation(new AggregationElement(AggregationType.COUNT, "count", "totalCount")).addAggregation(new AggregationElement(AggregationType.AVERAGE, "privae", "avgPrice")).build();
    try (FluoClient fluoClient = FluoFactory.newClient(super.getFluoConfiguration())) {
        // Write it to the Fluo table.
        try (Transaction tx = fluoClient.newTransaction()) {
            dao.write(tx, originalMetadata);
            tx.commit();
        }
        // Read it from the Fluo table.
        AggregationMetadata storedMetadata = null;
        try (Snapshot sx = fluoClient.newSnapshot()) {
            storedMetadata = dao.readAggregationMetadata(sx, "nodeId");
        }
        // Ensure the deserialized object is the same as the serialized one.
        assertEquals(originalMetadata, storedMetadata);
    }
}
Also used : Snapshot(org.apache.fluo.api.client.Snapshot) AggregationElement(org.apache.rya.api.function.aggregation.AggregationElement) FluoClient(org.apache.fluo.api.client.FluoClient) Transaction(org.apache.fluo.api.client.Transaction) VariableOrder(org.apache.rya.indexing.pcj.storage.accumulo.VariableOrder) Test(org.junit.Test)

Example 2 with AggregationElement

use of org.apache.rya.api.function.aggregation.AggregationElement in project incubator-rya by apache.

the class FluoQueryMetadataDAO method readAggregationMetadataBuilder.

private AggregationMetadata.Builder readAggregationMetadataBuilder(final SnapshotBase sx, final String nodeId) {
    requireNonNull(sx);
    requireNonNull(nodeId);
    // Fetch the values from the Fluo table.
    final String rowId = nodeId;
    final Map<Column, String> values = sx.gets(rowId, FluoQueryColumns.AGGREGATION_VARIABLE_ORDER, FluoQueryColumns.AGGREGATION_PARENT_NODE_ID, FluoQueryColumns.AGGREGATION_CHILD_NODE_ID, FluoQueryColumns.AGGREGATION_GROUP_BY_BINDING_NAMES);
    // Return an object holding them.
    final String varOrderString = values.get(FluoQueryColumns.AGGREGATION_VARIABLE_ORDER);
    final VariableOrder varOrder = new VariableOrder(varOrderString);
    final String parentNodeId = values.get(FluoQueryColumns.AGGREGATION_PARENT_NODE_ID);
    final String childNodeId = values.get(FluoQueryColumns.AGGREGATION_CHILD_NODE_ID);
    // Read the Group By variable order if one was present.
    final String groupByString = values.get(FluoQueryColumns.AGGREGATION_GROUP_BY_BINDING_NAMES);
    final VariableOrder groupByVars = groupByString.isEmpty() ? new VariableOrder() : new VariableOrder(groupByString.split(";"));
    // Deserialize the collection of AggregationElements.
    final Bytes aggBytes = sx.get(Bytes.of(nodeId.getBytes(Charsets.UTF_8)), FluoQueryColumns.AGGREGATION_AGGREGATIONS);
    final Collection<AggregationElement> aggregations;
    try (final ValidatingObjectInputStream vois = new ValidatingObjectInputStream(aggBytes.toInputStream())) // // this is how you find classes that you missed in the vois.accept() list, below.
    // { @Override protected void invalidClassNameFound(String className) throws java.io.InvalidClassException {
    // System.out.println("vois.accept(" + className + ".class, ");};};
    {
        // These classes are allowed to be deserialized. Others throw InvalidClassException.
        vois.accept(java.util.ArrayList.class, java.lang.Enum.class, AggregationElement.class, AggregationType.class);
        final Object object = vois.readObject();
        if (!(object instanceof Collection<?>)) {
            throw new InvalidClassException("Object read was not of type Collection. It was: " + object.getClass());
        }
        aggregations = (Collection<AggregationElement>) object;
    } catch (final IOException | ClassNotFoundException e) {
        throw new RuntimeException("Problem encountered while reading AggregationMetadata from the Fluo table. Unable " + "to deserialize the AggregationElements from a byte[].", e);
    }
    final AggregationMetadata.Builder builder = AggregationMetadata.builder(nodeId).setVarOrder(varOrder).setParentNodeId(parentNodeId).setChildNodeId(childNodeId).setGroupByVariableOrder(groupByVars);
    for (final AggregationElement aggregation : aggregations) {
        builder.addAggregation(aggregation);
    }
    return builder;
}
Also used : AggregationElement(org.apache.rya.api.function.aggregation.AggregationElement) VariableOrder(org.apache.rya.indexing.pcj.storage.accumulo.VariableOrder) InvalidClassException(java.io.InvalidClassException) ValidatingObjectInputStream(org.apache.commons.io.serialization.ValidatingObjectInputStream) IOException(java.io.IOException) Bytes(org.apache.fluo.api.data.Bytes) Column(org.apache.fluo.api.data.Column) Collection(java.util.Collection)

Example 3 with AggregationElement

use of org.apache.rya.api.function.aggregation.AggregationElement in project incubator-rya by apache.

the class AggregationMetadata method toString.

@Override
public String toString() {
    final StringBuilder string = new StringBuilder().append("AggregationMetadata {\n").append("    Node ID: " + super.getNodeId() + "\n").append("    Variable Order: " + super.getVariableOrder() + "\n").append("    Parent Node ID: " + parentNodeId + "\n").append("    Child Node ID: " + childNodeId + "\n");
    // Only print the group by names if they're preesnt.
    if (!groupByVariables.getVariableOrders().isEmpty()) {
        string.append("    GroupBy Variable Order: " + groupByVariables + "\n");
    }
    // Print each of the AggregationElements.
    string.append("    Aggregations: {\n");
    final Iterator<AggregationElement> it = aggregations.iterator();
    while (it.hasNext()) {
        final AggregationElement agg = it.next();
        string.append("        Type: " + agg.getAggregationType() + "\n");
        string.append("        Aggregated Binding Name: " + agg.getAggregatedBindingName() + "\n");
        string.append("        Result Binding Name: " + agg.getResultBindingName() + "\n");
        if (it.hasNext()) {
            string.append("\n");
        }
    }
    string.append("    }\n");
    string.append("}");
    return string.toString();
}
Also used : AggregationElement(org.apache.rya.api.function.aggregation.AggregationElement)

Example 4 with AggregationElement

use of org.apache.rya.api.function.aggregation.AggregationElement in project incubator-rya by apache.

the class FluoQueryMetadataDAOIT method aggregationMetadataTest_withGroupByVarOrders.

@Test
public void aggregationMetadataTest_withGroupByVarOrders() {
    final FluoQueryMetadataDAO dao = new FluoQueryMetadataDAO();
    // Create the object that will be serialized.
    final AggregationMetadata originalMetadata = AggregationMetadata.builder("nodeId").setVarOrder(new VariableOrder("totalCount")).setParentNodeId("parentNodeId").setChildNodeId("childNodeId").setGroupByVariableOrder(new VariableOrder("a", "b", "c")).addAggregation(new AggregationElement(AggregationType.COUNT, "count", "totalCount")).addAggregation(new AggregationElement(AggregationType.AVERAGE, "privae", "avgPrice")).build();
    try (FluoClient fluoClient = FluoFactory.newClient(super.getFluoConfiguration())) {
        // Write it to the Fluo table.
        try (Transaction tx = fluoClient.newTransaction()) {
            dao.write(tx, originalMetadata);
            tx.commit();
        }
        // Read it from the Fluo table.
        AggregationMetadata storedMetadata = null;
        try (Snapshot sx = fluoClient.newSnapshot()) {
            storedMetadata = dao.readAggregationMetadata(sx, "nodeId");
        }
    }
}
Also used : Snapshot(org.apache.fluo.api.client.Snapshot) AggregationElement(org.apache.rya.api.function.aggregation.AggregationElement) FluoClient(org.apache.fluo.api.client.FluoClient) Transaction(org.apache.fluo.api.client.Transaction) VariableOrder(org.apache.rya.indexing.pcj.storage.accumulo.VariableOrder) Test(org.junit.Test)

Example 5 with AggregationElement

use of org.apache.rya.api.function.aggregation.AggregationElement in project incubator-rya by apache.

the class AggregationResultUpdater method updateAggregateResults.

/**
 * Updates the results of an Aggregation node where its child has emitted a new Binding Set.
 *
 * @param tx - The transaction all Fluo queries will use. (not null)
 * @param childBindingSet - The Binding Set that was omitted by the Aggregation Node's child. (not null)
 * @param aggregationMetadata - The metadata of the Aggregation node whose results will be updated. (not null)
 * @throws Exception The update could not be successfully performed.
 */
public void updateAggregateResults(final TransactionBase tx, final VisibilityBindingSet childBindingSet, final AggregationMetadata aggregationMetadata) throws Exception {
    requireNonNull(tx);
    requireNonNull(childBindingSet);
    requireNonNull(aggregationMetadata);
    log.trace("Transaction ID: " + tx.getStartTimestamp() + "\n" + "Child Binding Set:\n" + childBindingSet + "\n");
    // The Row ID for the Aggregation State that needs to be updated is defined by the Group By variables.
    final String aggregationNodeId = aggregationMetadata.getNodeId();
    final VariableOrder groupByVars = aggregationMetadata.getGroupByVariableOrder();
    final Bytes rowId = makeRowKey(aggregationNodeId, groupByVars, childBindingSet);
    // Load the old state from the bytes if one was found; otherwise initialize the state.
    final Optional<Bytes> stateBytes = Optional.ofNullable(tx.get(rowId, FluoQueryColumns.AGGREGATION_BINDING_SET));
    final AggregationState state;
    if (stateBytes.isPresent()) {
        // Deserialize the old state
        final byte[] bytes = stateBytes.get().toArray();
        state = AGG_STATE_SERDE.deserialize(bytes);
    } else {
        // Initialize a new state.
        state = new AggregationState();
        // If we have group by bindings, their values need to be added to the state's binding set.
        final MapBindingSet bindingSet = state.getBindingSet();
        for (final String variable : aggregationMetadata.getGroupByVariableOrder()) {
            bindingSet.addBinding(childBindingSet.getBinding(variable));
        }
    }
    log.trace("Transaction ID: " + tx.getStartTimestamp() + "\n" + "Before Update: " + LogUtils.clean(state.getBindingSet().toString()) + "\n");
    // Update the visibilities of the result binding set based on the child's visibilities.
    final String oldVisibility = state.getVisibility();
    final String updateVisibilities = VisibilitySimplifier.unionAndSimplify(oldVisibility, childBindingSet.getVisibility());
    state.setVisibility(updateVisibilities);
    // Update the Aggregation State with each Aggregation function included within this group.
    for (final AggregationElement aggregation : aggregationMetadata.getAggregations()) {
        final AggregationType type = aggregation.getAggregationType();
        final AggregationFunction function = FUNCTIONS.get(type);
        if (function == null) {
            throw new RuntimeException("Unrecognized aggregation function: " + type);
        }
        function.update(aggregation, state, childBindingSet);
    }
    log.trace("Transaction ID: " + tx.getStartTimestamp() + "\n" + "After Update:" + LogUtils.clean(state.getBindingSet().toString()) + "\n");
    // Store the updated state. This will write on top of any old state that was present for the Group By values.
    tx.set(rowId, FluoQueryColumns.AGGREGATION_BINDING_SET, Bytes.of(AGG_STATE_SERDE.serialize(state)));
}
Also used : AggregationFunction(org.apache.rya.api.function.aggregation.AggregationFunction) Bytes(org.apache.fluo.api.data.Bytes) AggregationElement(org.apache.rya.api.function.aggregation.AggregationElement) VariableOrder(org.apache.rya.indexing.pcj.storage.accumulo.VariableOrder) MapBindingSet(org.openrdf.query.impl.MapBindingSet) AggregationState(org.apache.rya.api.function.aggregation.AggregationState) AggregationType(org.apache.rya.api.function.aggregation.AggregationType)

Aggregations

AggregationElement (org.apache.rya.api.function.aggregation.AggregationElement)5 VariableOrder (org.apache.rya.indexing.pcj.storage.accumulo.VariableOrder)4 FluoClient (org.apache.fluo.api.client.FluoClient)2 Snapshot (org.apache.fluo.api.client.Snapshot)2 Transaction (org.apache.fluo.api.client.Transaction)2 Bytes (org.apache.fluo.api.data.Bytes)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 InvalidClassException (java.io.InvalidClassException)1 Collection (java.util.Collection)1 ValidatingObjectInputStream (org.apache.commons.io.serialization.ValidatingObjectInputStream)1 Column (org.apache.fluo.api.data.Column)1 AggregationFunction (org.apache.rya.api.function.aggregation.AggregationFunction)1 AggregationState (org.apache.rya.api.function.aggregation.AggregationState)1 AggregationType (org.apache.rya.api.function.aggregation.AggregationType)1 MapBindingSet (org.openrdf.query.impl.MapBindingSet)1