Search in sources :

Example 1 with MeanTransform

use of uk.gov.gchq.gaffer.example.gettingstarted.function.transform.MeanTransform in project Gaffer by gchq.

the class LoadAndQuery4 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 DataGenerator4 dataGenerator = new DataGenerator4();
    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 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);
    // ---------------------------------------------------------
    log("\nAll edges containing the vertex 1. The counts and 'things' have been aggregated\n");
    for (final Element e : results) {
        log("GET_RELATED_EDGES_RESULT", e.toString());
    }
    // rerun previous query but calculate a mean
    // [transform] Create a mean transient property using an element transformer
    // ---------------------------------------------------------
    final ElementTransformer mean = new ElementTransformer.Builder().select("thing", "count").project("mean").execute(new MeanTransform()).build();
    // ---------------------------------------------------------
    // [get] Add the element transformer to the view and run the query
    // ---------------------------------------------------------
    final View view = new View.Builder().edge("data", new ViewElementDefinition.Builder().transientProperty("mean", Float.class).transformer(mean).build()).build();
    final GetEdges<EntitySeed> getRelatedEdgesWithMean = new GetEdges.Builder<EntitySeed>().addSeed(new EntitySeed("1")).view(view).build();
    final CloseableIterable<Edge> transientResults = graph.execute(getRelatedEdgesWithMean, user);
    // ---------------------------------------------------------
    log("\nWe can add a new property to the edges that is calculated from the aggregated values of other properties\n");
    for (final Element e : transientResults) {
        log("GET_RELATED_ELEMENTS_WITH_MEAN_RESULT", e.toString());
    }
    return transientResults;
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) MeanTransform(uk.gov.gchq.gaffer.example.gettingstarted.function.transform.MeanTransform) User(uk.gov.gchq.gaffer.user.User) ElementTransformer(uk.gov.gchq.gaffer.data.element.function.ElementTransformer) Element(uk.gov.gchq.gaffer.data.element.Element) ArrayList(java.util.ArrayList) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) DataGenerator4(uk.gov.gchq.gaffer.example.gettingstarted.generator.DataGenerator4) 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 ElementTransformer (uk.gov.gchq.gaffer.data.element.function.ElementTransformer)1 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)1 ViewElementDefinition (uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition)1 MeanTransform (uk.gov.gchq.gaffer.example.gettingstarted.function.transform.MeanTransform)1 DataGenerator4 (uk.gov.gchq.gaffer.example.gettingstarted.generator.DataGenerator4)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