Search in sources :

Example 46 with User

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

the class Visibilities method run.

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 basicUser = 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 RoadAndRoadUseWithSecurityElementGenerator()).input(IOUtils.readLines(StreamUtil.openStream(getClass(), "RoadAndRoadUseWithSecurity/data.txt"))).build()).then(new AddElements()).build();
    graph.execute(addOpChain, basicUser);
    // ---------------------------------------------------------
    print("\nNow run a simple query to get edges\n");
    // [get simple] get all the edges that contain the vertex "10" or "23"
    // ---------------------------------------------------------
    final GetElements getEdges = new GetElements.Builder().input(new EntitySeed("10"), new EntitySeed("23")).view(new View.Builder().edge("RoadUse").build()).build();
    final CloseableIterable<? extends Element> resultsWithBasicUser = graph.execute(getEdges, basicUser);
    // ---------------------------------------------------------
    for (final Element e : resultsWithBasicUser) {
        print("GET_ELEMENTS_RESULT", e.toString());
    }
    print("We get nothing back");
    print("\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 "10" or "23"
    // ---------------------------------------------------------
    final User publicUser = new User.Builder().userId("publicUser").dataAuth("public").build();
    final GetElements getPublicRelatedEdges = new GetElements.Builder().input(new EntitySeed("10"), new EntitySeed("23")).view(new View.Builder().edge("RoadUse").build()).build();
    final CloseableIterable<? extends Element> publicResults = graph.execute(getPublicRelatedEdges, publicUser);
    // ---------------------------------------------------------
    for (final Element e : publicResults) {
        print("GET_PUBLIC_EDGES_RESULT", e.toString());
    }
    print("\nGet edges with the private visibility. We should get the public edges too.\n");
    // [get private] get all the edges that contain the vertex "10" or "23"
    // ---------------------------------------------------------
    final User privateUser = new User.Builder().userId("privateUser").dataAuth("private").build();
    final GetElements getPrivateRelatedEdges = new GetElements.Builder().input(new EntitySeed("10"), new EntitySeed("23")).view(new View.Builder().edge("RoadUse").build()).build();
    final CloseableIterable<? extends Element> privateResults = graph.execute(getPrivateRelatedEdges, privateUser);
    // ---------------------------------------------------------
    for (final Element e : privateResults) {
        print("GET_PRIVATE_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) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) Graph(uk.gov.gchq.gaffer.graph.Graph) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) RoadAndRoadUseWithSecurityElementGenerator(uk.gov.gchq.gaffer.doc.dev.generator.RoadAndRoadUseWithSecurityElementGenerator)

Example 47 with User

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

the class AddElementsExample method addElements.

public void addElements() throws OperationException {
    print("### " + getMethodNameAsSentence(0) + "\n");
    printSimpleGraphAsAscii();
    final AddElements operation = new AddElements.Builder().input(new Entity.Builder().group("entity").vertex(6).property("count", 1).build(), new Edge.Builder().group("edge").source(5).dest(6).directed(true).property("count", 1).build()).build();
    printJavaJsonPython(operation, "new AddElements.Builder()\n" + "                .input(new Entity.Builder()\n" + "                                .group(\"entity\")\n" + "                                .vertex(6)\n" + "                                .property(\"count\", 1)\n" + "                                .build(),\n" + "                        new Edge.Builder()\n" + "                                .group(\"edge\")\n" + "                                .source(5).dest(6).directed(true)\n" + "                                .property(\"count\", 1)\n" + "                                .build())\n" + "                .build();");
    getGraph().execute(operation, new User("user01"));
    print("Updated graph:");
    print("```");
    print("");
    print("    --> 4 <--");
    print("  /     ^     \\");
    print(" /      |      \\");
    print("1  -->  2  -->  3");
    print("         \\");
    print("           -->  5  -->  6");
    print("```");
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) User(uk.gov.gchq.gaffer.user.User)

Example 48 with User

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

the class Cardinalities method run.

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 RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator()).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 edges
    // ---------------------------------------------------------
    final CloseableIterable<? extends Element> edges = graph.execute(new GetAllElements(), user);
    // ---------------------------------------------------------
    print("\nAll edges:");
    for (final Element edge : edges) {
        print("GET_ALL_EDGES_RESULT", edge.toString());
    }
    // [get all cardinalities] Get all cardinalities
    // ---------------------------------------------------------
    final GetAllElements getAllCardinalities = new GetAllElements.Builder().view(new View.Builder().entity("Cardinality").build()).build();
    // ---------------------------------------------------------
    final CloseableIterable<? extends Element> allCardinalities = graph.execute(getAllCardinalities, user);
    print("\nAll cardinalities");
    for (final Element cardinality : allCardinalities) {
        final String edgeGroup = cardinality.getProperty("edgeGroup").toString();
        print("ALL_CARDINALITIES_RESULT", "Vertex " + ((Entity) cardinality).getVertex() + " " + edgeGroup + ": " + ((HyperLogLogPlus) cardinality.getProperty("hllp")).cardinality());
    }
    // [get all summarised cardinalities] Get all summarised cardinalities over all edges
    // ---------------------------------------------------------
    final GetAllElements getAllSummarisedCardinalities = new GetAllElements.Builder().view(new View.Builder().entity("Cardinality", new ViewElementDefinition.Builder().groupBy().build()).build()).build();
    // ---------------------------------------------------------
    final CloseableIterable<? extends Element> allSummarisedCardinalities = graph.execute(getAllSummarisedCardinalities, user);
    print("\nAll summarised cardinalities");
    for (final Element cardinality : allSummarisedCardinalities) {
        final String edgeGroup = cardinality.getProperty("edgeGroup").toString();
        print("ALL_SUMMARISED_CARDINALITIES_RESULT", "Vertex " + ((Entity) cardinality).getVertex() + " " + edgeGroup + ": " + ((HyperLogLogPlus) cardinality.getProperty("hllp")).cardinality());
    }
    // [get roaduse edge cardinality 10] Get the cardinality value at vertex 10 for RoadUse edges
    // ---------------------------------------------------------
    final GetElements getCardinalities = new GetElements.Builder().input(new EntitySeed("10")).view(new View.Builder().entity("Cardinality", new ViewElementDefinition.Builder().preAggregationFilter(new ElementFilter.Builder().select("edgeGroup").execute(new IsEqual(CollectionUtil.treeSet("RoadUse"))).build()).build()).build()).build();
    // ---------------------------------------------------------
    final Element roadUse10Cardinality;
    try (final CloseableIterable<? extends Element> elements = graph.execute(getCardinalities, user)) {
        roadUse10Cardinality = elements.iterator().next();
    }
    print("\nRoadUse edge cardinality at vertex 10:");
    final String edgeGroup = (roadUse10Cardinality.getProperty("edgeGroup")).toString();
    print("CARDINALITY_OF_10_RESULT", "Vertex " + ((Entity) roadUse10Cardinality).getVertex() + " " + edgeGroup + ": " + ((HyperLogLogPlus) roadUse10Cardinality.getProperty("hllp")).cardinality());
    // [get 2 hops with a cardinality filter] 2 hops with a cardinality filter
    // ---------------------------------------------------------
    final OperationChain<CloseableIterable<? extends Element>> twoHopsWithCardinalityFilter = new OperationChain.Builder().first(new GetElements.Builder().input(new EntitySeed("M5")).view(new View.Builder().edge("RoadHasJunction").build()).build()).then(new GetElements.Builder().view(new View.Builder().entity("Cardinality", new ViewElementDefinition.Builder().preAggregationFilter(new ElementFilter.Builder().select("edgeGroup").execute(new IsEqual(CollectionUtil.treeSet("RoadUse"))).build()).groupBy().postAggregationFilter(new ElementFilter.Builder().select("hllp").execute(new HyperLogLogPlusIsLessThan(5)).build()).build()).build()).build()).then(new GetElements.Builder().view(new View.Builder().edge("RoadUse").build()).build()).build();
    // ---------------------------------------------------------
    final CloseableIterable<? extends Element> twoHopsWithCardinalityFilterResult = graph.execute(twoHopsWithCardinalityFilter, user);
    print("\n2 hops with cardinality filter result:");
    for (final Element element : twoHopsWithCardinalityFilterResult) {
        print("2_HOP_RESULT", element.toString());
    }
    return allSummarisedCardinalities;
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) Entity(uk.gov.gchq.gaffer.data.element.Entity) User(uk.gov.gchq.gaffer.user.User) 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) RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator(uk.gov.gchq.gaffer.doc.user.generator.RoadAndRoadUseWithTimesAndCardinalitiesElementGenerator) GetAllElements(uk.gov.gchq.gaffer.operation.impl.get.GetAllElements) HyperLogLogPlusIsLessThan(uk.gov.gchq.gaffer.sketches.clearspring.cardinality.predicate.HyperLogLogPlusIsLessThan) CloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) IsEqual(uk.gov.gchq.koryphe.impl.predicate.IsEqual) Graph(uk.gov.gchq.gaffer.graph.Graph) HyperLogLogPlus(com.clearspring.analytics.stream.cardinality.HyperLogLogPlus) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed)

