Search in sources :

Example 26 with Graph

use of uk.gov.gchq.gaffer.graph.Graph 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)

Example 27 with Graph

use of uk.gov.gchq.gaffer.graph.Graph 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)

Example 28 with Graph

use of uk.gov.gchq.gaffer.graph.Graph in project Gaffer by gchq.

the class LoadAndQuery5 method run.

public CloseableIterable<Edge> run() throws OperationException {
    // [user] Create a user
    // ---------------------------------------------------------
    final User basicUser = new User("basicUser");
    // ---------------------------------------------------------
    // [generate] create some edges from the data file using our data generator class
    // ---------------------------------------------------------
    final List<Element> elements = new ArrayList<>();
    final DataGenerator5 dataGenerator = new DataGenerator5();
    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, basicUser);
    // ---------------------------------------------------------
    log("The elements have been added.\n");
    log("\nNow run a simple query to get edges\n");
    // [get simple] get all the edges that contain the vertex "1"
    // ---------------------------------------------------------
    final GetEdges<EntitySeed> getEdges = new GetEdges.Builder<EntitySeed>().addSeed(new EntitySeed("1")).build();
    final CloseableIterable<Edge> resultsWithBasicUser = graph.execute(getEdges, basicUser);
    // ---------------------------------------------------------
    for (final Element e : resultsWithBasicUser) {
        log("GET_RELATED_EDGES_RESULT", e.toString());
    }
    log("We get nothing back");
    log("\nGet edges with the public visibility. We shouldn't see any of the private ones.\n");
    // [get public] get all the edges that contain the vertex "1"
    // ---------------------------------------------------------
    final User publicUser = new User.Builder().userId("publicUser").dataAuth("public").build();
    final GetEdges<EntitySeed> getPublicRelatedEdges = new GetEdges.Builder<EntitySeed>().addSeed(new EntitySeed("1")).build();
    final CloseableIterable<Edge> publicResults = graph.execute(getPublicRelatedEdges, publicUser);
    // ---------------------------------------------------------
    for (final Element e : publicResults) {
        log("GET_PUBLIC_RELATED_EDGES_RESULT", e.toString());
    }
    log("\nGet edges with the private visibility. We should get the public edges too.\n");
    // [get private] get all the edges that contain the vertex "1"
    // ---------------------------------------------------------
    final User privateUser = new User.Builder().userId("privateUser").dataAuth("private").build();
    final GetEdges<EntitySeed> getPrivateRelatedEdges = new GetEdges.Builder<EntitySeed>().addSeed(new EntitySeed("1")).build();
    final CloseableIterable<Edge> privateResults = graph.execute(getPrivateRelatedEdges, privateUser);
    // ---------------------------------------------------------
    for (final Element e : privateResults) {
        log("GET_PRIVATE_RELATED_EDGES_RESULT", e.toString());
    }
    return publicResults;
}
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) DataGenerator5(uk.gov.gchq.gaffer.example.gettingstarted.generator.DataGenerator5) Graph(uk.gov.gchq.gaffer.graph.Graph) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) Edge(uk.gov.gchq.gaffer.data.element.Edge)

Example 29 with Graph

use of uk.gov.gchq.gaffer.graph.Graph in project Gaffer by gchq.

the class LoadAndQuery6 method run.

public CloseableIterable<String> run() throws OperationException {
    // [user] Create a user
    // ---------------------------------------------------------
    final User user = new User("user01");
    // ---------------------------------------------------------
    // [graph] create a graph using our schema and store properties
    // ---------------------------------------------------------
    final Graph graph = new Graph.Builder().addSchemas(getSchemas()).storeProperties(getStoreProperties()).build();
    // ---------------------------------------------------------
    // [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 DataGenerator6 dataGenerator = new DataGenerator6();
    final List<String> data = DataUtils.loadData(getData());
    final OperationChain addOpChain = new OperationChain.Builder().first(new GenerateElements.Builder<String>().generator(dataGenerator).objects(data).build()).then(new AddElements()).build();
    graph.execute(addOpChain, user);
    // ---------------------------------------------------------
    // [get] Create and execute an operation chain consisting of 3 operations:
    //GetAdjacentEntitySeeds - starting at vertex 1 get all adjacent vertices (vertices at other end of outbound edges)
    //GetRelatedEdges - get outbound edges
    //GenerateObjects - convert the edges back into comma separated strings
    // ---------------------------------------------------------
    final OperationChain<CloseableIterable<String>> opChain = new OperationChain.Builder().first(new GetAdjacentEntitySeeds.Builder().addSeed(new EntitySeed("1")).inOutType(IncludeIncomingOutgoingType.OUTGOING).build()).then(new GetEdges.Builder<EntitySeed>().inOutType(IncludeIncomingOutgoingType.OUTGOING).build()).then(new GenerateObjects.Builder<Edge, String>().generator(dataGenerator).build()).build();
    final CloseableIterable<String> results = graph.execute(opChain, user);
    // ---------------------------------------------------------
    log("\nFiltered edges converted back into comma separated strings. The counts have been aggregated\n");
    for (final String result : results) {
        log("RESULT", result);
    }
    return results;
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) User(uk.gov.gchq.gaffer.user.User) GetAdjacentEntitySeeds(uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentEntitySeeds) CloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable) DataGenerator6(uk.gov.gchq.gaffer.example.gettingstarted.generator.DataGenerator6) Graph(uk.gov.gchq.gaffer.graph.Graph) GetEdges(uk.gov.gchq.gaffer.operation.impl.get.GetEdges) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed)

