use of uk.gov.gchq.gaffer.sketches.clearspring.cardinality.predicate.HyperLogLogPlusIsLessThan 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;
}
use of uk.gov.gchq.gaffer.sketches.clearspring.cardinality.predicate.HyperLogLogPlusIsLessThan in project gaffer-doc by gchq.
the class HyperLogLogPlusIsLessThanExample method hyperLogLogPlusIsLessThanOrEqualTo2.
public void hyperLogLogPlusIsLessThanOrEqualTo2() {
// ---------------------------------------------------------
final HyperLogLogPlusIsLessThan function = new HyperLogLogPlusIsLessThan(2, true);
// ---------------------------------------------------------
runExample(function, null, hllp1, hllp2, hllp3);
}
use of uk.gov.gchq.gaffer.sketches.clearspring.cardinality.predicate.HyperLogLogPlusIsLessThan in project gaffer-doc by gchq.
the class Views 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(), "RoadAndRoadUseWithTimesAndCardinalities/data.txt"))).build()).then(new AddElements()).build();
graph.execute(addOpChain, user);
// ---------------------------------------------------------
print("The elements have been added.");
// [view with groups]
// ---------------------------------------------------------
final View viewWithGroups = new View.Builder().edge("RoadUse").edge("RoadHasJunction").entity("Cardinality").build();
// ---------------------------------------------------------
printJsonAndPythonWithClass("VIEW_WITH_GROUPS", viewWithGroups);
// [view with filters]
// ---------------------------------------------------------
final View viewWithFilters = new View.Builder().edge("RoadUse", new ViewElementDefinition.Builder().postAggregationFilter(new ElementFilter.Builder().select("count").execute(new IsMoreThan(2L)).build()).build()).entity("Cardinality", new ViewElementDefinition.Builder().postAggregationFilter(new ElementFilter.Builder().select("count").execute(new IsMoreThan(5L)).select("hllp").execute(new HyperLogLogPlusIsLessThan(10L)).build()).build()).build();
// ---------------------------------------------------------
printJsonAndPythonWithClass("VIEW_WITH_FILTERS", viewWithFilters);
// [view with removed properties]
// ---------------------------------------------------------
final View viewWithRemovedProperties = new View.Builder().edge("RoadUse", new ViewElementDefinition.Builder().properties("count").build()).entity("Cardinality", new ViewElementDefinition.Builder().excludeProperties("hllp", "edgeGroups").build()).build();
// ---------------------------------------------------------
printJsonAndPythonWithClass("VIEW_WITH_REMOVED_PROPERTIES", viewWithRemovedProperties);
// [view with global filter] run query with a global filter to return only elements with a count more than 2
// ---------------------------------------------------------
final View viewWithGlobalFilter = new View.Builder().globalElements(new GlobalViewElementDefinition.Builder().postAggregationFilter(new ElementFilter.Builder().select("count").execute(new IsMoreThan(2L)).build()).build()).build();
// ---------------------------------------------------------
printJsonAndPythonWithClass("VIEW_WITH_GLOBAL_FILTER", viewWithGlobalFilter);
// [view with global and specific filters]
// ---------------------------------------------------------
final View globalAndSpecificView = new View.Builder().globalElements(new GlobalViewElementDefinition.Builder().postAggregationFilter(new ElementFilter.Builder().select("count").execute(new IsMoreThan(0L)).build()).build()).edge("RoadUse", new ViewElementDefinition.Builder().postAggregationFilter(new ElementFilter.Builder().select("count").execute(new IsMoreThan(2L)).build()).build()).entity("Cardinality").build();
// ---------------------------------------------------------
printJsonAndPythonWithClass("VIEW_WITH_GLOBAL_AND_SPECIFIC_FILTERS", globalAndSpecificView);
// [view with global and specific filters expanded]
// ---------------------------------------------------------
final View globalAndSpecificViewExpanded = new View.Builder().edge("RoadUse", new ViewElementDefinition.Builder().postAggregationFilter(new ElementFilter.Builder().select("count").execute(new IsMoreThan(2L)).select("count").execute(new IsMoreThan(0L)).build()).build()).entity("Cardinality", new ViewElementDefinition.Builder().postAggregationFilter(new ElementFilter.Builder().select("count").execute(new IsMoreThan(0L)).build()).build()).build();
// ---------------------------------------------------------
printJsonAndPythonWithClass("VIEW_WITH_GLOBAL_AND_SPECIFIC_FILTERS_EXPANDED", globalAndSpecificViewExpanded);
// [view with global removed properties]
// ---------------------------------------------------------
final View viewWithGlobalRemovedProperties = new View.Builder().globalElements(new GlobalViewElementDefinition.Builder().properties("count").build()).build();
// ---------------------------------------------------------
printJsonAndPythonWithClass("VIEW_WITH_GLOBAL_REMOVED_PROPERTIES", viewWithGlobalRemovedProperties);
// [view with global and specific removed properties]
// ---------------------------------------------------------
final View viewWithGlobalAndSpecificRemovedProperties = new View.Builder().globalElements(new GlobalViewElementDefinition.Builder().properties("count").build()).edge("RoadUse").entity("Cardinality", new ViewElementDefinition.Builder().properties("hllp").build()).build();
// ---------------------------------------------------------
printJsonAndPythonWithClass("VIEW_WITH_GLOBAL_AND_SPECIFIC_REMOVED_PROPERTIES", viewWithGlobalAndSpecificRemovedProperties);
// [view with global and specific removed properties expanded]
// ---------------------------------------------------------
final View viewWithGlobalAndSpecificRemovedPropertiesExpanded = new View.Builder().edge("RoadUse", new ViewElementDefinition.Builder().properties("count").build()).entity("Cardinality", new ViewElementDefinition.Builder().properties("hllp").build()).build();
// ---------------------------------------------------------
printJsonAndPythonWithClass("VIEW_WITH_GLOBAL_AND_SPECIFIC_REMOVED_PROPERTIES_EXPANDED", viewWithGlobalAndSpecificRemovedPropertiesExpanded);
return null;
}
use of uk.gov.gchq.gaffer.sketches.clearspring.cardinality.predicate.HyperLogLogPlusIsLessThan in project gaffer-doc by gchq.
the class HyperLogLogPlusIsLessThanExample method hyperLogLogPlusIsLessThan2.
public void hyperLogLogPlusIsLessThan2() {
// ---------------------------------------------------------
final HyperLogLogPlusIsLessThan function = new HyperLogLogPlusIsLessThan(2);
// ---------------------------------------------------------
runExample(function, null, hllp1, hllp2, hllp3);
}
Aggregations