Example 49 with User

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

the class FullExample method run.

@Override
public Iterable<? extends String> 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");
    // ---------------------------------------------------------
    try (final CSVParser parser = CSVParser.parse(getClass().getResource("/FullExample/data.txt"), Charset.defaultCharset(), CSVFormat.DEFAULT.withFirstRecordAsHeader())) {
        final OperationChain<Void> addOpChain = new OperationChain.Builder().first(new GenerateElements.Builder<CSVRecord>().generator(new RoadTrafficCsvElementGenerator()).input(parser).build()).then(new AddElements()).build();
        graph.execute(addOpChain, user);
    }
    // ---------------------------------------------------------
    print("The elements have been added.");
    // [get] Get all road junctions in the South West that were heavily used by buses in the year 2000.
    // ---------------------------------------------------------
    final OperationChain<Iterable<? extends String>> opChain = new OperationChain.Builder().first(new GetAdjacentIds.Builder().input(new EntitySeed("South West")).view(new View.Builder().edge("RegionContainsLocation").build()).build()).then(new GetAdjacentIds.Builder().view(new View.Builder().edge("LocationContainsRoad").build()).build()).then(new ToSet<>()).then(new GetAdjacentIds.Builder().view(new View.Builder().edge("RoadHasJunction").build()).build()).then(new GetElements.Builder().view(new View.Builder().globalElements(new GlobalViewElementDefinition.Builder().groupBy().build()).entity("JunctionUse", new ViewElementDefinition.Builder().preAggregationFilter(new ElementFilter.Builder().select("startDate", "endDate").execute(new InDateRangeDual.Builder().start("2000/01/01").end("2001/01/01").build()).build()).postAggregationFilter(new ElementFilter.Builder().select("countByVehicleType").execute(new PredicateMap<>("BUS", new IsMoreThan(1000L))).build()).transientProperty("busCount", Long.class).transformer(new ElementTransformer.Builder().select("countByVehicleType").execute(new FreqMapExtractor("BUS")).project("busCount").build()).build()).build()).inOutType(SeededGraphFilters.IncludeIncomingOutgoingType.OUTGOING).build()).then(new Sort.Builder().comparators(new ElementPropertyComparator.Builder().groups("JunctionUse").property("busCount").reverse(true).build()).resultLimit(2).deduplicate(true).build()).then(new ToCsv.Builder().generator(new CsvGenerator.Builder().vertex("Junction").property("busCount", "Bus Count").build()).build()).build();
    // ---------------------------------------------------------
    printJsonAndPython("GET", opChain);
    final Iterable<? extends String> results = graph.execute(opChain, user);
    print("\nAll road junctions in the South West that were heavily used by buses in year 2000.");
    for (final String result : results) {
        print("RESULT", result);
    }
    return results;
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) User(uk.gov.gchq.gaffer.user.User) GetAdjacentIds(uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) ToSet(uk.gov.gchq.gaffer.operation.impl.output.ToSet) Sort(uk.gov.gchq.gaffer.operation.impl.compare.Sort) RoadTrafficCsvElementGenerator(uk.gov.gchq.gaffer.traffic.generator.RoadTrafficCsvElementGenerator) ElementTransformer(uk.gov.gchq.gaffer.data.element.function.ElementTransformer) FreqMapExtractor(uk.gov.gchq.gaffer.types.function.FreqMapExtractor) CsvGenerator(uk.gov.gchq.gaffer.data.generator.CsvGenerator) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) ElementPropertyComparator(uk.gov.gchq.gaffer.data.element.comparison.ElementPropertyComparator) InDateRangeDual(uk.gov.gchq.koryphe.impl.predicate.range.InDateRangeDual) Graph(uk.gov.gchq.gaffer.graph.Graph) CSVParser(org.apache.commons.csv.CSVParser) 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.koryphe.impl.predicate.IsMoreThan)

