Search in sources :

Example 51 with User

use of uk.gov.gchq.gaffer.user.User in project gaffer-doc by gchq.

the class GetJobResultsExample method runExamples.

@Override
public void runExamples() {
    try {
        final OperationChain<JobDetail> opChain = new OperationChain.Builder().first(new GetAllElements()).then(new ExportToGafferResultCache<>()).then(new DiscardOutput()).then(new GetJobDetails()).build();
        final JobDetail jobDetails = getGraph().execute(opChain, new User("user01"));
        jobId = jobDetails.getJobId();
    } catch (final OperationException e) {
        throw new RuntimeException(e);
    }
    getJobResults();
}
Also used : JobDetail(uk.gov.gchq.gaffer.jobtracker.JobDetail) GetJobDetails(uk.gov.gchq.gaffer.operation.impl.job.GetJobDetails) User(uk.gov.gchq.gaffer.user.User) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) GetAllElements(uk.gov.gchq.gaffer.operation.impl.get.GetAllElements) ExportToGafferResultCache(uk.gov.gchq.gaffer.operation.impl.export.resultcache.ExportToGafferResultCache) DiscardOutput(uk.gov.gchq.gaffer.operation.impl.DiscardOutput) OperationException(uk.gov.gchq.gaffer.operation.OperationException)

Example 52 with User

use of uk.gov.gchq.gaffer.user.User 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;
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) User(uk.gov.gchq.gaffer.user.User) RoadAndRoadUseWithTimesElementGenerator(uk.gov.gchq.gaffer.doc.user.generator.RoadAndRoadUseWithTimesElementGenerator) Element(uk.gov.gchq.gaffer.data.element.Element) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) Graph(uk.gov.gchq.gaffer.graph.Graph) IsLessThan(uk.gov.gchq.koryphe.impl.predicate.IsLessThan) Min(uk.gov.gchq.koryphe.impl.binaryoperator.Min) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) GetAllElements(uk.gov.gchq.gaffer.operation.impl.get.GetAllElements) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan) ElementAggregator(uk.gov.gchq.gaffer.data.element.function.ElementAggregator)

Example 53 with User

use of uk.gov.gchq.gaffer.user.User in project gaffer-doc by gchq.

the class MultipleEdges 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 RoadAndRoadUseElementGenerator dataGenerator = new RoadAndRoadUseElementGenerator();
    for (final String line : IOUtils.readLines(StreamUtil.openStream(getClass(), dataPath))) {
        Iterables.addAll(elements, dataGenerator._apply(line));
    }
    // ---------------------------------------------------------
    print("Elements generated from the data file.");
    for (final Element element : elements) {
        print("GENERATED_EDGES", element.toString());
    }
    print("");
    // [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] 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 simple] Get all the edges related to vertex 10
    // ---------------------------------------------------------
    final GetElements getElements = new GetElements.Builder().input(new EntitySeed("10")).build();
    final CloseableIterable<? extends Element> results = graph.execute(getElements, user);
    // ---------------------------------------------------------
    print("\nAll elements containing vertex 10");
    print("\nNotice that the edges are aggregated within their groups");
    for (final Element e : results) {
        print("GET_ELEMENTS_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("RoadHasJunction").build();
    final GetElements getRoadHasJunctionEdges = new GetElements.Builder().input(new EntitySeed("10")).view(view).build();
    final CloseableIterable<? extends Element> filteredResults = graph.execute(getRoadHasJunctionEdges, user);
    // ---------------------------------------------------------
    print("\nAll RoadHasJunction edges containing vertex 10\n");
    for (final Element e : filteredResults) {
        print("GET_ROAD_HAS_JUNCTION_EDGES_RESULT", e.toString());
    }
    return filteredResults;
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) User(uk.gov.gchq.gaffer.user.User) RoadAndRoadUseElementGenerator(uk.gov.gchq.gaffer.doc.user.generator.RoadAndRoadUseElementGenerator) Element(uk.gov.gchq.gaffer.data.element.Element) ArrayList(java.util.ArrayList) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) Graph(uk.gov.gchq.gaffer.graph.Graph) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed)

Example 54 with User

use of uk.gov.gchq.gaffer.user.User in project gaffer-doc by gchq.

the class Subgraphs method run.