Example 30 with Graph

use of uk.gov.gchq.gaffer.graph.Graph in project Gaffer by gchq.

the class LoadAndQuery7 method run.

public Iterable<Edge> run() throws OperationException {
    // [user] Create a user
    // ---------------------------------------------------------
    final User user = new User("user01");
    // ---------------------------------------------------------
    // [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 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 addOpChain = new OperationChain.Builder().first(new GenerateElements.Builder<String>().generator(new DataGenerator7()).objects(DataUtils.loadData(getData())).build()).then(new AddElements()).build();
    graph.execute(addOpChain, user);
    // ---------------------------------------------------------
    // Create some starting seeds for the sub graph.
    final Iterable<EntitySeed> seeds = Lists.newArrayList(new EntitySeed("1"));
    // Create a view to return only edges that have a count more than 1
    // Note we could have used a different view for each hop in order to
    // specify the edges we wish to hop down or to attempt to prevent caching
    // duplicate edges.
    final View view = new View.Builder().edge("data", new ViewElementDefinition.Builder().preAggregationFilter(new ElementFilter.Builder().select("count").execute(new IsMoreThan(1)).build()).build()).build();
    // [extractor] Create a generator that will extract entity seeds
    // This generator will extract just the destination vertices from edges
    // and skip any entities.
    // ---------------------------------------------------------
    final EntitySeedExtractor destVerticesExtractor = new EntitySeedExtractor(new IsEdgeValidator(), new AlwaysValid<>(), true, IdentifierType.DESTINATION);
    // ---------------------------------------------------------
    // [get] Create a sub graph
    // Start getting related edges with the given seeds.
    // Then update the export with the results
    // Between each hop we need to extract the destination vertices of the
    // previous edges.
    // Finally finish off by returning all the edges in the export.
    // ---------------------------------------------------------
    final OperationChain opChain = new OperationChain.Builder().first(new GetEdges.Builder<EntitySeed>().seeds(seeds).inOutType(IncludeIncomingOutgoingType.OUTGOING).view(view).build()).then(new ExportToSet()).then(new GenerateObjects<Edge, EntitySeed>(destVerticesExtractor)).then(new GetEdges.Builder<EntitySeed>().inOutType(IncludeIncomingOutgoingType.OUTGOING).view(view).build()).then(new ExportToSet()).then(new GetSetExport()).build();
    final Iterable<Edge> subGraph = (Iterable<Edge>) graph.execute(opChain, user);
    // ---------------------------------------------------------
    log("\nSub graph:");
    for (final Edge edge : subGraph) {
        log("SUB_GRAPH", edge.toString());
    }
    return subGraph;
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) User(uk.gov.gchq.gaffer.user.User) ExportToSet(uk.gov.gchq.gaffer.operation.impl.export.set.ExportToSet) GetSetExport(uk.gov.gchq.gaffer.operation.impl.export.set.GetSetExport) IsEdgeValidator(uk.gov.gchq.gaffer.data.IsEdgeValidator) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) DataGenerator7(uk.gov.gchq.gaffer.example.gettingstarted.generator.DataGenerator7) Graph(uk.gov.gchq.gaffer.graph.Graph) EntitySeedExtractor(uk.gov.gchq.gaffer.operation.data.generator.EntitySeedExtractor) GetEdges(uk.gov.gchq.gaffer.operation.impl.get.GetEdges) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) 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

Graph (uk.gov.gchq.gaffer.graph.Graph)72 User (uk.gov.gchq.gaffer.user.User)52 Test (org.junit.Test)45 AddElements (uk.gov.gchq.gaffer.operation.impl.add.AddElements)36 Edge (uk.gov.gchq.gaffer.data.element.Edge)29 Entity (uk.gov.gchq.gaffer.data.element.Entity)22 EntitySeed (uk.gov.gchq.gaffer.operation.data.EntitySeed)22 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)20 HashSet (java.util.HashSet)18 Element (uk.gov.gchq.gaffer.data.element.Element)16 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)16 ArrayList (java.util.ArrayList)15 SparkConf (org.apache.spark.SparkConf)13 JSONSerialiser (uk.gov.gchq.gaffer.jsonserialisation.JSONSerialiser)11 DataOutputStream (java.io.DataOutputStream)10 ByteArrayOutputStream (org.apache.commons.io.output.ByteArrayOutputStream)10 Configuration (org.apache.hadoop.conf.Configuration)10 CloseableIterable (uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable)10 SQLContext (org.apache.spark.sql.SQLContext)9 ViewElementDefinition (uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition)9