Example 50 with User

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

the class TheBasics 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 RoadUseElementGenerator dataGenerator = new RoadUseElementGenerator();
    for (final String line : IOUtils.readLines(StreamUtil.openStream(getClass(), dataPath))) {
        elements.add(dataGenerator._apply(line));
    }
    // ---------------------------------------------------------
    print("Elements generated from the data file.");
    for (final Element element : elements) {
        print("GENERATED_EDGES", element.toString());
    }
    print("");
    Graph graph;
    // [graph from files] Create a graph using config, schema and store properties files
    // ---------------------------------------------------------
    graph = new Graph.Builder().config(StreamUtil.openStream(getClass(), graphConfigPath)).addSchemas(StreamUtil.openStreams(getClass(), schemaPath)).storeProperties(StreamUtil.openStream(getClass(), storePropertiesPath)).build();
    // ---------------------------------------------------------
    // [graph] Create a graph using config, schema and store properties from java
    // ---------------------------------------------------------
    final GraphConfig config = new GraphConfig.Builder().graphId(getClass().getSimpleName()).build();
    final Schema schema = new Schema.Builder().edge("RoadUse", new SchemaEdgeDefinition.Builder().description("A directed edge representing vehicles moving from junction A to junction B.").source("junction").destination("junction").directed("true").property("count", "count.long").build()).type("junction", new TypeDefinition.Builder().description("A road junction represented by a String.").clazz(String.class).build()).type("count.long", new TypeDefinition.Builder().description("A long count that must be greater than or equal to 0.").clazz(Long.class).validateFunctions(new IsMoreThan(0L, true)).aggregateFunction(new Sum()).build()).type("true", new TypeDefinition.Builder().description("A simple boolean that must always be true.").clazz(Boolean.class).validateFunctions(new IsTrue()).build()).build();
    final AccumuloProperties properties = new AccumuloProperties();
    properties.setStoreClass(SingleUseMockAccumuloStore.class);
    properties.setInstance("instance1");
    properties.setZookeepers("zookeeper1");
    properties.setUser("user01");
    properties.setPassword("password");
    graph = new Graph.Builder().config(config).addSchema(schema).storeProperties(properties).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] Get all the edges that contain the vertex "10"
    // ---------------------------------------------------------
    final GetElements query = new GetElements.Builder().input(new EntitySeed("10")).view(new View.Builder().edge("RoadUse").build()).build();
    final CloseableIterable<? extends Element> results = graph.execute(query, user);
    // ---------------------------------------------------------
    print("\nAll edges containing the vertex 10. The counts have been aggregated.");
    for (final Element e : results) {
        print("GET_ELEMENTS_RESULT", e.toString());
    }
    return results;
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) User(uk.gov.gchq.gaffer.user.User) AccumuloProperties(uk.gov.gchq.gaffer.accumulostore.AccumuloProperties) Element(uk.gov.gchq.gaffer.data.element.Element) Schema(uk.gov.gchq.gaffer.store.schema.Schema) ArrayList(java.util.ArrayList) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) TypeDefinition(uk.gov.gchq.gaffer.store.schema.TypeDefinition) GraphConfig(uk.gov.gchq.gaffer.graph.GraphConfig) Sum(uk.gov.gchq.koryphe.impl.binaryoperator.Sum) Graph(uk.gov.gchq.gaffer.graph.Graph) IsTrue(uk.gov.gchq.koryphe.impl.predicate.IsTrue) RoadUseElementGenerator(uk.gov.gchq.gaffer.doc.user.generator.RoadUseElementGenerator) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) SchemaEdgeDefinition(uk.gov.gchq.gaffer.store.schema.SchemaEdgeDefinition) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan)

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