Search in sources :

Example 1 with DataGenerator3

use of uk.gov.gchq.gaffer.example.gettingstarted.generator.DataGenerator3 in project Gaffer by gchq.

the class LoadAndQuery3 method run.

public CloseableIterable<Edge> run() throws OperationException {
    // [user] Create a user
    // ---------------------------------------------------------
    final User user = new User("user01");
    // ---------------------------------------------------------
    // [generate] create some edges from the data file using our data generator class
    // ---------------------------------------------------------
    final List<Element> elements = new ArrayList<>();
    final DataGenerator3 dataGenerator = new DataGenerator3();
    for (final String s : DataUtils.loadData(getData())) {
        elements.add(dataGenerator.getElement(s));
    }
    // ---------------------------------------------------------
    log("Elements generated from the data file.");
    for (final Element element : elements) {
        log("GENERATED_EDGES", element.toString());
    }
    log("");
    // [graph] create a graph using our schema and store properties
    // ---------------------------------------------------------
    final Graph graph = new Graph.Builder().addSchemas(getSchemas()).storeProperties(getStoreProperties()).build();
    // ---------------------------------------------------------
    // [add] add the edges to the graph
    // ---------------------------------------------------------
    final AddElements addElements = new AddElements.Builder().elements(elements).build();
    graph.execute(addElements, user);
    // ---------------------------------------------------------
    log("The elements have been added.\n");
    log("\nAll edges containing the vertex 1. The counts have been aggregated\n");
    // [get simple] get all the edges that contain the vertex "1"
    // ---------------------------------------------------------
    final GetEdges<EntitySeed> getRelatedEdges = new GetEdges.Builder<EntitySeed>().addSeed(new EntitySeed("1")).build();
    final CloseableIterable<Edge> results = graph.execute(getRelatedEdges, user);
    // ---------------------------------------------------------
    for (final Element e : results) {
        log("GET_RELATED_EDGES_RESULT", e.toString());
    }
    // [get] rerun previous query with a filter to return only edges with a count more than 3
    // ---------------------------------------------------------
    final View view = new View.Builder().edge("data", new ViewElementDefinition.Builder().preAggregationFilter(new ElementFilter.Builder().select("count").execute(new IsMoreThan(3)).build()).build()).build();
    final GetEdges<EntitySeed> getRelatedEdgesWithCountMoreThan3 = new GetEdges.Builder<EntitySeed>().addSeed(new EntitySeed("1")).view(view).build();
    final CloseableIterable<Edge> filteredResults = graph.execute(getRelatedEdgesWithCountMoreThan3, user);
    // ---------------------------------------------------------
    log("\nAll edges containing the vertex 1 with an aggregated count more than than 3\n");
    for (final Element e : filteredResults) {
        log("GET_RELATED_ELEMENTS_WITH_COUNT_MORE_THAN_3_RESULT", e.toString());
    }
    return filteredResults;
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) User(uk.gov.gchq.gaffer.user.User) Element(uk.gov.gchq.gaffer.data.element.Element) ArrayList(java.util.ArrayList) DataGenerator3(uk.gov.gchq.gaffer.example.gettingstarted.generator.DataGenerator3) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) Graph(uk.gov.gchq.gaffer.graph.Graph) GetEdges(uk.gov.gchq.gaffer.operation.impl.get.GetEdges) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) IsMoreThan(uk.gov.gchq.gaffer.function.filter.IsMoreThan) Edge(uk.gov.gchq.gaffer.data.element.Edge)

Aggregations

ArrayList (java.util.ArrayList)1 Edge (uk.gov.gchq.gaffer.data.element.Edge)1 Element (uk.gov.gchq.gaffer.data.element.Element)1 ElementFilter (uk.gov.gchq.gaffer.data.element.function.ElementFilter)1 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)1 DataGenerator3 (uk.gov.gchq.gaffer.example.gettingstarted.generator.DataGenerator3)1 IsMoreThan (uk.gov.gchq.gaffer.function.filter.IsMoreThan)1 Graph (uk.gov.gchq.gaffer.graph.Graph)1 EntitySeed (uk.gov.gchq.gaffer.operation.data.EntitySeed)1 AddElements (uk.gov.gchq.gaffer.operation.impl.add.AddElements)1 GetEdges (uk.gov.gchq.gaffer.operation.impl.get.GetEdges)1 User (uk.gov.gchq.gaffer.user.User)1