use of uk.gov.gchq.gaffer.doc.user.generator.RoadAndRoadUseWithTimesElementGenerator in project gaffer-doc by gchq.
the class Aggregation method run.
@Override
public CloseableIterable<? extends Element> run() throws OperationException, IOException {
// [graph] create a graph using our schema and store properties
// ---------------------------------------------------------
final Graph graph = new Graph.Builder().config(getDefaultGraphConfig()).addSchemas(StreamUtil.openStreams(getClass(), schemaPath)).storeProperties(getDefaultStoreProperties()).build();
// ---------------------------------------------------------
// [user] Create a user
// ---------------------------------------------------------
final User user = new User("user01");
// ---------------------------------------------------------
// [add] Create a data generator and add the edges to the graph using an operation chain consisting of:
// generateElements - generating edges from the data (note these are directed edges)
// addElements - add the edges to the graph
// ---------------------------------------------------------
final OperationChain<Void> addOpChain = new OperationChain.Builder().first(new GenerateElements.Builder<String>().generator(new RoadAndRoadUseWithTimesElementGenerator()).input(IOUtils.readLines(StreamUtil.openStream(getClass(), dataPath))).build()).then(new AddElements()).build();
graph.execute(addOpChain, user);
// ---------------------------------------------------------
print("The elements have been added.");
// [get] Get all RoadUse edges
// ---------------------------------------------------------
final GetAllElements getAllRoadUseEdges = new GetAllElements.Builder().view(new View.Builder().edge("RoadUse").build()).build();
final CloseableIterable<? extends Element> roadUseElements = graph.execute(getAllRoadUseEdges, user);
// ---------------------------------------------------------
print("\nAll RoadUse edges in daily time buckets:");
for (final Element element : roadUseElements) {
print("GET_ALL_EDGES_RESULT", element.toString());
}
// [get all edges summarised] Get all edges summarised (merge all time windows together)
// This is achieved by overriding the 'groupBy' start and end time properties.
// ---------------------------------------------------------
final GetAllElements edgesSummarisedOperation = new GetAllElements.Builder().view(new View.Builder().edge("RoadUse", new ViewElementDefinition.Builder().groupBy().build()).build()).build();
final CloseableIterable<? extends Element> edgesSummarised = graph.execute(edgesSummarisedOperation, user);
// ---------------------------------------------------------
print("\nAll edges summarised:");
for (final Element edge : edgesSummarised) {
print("GET_ALL_EDGES_SUMMARISED_RESULT", edge.toString());
}
// [get all edges summarised in time window] Get all edges summarised over a provided 2 day time period
// This is achieved by overriding the 'groupBy' start and end time properties
// and providing a filter.
// ---------------------------------------------------------
final GetAllElements edgesSummarisedInTimeWindowOperation = new GetAllElements.Builder().view(new View.Builder().edge("RoadUse", new ViewElementDefinition.Builder().preAggregationFilter(new ElementFilter.Builder().select("startDate").execute(new IsMoreThan(MAY_01_2000, true)).select("endDate").execute(new IsLessThan(MAY_02_2000, false)).build()).groupBy().build()).build()).build();
final CloseableIterable<? extends Element> edgesSummarisedInTimeWindow = graph.execute(edgesSummarisedInTimeWindowOperation, user);
// ---------------------------------------------------------
print("\nEdges in 2 day time window:");
for (final Element edge : edgesSummarisedInTimeWindow) {
print("GET_ALL_EDGES_SUMMARISED_IN_TIME_WINDOW_RESULT", edge.toString());
}
// [get all edges summarised in time window with min count] Now a bit more advanced.
// At query time you can actually override the logic for how Gaffer
// aggregates properties together. So by default the count property
// is aggregated with Sum. At query time we could change that, so the
// count property was aggregated with Max.
// ---------------------------------------------------------
final GetAllElements edgesSummarisedInTimeWindowWithMinCountOperation = new GetAllElements.Builder().view(new View.Builder().edge("RoadUse", new ViewElementDefinition.Builder().preAggregationFilter(new ElementFilter.Builder().select("startDate").execute(new IsMoreThan(MAY_01_2000, true)).select("endDate").execute(new IsLessThan(MAY_03_2000, false)).build()).groupBy().aggregator(new ElementAggregator.Builder().select("count").execute(new Min()).build()).build()).build()).build();
printJson("GET_ALL_EDGES_SUMMARISED_IN_TIME_WINDOW_RESULT_WITH_MIN_COUNT", edgesSummarisedInTimeWindowWithMinCountOperation);
final CloseableIterable<? extends Element> edgesSummarisedInTimeWindowWithMinCount = graph.execute(edgesSummarisedInTimeWindowWithMinCountOperation, user);
// ---------------------------------------------------------
print("\nEdges in 3 day time window with min count:");
for (final Element edge : edgesSummarisedInTimeWindowWithMinCount) {
print("GET_ALL_EDGES_SUMMARISED_IN_TIME_WINDOW_RESULT_WITH_MIN_COUNT", edge.toString());
}
return edgesSummarisedInTimeWindowWithMinCount;
}
Aggregations