Search in sources :

Example 1 with DataGenerator2

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

the class LoadAndQuery2 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 DataGenerator2 dataGenerator = new DataGenerator2();
    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");
    // [get simple] Get all the edges related to vertex 1
    // ---------------------------------------------------------
    final GetEdges<EntitySeed> getRelatedEdges = new GetEdges.Builder<EntitySeed>().addSeed(new EntitySeed("1")).build();
    final CloseableIterable<Edge> allColoursResults = graph.execute(getRelatedEdges, user);
    // ---------------------------------------------------------
    log("\nAll edges containing vertex 1");
    log("\nNotice that the edges are aggregated within their groups");
    for (final Element e : allColoursResults) {
        log("GET_RELATED_EDGES_RESULT", e.toString());
    }
    // [get] Rerun the previous query with a View to specify which subset of results we want
    // ---------------------------------------------------------
    final View view = new View.Builder().edge("red").build();
    final GetEdges<EntitySeed> getRelatedRedEdges = new GetEdges.Builder<EntitySeed>().addSeed(new EntitySeed("1")).view(view).build();
    final CloseableIterable<Edge> redResults = graph.execute(getRelatedRedEdges, user);
    // ---------------------------------------------------------
    log("\nAll red edges containing vertex 1\n");
    for (final Element e : redResults) {
        log("GET_RELATED_RED_EDGES_RESULT", e.toString());
    }
    return redResults;
}
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) DataGenerator2(uk.gov.gchq.gaffer.example.gettingstarted.generator.DataGenerator2) 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) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) 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 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)1 DataGenerator2 (uk.gov.gchq.gaffer.example.gettingstarted.generator.DataGenerator2)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