use of uk.gov.gchq.gaffer.operation.util.AggregatePair in project gaffer-doc by gchq.
the class AggregateExample method aggregateOnlyEdgesOfTypeEdgeWithATransientPropertyAndProvidedAggregator.
public void aggregateOnlyEdgesOfTypeEdgeWithATransientPropertyAndProvidedAggregator() {
// ---------------------------------------------------------
final String[] groupBy = {};
final Aggregate aggregate = new Aggregate.Builder().edge("edge", new AggregatePair(groupBy, new ElementAggregator.Builder().select("transientProperty1").execute(new StringConcat()).build())).build();
// ---------------------------------------------------------
showExample(aggregate, "The groupBy has been set to an empty array. This will override the groupBy value in the schema.");
}
use of uk.gov.gchq.gaffer.operation.util.AggregatePair in project Gaffer by gchq.
the class AggregateHandler method doOperation.
public Iterable<? extends Element> doOperation(final Aggregate operation, final Schema schema) throws OperationException {
if (null == operation.getInput()) {
throw new OperationException("Aggregate operation has null iterable of elements");
}
// all elements should be used. This matches the way a View works.
if (null == operation.getEntities() && null == operation.getEdges()) {
final Map<String, AggregatePair> entityMap = new HashMap<>();
schema.getEntityGroups().forEach(e -> entityMap.put(e, new AggregatePair()));
operation.setEntities(entityMap);
final Map<String, AggregatePair> edgeMap = new HashMap<>();
schema.getEdgeGroups().forEach(e -> edgeMap.put(e, new AggregatePair()));
operation.setEdges(edgeMap);
}
final ValidationResult result = validator.validate(operation, schema);
if (!result.isValid()) {
throw new OperationException("Aggregate operation is invalid. " + result.getErrorString());
}
return AggregatorUtil.queryAggregate(operation.getInput(), schema, buildView(operation));
}
use of uk.gov.gchq.gaffer.operation.util.AggregatePair in project Gaffer by gchq.
the class AggregateHandler method buildView.
private View buildView(final Aggregate operation) {
View.Builder builder = new View.Builder();
if (null != operation.getEntities()) {
for (final Map.Entry<String, AggregatePair> entry : operation.getEntities().entrySet()) {
final String group = entry.getKey();
final AggregatePair pair = entry.getValue();
builder = builder.entity(group, new ViewElementDefinition.Builder().groupBy(pair.getGroupBy()).aggregator(pair.getElementAggregator()).build());
}
}
if (null != operation.getEdges()) {
for (final Map.Entry<String, AggregatePair> entry : operation.getEdges().entrySet()) {
final String group = entry.getKey();
final AggregatePair pair = entry.getValue();
builder = builder.edge(group, new ViewElementDefinition.Builder().groupBy(pair.getGroupBy()).aggregator(pair.getElementAggregator()).build());
}
}
return builder.build();
}
use of uk.gov.gchq.gaffer.operation.util.AggregatePair in project Gaffer by gchq.
the class AggregateHandlerTest method shouldFailValidationWhenSchemaElementDefinitionsAreNull.
@Test
public void shouldFailValidationWhenSchemaElementDefinitionsAreNull() {
// Given
given(store.getSchema()).willReturn(new Schema());
input.add(edge);
input.add(edge1);
edges.put(TestGroups.EDGE, new AggregatePair());
final Aggregate aggregate = new Aggregate.Builder().input(input).edges(edges).build();
// When / Then
try {
final Iterable<? extends Element> results = handler.doOperation(aggregate, context, store);
} catch (final OperationException e) {
assertTrue(e.getMessage().contains("Edge group: " + TestGroups.EDGE + " does not exist in the schema."));
}
}
use of uk.gov.gchq.gaffer.operation.util.AggregatePair in project Gaffer by gchq.
the class AggregateHandlerTest method shouldAggregateElementsWhenNoGroupByInSchema.
@Test
public void shouldAggregateElementsWhenNoGroupByInSchema() throws OperationException {
// Given
final Schema schema = new Schema.Builder().entity(TestGroups.ENTITY, new SchemaEntityDefinition.Builder().build()).build();
given(store.getSchema()).willReturn(schema);
input.add(entity);
input.add(entity1);
input.add(entity2);
final AggregatePair pair = new AggregatePair(new String[] { "timestamp" }, new ElementAggregator.Builder().select("count").execute(new Sum()).build());
entities.put(TestGroups.ENTITY, pair);
final Entity expectedEntity = new Entity.Builder().group(TestGroups.ENTITY).property("timestamp", 3L).property("count", 5).build();
expected.add(expectedEntity);
expected.add(entity1);
final Aggregate aggregate = new Aggregate.Builder().input(input).entities(entities).build();
// When
final Iterable<? extends Element> results = handler.doOperation(aggregate, context, store);
final Set<Element> resultsSet = Sets.newHashSet(results);
// Then
assertEquals(expected, resultsSet);
}
Aggregations