use of uk.gov.gchq.gaffer.operation.impl.add.AddElements in project gaffer-doc by gchq.
the class AddElementsExample method addElements.
public void addElements() throws OperationException {
print("### " + getMethodNameAsSentence(0) + "\n");
printSimpleGraphAsAscii();
final AddElements operation = new AddElements.Builder().input(new Entity.Builder().group("entity").vertex(6).property("count", 1).build(), new Edge.Builder().group("edge").source(5).dest(6).directed(true).property("count", 1).build()).build();
printJavaJsonPython(operation, "new AddElements.Builder()\n" + " .input(new Entity.Builder()\n" + " .group(\"entity\")\n" + " .vertex(6)\n" + " .property(\"count\", 1)\n" + " .build(),\n" + " new Edge.Builder()\n" + " .group(\"edge\")\n" + " .source(5).dest(6).directed(true)\n" + " .property(\"count\", 1)\n" + " .build())\n" + " .build();");
getGraph().execute(operation, new User("user01"));
print("Updated graph:");
print("```");
print("");
print(" --> 4 <--");
print(" / ^ \\");
print(" / | \\");
print("1 --> 2 --> 3");
print(" \\");
print(" --> 5 --> 6");
print("```");
}
use of uk.gov.gchq.gaffer.operation.impl.add.AddElements in project gaffer-doc by gchq.
the class Cardinalities method run.
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 RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator()).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 edges
// ---------------------------------------------------------
final CloseableIterable<? extends Element> edges = graph.execute(new GetAllElements(), user);
// ---------------------------------------------------------
print("\nAll edges:");
for (final Element edge : edges) {
print("GET_ALL_EDGES_RESULT", edge.toString());
}
// [get all cardinalities] Get all cardinalities
// ---------------------------------------------------------
final GetAllElements getAllCardinalities = new GetAllElements.Builder().view(new View.Builder().entity("Cardinality").build()).build();
// ---------------------------------------------------------
final CloseableIterable<? extends Element> allCardinalities = graph.execute(getAllCardinalities, user);
print("\nAll cardinalities");
for (final Element cardinality : allCardinalities) {
final String edgeGroup = cardinality.getProperty("edgeGroup").toString();
print("ALL_CARDINALITIES_RESULT", "Vertex " + ((Entity) cardinality).getVertex() + " " + edgeGroup + ": " + ((HyperLogLogPlus) cardinality.getProperty("hllp")).cardinality());
}
// [get all summarised cardinalities] Get all summarised cardinalities over all edges
// ---------------------------------------------------------
final GetAllElements getAllSummarisedCardinalities = new GetAllElements.Builder().view(new View.Builder().entity("Cardinality", new ViewElementDefinition.Builder().groupBy().build()).build()).build();
// ---------------------------------------------------------
final CloseableIterable<? extends Element> allSummarisedCardinalities = graph.execute(getAllSummarisedCardinalities, user);
print("\nAll summarised cardinalities");
for (final Element cardinality : allSummarisedCardinalities) {
final String edgeGroup = cardinality.getProperty("edgeGroup").toString();
print("ALL_SUMMARISED_CARDINALITIES_RESULT", "Vertex " + ((Entity) cardinality).getVertex() + " " + edgeGroup + ": " + ((HyperLogLogPlus) cardinality.getProperty("hllp")).cardinality());
}
// [get roaduse edge cardinality 10] Get the cardinality value at vertex 10 for RoadUse edges
// ---------------------------------------------------------
final GetElements getCardinalities = new GetElements.Builder().input(new EntitySeed("10")).view(new View.Builder().entity("Cardinality", new ViewElementDefinition.Builder().preAggregationFilter(new ElementFilter.Builder().select("edgeGroup").execute(new IsEqual(CollectionUtil.treeSet("RoadUse"))).build()).build()).build()).build();
// ---------------------------------------------------------
final Element roadUse10Cardinality;
try (final CloseableIterable<? extends Element> elements = graph.execute(getCardinalities, user)) {
roadUse10Cardinality = elements.iterator().next();
}
print("\nRoadUse edge cardinality at vertex 10:");
final String edgeGroup = (roadUse10Cardinality.getProperty("edgeGroup")).toString();
print("CARDINALITY_OF_10_RESULT", "Vertex " + ((Entity) roadUse10Cardinality).getVertex() + " " + edgeGroup + ": " + ((HyperLogLogPlus) roadUse10Cardinality.getProperty("hllp")).cardinality());
// [get 2 hops with a cardinality filter] 2 hops with a cardinality filter
// ---------------------------------------------------------
final OperationChain<CloseableIterable<? extends Element>> twoHopsWithCardinalityFilter = new OperationChain.Builder().first(new GetElements.Builder().input(new EntitySeed("M5")).view(new View.Builder().edge("RoadHasJunction").build()).build()).then(new GetElements.Builder().view(new View.Builder().entity("Cardinality", new ViewElementDefinition.Builder().preAggregationFilter(new ElementFilter.Builder().select("edgeGroup").execute(new IsEqual(CollectionUtil.treeSet("RoadUse"))).build()).groupBy().postAggregationFilter(new ElementFilter.Builder().select("hllp").execute(new HyperLogLogPlusIsLessThan(5)).build()).build()).build()).build()).then(new GetElements.Builder().view(new View.Builder().edge("RoadUse").build()).build()).build();
// ---------------------------------------------------------
final CloseableIterable<? extends Element> twoHopsWithCardinalityFilterResult = graph.execute(twoHopsWithCardinalityFilter, user);
print("\n2 hops with cardinality filter result:");
for (final Element element : twoHopsWithCardinalityFilterResult) {
print("2_HOP_RESULT", element.toString());
}
return allSummarisedCardinalities;
}
use of uk.gov.gchq.gaffer.operation.impl.add.AddElements in project gaffer-doc by gchq.
the class FullExample method run.
@Override
public Iterable<? extends String> 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");
// ---------------------------------------------------------
try (final CSVParser parser = CSVParser.parse(getClass().getResource("/FullExample/data.txt"), Charset.defaultCharset(), CSVFormat.DEFAULT.withFirstRecordAsHeader())) {
final OperationChain<Void> addOpChain = new OperationChain.Builder().first(new GenerateElements.Builder<CSVRecord>().generator(new RoadTrafficCsvElementGenerator()).input(parser).build()).then(new AddElements()).build();
graph.execute(addOpChain, user);
}
// ---------------------------------------------------------
print("The elements have been added.");
// [get] Get all road junctions in the South West that were heavily used by buses in the year 2000.
// ---------------------------------------------------------
final OperationChain<Iterable<? extends String>> opChain = new OperationChain.Builder().first(new GetAdjacentIds.Builder().input(new EntitySeed("South West")).view(new View.Builder().edge("RegionContainsLocation").build()).build()).then(new GetAdjacentIds.Builder().view(new View.Builder().edge("LocationContainsRoad").build()).build()).then(new ToSet<>()).then(new GetAdjacentIds.Builder().view(new View.Builder().edge("RoadHasJunction").build()).build()).then(new GetElements.Builder().view(new View.Builder().globalElements(new GlobalViewElementDefinition.Builder().groupBy().build()).entity("JunctionUse", new ViewElementDefinition.Builder().preAggregationFilter(new ElementFilter.Builder().select("startDate", "endDate").execute(new InDateRangeDual.Builder().start("2000/01/01").end("2001/01/01").build()).build()).postAggregationFilter(new ElementFilter.Builder().select("countByVehicleType").execute(new PredicateMap<>("BUS", new IsMoreThan(1000L))).build()).transientProperty("busCount", Long.class).transformer(new ElementTransformer.Builder().select("countByVehicleType").execute(new FreqMapExtractor("BUS")).project("busCount").build()).build()).build()).inOutType(SeededGraphFilters.IncludeIncomingOutgoingType.OUTGOING).build()).then(new Sort.Builder().comparators(new ElementPropertyComparator.Builder().groups("JunctionUse").property("busCount").reverse(true).build()).resultLimit(2).deduplicate(true).build()).then(new ToCsv.Builder().generator(new CsvGenerator.Builder().vertex("Junction").property("busCount", "Bus Count").build()).build()).build();
// ---------------------------------------------------------
printJsonAndPython("GET", opChain);
final Iterable<? extends String> results = graph.execute(opChain, user);
print("\nAll road junctions in the South West that were heavily used by buses in year 2000.");
for (final String result : results) {
print("RESULT", result);
}
return results;
}
use of uk.gov.gchq.gaffer.operation.impl.add.AddElements in project gaffer-doc by gchq.
the class TheBasics method run.
@Override
public CloseableIterable<? extends Element> run() throws OperationException, IOException {
// [generate] Create some edges from the simple data file using our Road Use generator class
// ---------------------------------------------------------
final List<Element> elements = new ArrayList<>();
final RoadUseElementGenerator dataGenerator = new RoadUseElementGenerator();
for (final String line : IOUtils.readLines(StreamUtil.openStream(getClass(), dataPath))) {
elements.add(dataGenerator._apply(line));
}
// ---------------------------------------------------------
print("Elements generated from the data file.");
for (final Element element : elements) {
print("GENERATED_EDGES", element.toString());
}
print("");
Graph graph;
// [graph from files] Create a graph using config, schema and store properties files
// ---------------------------------------------------------
graph = new Graph.Builder().config(StreamUtil.openStream(getClass(), graphConfigPath)).addSchemas(StreamUtil.openStreams(getClass(), schemaPath)).storeProperties(StreamUtil.openStream(getClass(), storePropertiesPath)).build();
// ---------------------------------------------------------
// [graph] Create a graph using config, schema and store properties from java
// ---------------------------------------------------------
final GraphConfig config = new GraphConfig.Builder().graphId(getClass().getSimpleName()).build();
final Schema schema = new Schema.Builder().edge("RoadUse", new SchemaEdgeDefinition.Builder().description("A directed edge representing vehicles moving from junction A to junction B.").source("junction").destination("junction").directed("true").property("count", "count.long").build()).type("junction", new TypeDefinition.Builder().description("A road junction represented by a String.").clazz(String.class).build()).type("count.long", new TypeDefinition.Builder().description("A long count that must be greater than or equal to 0.").clazz(Long.class).validateFunctions(new IsMoreThan(0L, true)).aggregateFunction(new Sum()).build()).type("true", new TypeDefinition.Builder().description("A simple boolean that must always be true.").clazz(Boolean.class).validateFunctions(new IsTrue()).build()).build();
final AccumuloProperties properties = new AccumuloProperties();
properties.setStoreClass(SingleUseMockAccumuloStore.class);
properties.setInstance("instance1");
properties.setZookeepers("zookeeper1");
properties.setUser("user01");
properties.setPassword("password");
graph = new Graph.Builder().config(config).addSchema(schema).storeProperties(properties).build();
// ---------------------------------------------------------
// [user] Create a user
// ---------------------------------------------------------
final User user = new User("user01");
// ---------------------------------------------------------
// [add] Add the edges to the graph
// ---------------------------------------------------------
final AddElements addElements = new AddElements.Builder().input(elements).build();
graph.execute(addElements, user);
// ---------------------------------------------------------
print("The elements have been added.");
// [get] Get all the edges that contain the vertex "10"
// ---------------------------------------------------------
final GetElements query = new GetElements.Builder().input(new EntitySeed("10")).view(new View.Builder().edge("RoadUse").build()).build();
final CloseableIterable<? extends Element> results = graph.execute(query, user);
// ---------------------------------------------------------
print("\nAll edges containing the vertex 10. The counts have been aggregated.");
for (final Element e : results) {
print("GET_ELEMENTS_RESULT", e.toString());
}
return results;
}
use of uk.gov.gchq.gaffer.operation.impl.add.AddElements 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