@Override
public Iterable<? 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(), "RoadAndRoadUseWithTimesAndCardinalities/data.txt"))).build()).then(new AddElements()).build();
    graph.execute(addOpChain, user);
    // ---------------------------------------------------------
    print("The elements have been added.");
    // [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 and convert them to EntitySeeds.
    // Finally finish off by returning all the edges in the export.
    // ---------------------------------------------------------
    final OperationChain<Iterable<?>> opChain = new OperationChain.Builder().first(new GetElements.Builder().input(new EntitySeed("M5")).inOutType(IncludeIncomingOutgoingType.OUTGOING).view(new View.Builder().edge("RoadHasJunction").build()).build()).then(new ExportToSet<>()).then(new ToVertices.Builder().edgeVertices(ToVertices.EdgeVertices.DESTINATION).build()).then(new ToEntitySeeds()).then(new GetElements.Builder().inOutType(IncludeIncomingOutgoingType.OUTGOING).view(new View.Builder().edge("RoadUse", new ViewElementDefinition.Builder().preAggregationFilter(new ElementFilter.Builder().select("count").execute(new IsMoreThan(1L)).build()).build()).build()).build()).then(new ExportToSet<>()).then(new DiscardOutput()).then(new GetSetExport()).build();
    // ---------------------------------------------------------
    final Iterable<? extends Element> subGraph = (Iterable<? extends Element>) graph.execute(opChain, user);
    print("\nSub graph:");
    for (final Element edge : subGraph) {
        print("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) ToEntitySeeds(uk.gov.gchq.gaffer.operation.impl.output.ToEntitySeeds) Element(uk.gov.gchq.gaffer.data.element.Element) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) GetSetExport(uk.gov.gchq.gaffer.operation.impl.export.set.GetSetExport) RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator(uk.gov.gchq.gaffer.doc.user.generator.RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator) ToVertices(uk.gov.gchq.gaffer.operation.impl.output.ToVertices) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) Graph(uk.gov.gchq.gaffer.graph.Graph) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) DiscardOutput(uk.gov.gchq.gaffer.operation.impl.DiscardOutput) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan)

Example 55 with User

use of uk.gov.gchq.gaffer.user.User in project gaffer-doc by gchq.

the class Transforms 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 RoadAndRoadUseElementGenerator dataGenerator = new RoadAndRoadUseElementGenerator();
    for (final String line : IOUtils.readLines(StreamUtil.openStream(getClass(), dataPath))) {
        Iterables.addAll(elements, dataGenerator._apply(line));
    }
    // ---------------------------------------------------------
    print("Elements generated from the data file.");
    for (final Element element : elements) {
        print("GENERATED_EDGES", element.toString());
    }
    print("");
    // [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] 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 simple] get all the edges that contain the vertex "1"
    // ---------------------------------------------------------
    final GetElements getEdges = new GetElements.Builder().input(new EntitySeed("10")).build();
    final CloseableIterable<? extends Element> results = graph.execute(getEdges, user);
    // ---------------------------------------------------------
    print("\nAll edges containing the vertex 10. The counts and 'things' have been aggregated\n");
    for (final Element e : results) {
        print("GET_ELEMENTS_RESULT", e.toString());
    }
    // [transform] Create a description transient property using an element transformer
    // ---------------------------------------------------------
    final ElementTransformer descriptionTransformer = new ElementTransformer.Builder().select("SOURCE", "DESTINATION", "count").execute(new DescriptionTransform()).project("description").build();
    // ---------------------------------------------------------
    printJson("TRANSFORM", descriptionTransformer);
    // [get] Add the element transformer to the view and run the query
    // ---------------------------------------------------------
    final GetElements getEdgesWithDescription = new GetElements.Builder().input(new EntitySeed("10")).view(new View.Builder().edge("RoadUse", new ViewElementDefinition.Builder().transientProperty("description", String.class).transformer(descriptionTransformer).build()).build()).build();
    final CloseableIterable<? extends Element> resultsWithDescription = graph.execute(getEdgesWithDescription, user);
    // ---------------------------------------------------------
    printJsonAndPython("GET", getEdgesWithDescription);
    print("\nWe can add a new property to the edges that is calculated from the aggregated values of other properties\n");
    for (final Element e : resultsWithDescription) {
        print("GET_ELEMENTS_WITH_DESCRIPTION_RESULT", e.toString());
    }
    // [get with no count]
    // ---------------------------------------------------------
    final GetElements getEdgesWithDescriptionAndNoCount = new GetElements.Builder().input(new EntitySeed("10")).view(new View.Builder().edge("RoadUse", new ViewElementDefinition.Builder().transientProperty("description", String.class).transformer(descriptionTransformer).excludeProperties("count").build()).build()).build();
    final CloseableIterable<? extends Element> resultsWithDescriptionAndNoCount = graph.execute(getEdgesWithDescriptionAndNoCount, user);
    // ---------------------------------------------------------
    printJsonAndPython("GET_WITH_NO_COUNT", getEdgesWithDescriptionAndNoCount);
    print("\nAnd the result without the count property:\n");
    for (final Element e : resultsWithDescriptionAndNoCount) {
        print("GET_ELEMENTS_WITH_DESCRIPTION_AND_NO_COUNT_RESULT", e.toString());
    }
    return resultsWithDescriptionAndNoCount;
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) User(uk.gov.gchq.gaffer.user.User) RoadAndRoadUseElementGenerator(uk.gov.gchq.gaffer.doc.user.generator.RoadAndRoadUseElementGenerator) ElementTransformer(uk.gov.gchq.gaffer.data.element.function.ElementTransformer) Element(uk.gov.gchq.gaffer.data.element.Element) ArrayList(java.util.ArrayList) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) Graph(uk.gov.gchq.gaffer.graph.Graph) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) DescriptionTransform(uk.gov.gchq.gaffer.traffic.transform.DescriptionTransform)

Aggregations

User (uk.gov.gchq.gaffer.user.User)378 Test (org.junit.jupiter.api.Test)188 Graph (uk.gov.gchq.gaffer.graph.Graph)155 Element (uk.gov.gchq.gaffer.data.element.Element)143 AddElements (uk.gov.gchq.gaffer.operation.impl.add.AddElements)128 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)110 HashSet (java.util.HashSet)109 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)104 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)103 EntitySeed (uk.gov.gchq.gaffer.operation.data.EntitySeed)98 Edge (uk.gov.gchq.gaffer.data.element.Edge)85 Context (uk.gov.gchq.gaffer.store.Context)85 Entity (uk.gov.gchq.gaffer.data.element.Entity)77 Test (org.junit.Test)61 ArrayList (java.util.ArrayList)57 OperationException (uk.gov.gchq.gaffer.operation.OperationException)52 GetAllElements (uk.gov.gchq.gaffer.operation.impl.get.GetAllElements)49 CloseableIterable (uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable)48 ViewElementDefinition (uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition)45 ElementFilter (uk.gov.gchq.gaffer.data.element.function.ElementFilter)43