use of uk.gov.gchq.gaffer.graph.GraphConfig 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;
}
use of uk.gov.gchq.gaffer.graph.GraphConfig in project Gaffer by gchq.
the class FederatedGraphStorage method changeGraphId.
private boolean changeGraphId(final String graphId, final String newGraphId, final Predicate<FederatedAccess> accessPredicate) throws StorageException {
boolean rtn;
final Graph graphToMove = getGraphToMove(graphId, accessPredicate);
if (nonNull(graphToMove)) {
FederatedAccess key = null;
// remove graph to be moved from storage
for (final Entry<FederatedAccess, Set<Graph>> entry : storage.entrySet()) {
final boolean removed = entry.getValue().removeIf(graph -> graph.getGraphId().equals(graphId));
if (removed) {
key = entry.getKey();
break;
}
}
// Update Tables
String storeClass = graphToMove.getStoreProperties().getStoreClass();
if (nonNull(storeClass) && storeClass.startsWith(AccumuloStore.class.getPackage().getName())) {
/*
* This logic is only for Accumulo derived stores Only.
* For updating table names to match graphs names.
*
* uk.gov.gchq.gaffer.accumulostore.[AccumuloStore, SingleUseAccumuloStore,
* SingleUseMockAccumuloStore, MockAccumuloStore, MiniAccumuloStore]
*/
try {
AccumuloProperties tmpAccumuloProps = (AccumuloProperties) graphToMove.getStoreProperties();
Connector connection = TableUtils.getConnector(tmpAccumuloProps.getInstance(), tmpAccumuloProps.getZookeepers(), tmpAccumuloProps.getUser(), tmpAccumuloProps.getPassword());
if (connection.tableOperations().exists(graphId)) {
connection.tableOperations().offline(graphId);
connection.tableOperations().rename(graphId, newGraphId);
connection.tableOperations().online(newGraphId);
}
} catch (final Exception e) {
LOGGER.warn("Error trying to update tables for graphID:{} graphToMove:{}", graphId, graphToMove);
LOGGER.warn("Error trying to update tables.", e);
}
}
final GraphConfig configWithNewGraphId = cloneGraphConfigWithNewGraphId(newGraphId, graphToMove);
// add the graph being renamed.
GraphSerialisable newGraphSerialisable = new GraphSerialisable.Builder().graph(graphToMove).config(configWithNewGraphId).build();
this.put(newGraphSerialisable, key);
// Update cache
if (isCacheEnabled()) {
try {
// Overwrite cache = true because the graphLibrary should have thrown an error before this point.
federatedStoreCache.addGraphToCache(newGraphSerialisable, key, true);
} catch (final CacheOperationException e) {
String s = "Contact Admin for recovery. Error occurred updating graphId. GraphStorage=updated, Cache=outdated graphId.";
LOGGER.error(s + " graphStorage graphId:{} cache graphId:{}", newGraphId, graphId);
throw new StorageException(s, e);
}
federatedStoreCache.deleteGraphFromCache(graphId);
}
rtn = true;
} else {
rtn = false;
}
return rtn;
}
use of uk.gov.gchq.gaffer.graph.GraphConfig in project Gaffer by gchq.
the class OperationControllerIT method shouldCorrectlyStreamExecuteChunked.
@Test
public void shouldCorrectlyStreamExecuteChunked() throws Exception {
// Given
final Schema schema = new Schema.Builder().entity("g1", new SchemaEntityDefinition.Builder().vertex("string").build()).type("string", new TypeDefinition.Builder().clazz(String.class).aggregateFunction(new StringConcat()).build()).build();
Graph graph = new Graph.Builder().config(new GraphConfig("id")).storeProperties(new MapStoreProperties()).addSchema(schema).build();
when(getGraphFactory().getGraph()).thenReturn(graph);
Entity ent1 = new Entity.Builder().group("g1").vertex("v1").build();
Entity ent2 = new Entity.Builder().group("g1").vertex("v2").build();
final ObjectMapper mapper = createDefaultMapper();
graph.execute(new AddElements.Builder().input(ent1).build(), new Context());
graph.execute(new AddElements.Builder().input(ent2).build(), new Context());
// When
final ResponseEntity<String> response = post("/graph/operations/execute/chunked", new GetAllElements.Builder().build(), String.class);
// Then
String expected = mapper.writeValueAsString(ent1) + "\r\n" + mapper.writeValueAsString(ent2) + "\r\n";
assertEquals(expected, response.getBody());
}
use of uk.gov.gchq.gaffer.graph.GraphConfig in project Gaffer by gchq.
the class GraphConfigurationControllerTest method shouldReturnGraphId.
@Test
public void shouldReturnGraphId() {
// Given
when(graphFactory.getGraph()).thenReturn(new Graph.Builder().config(new GraphConfig("id")).addSchema(new Schema()).storeProperties(new MapStoreProperties()).description("test graph").build());
// When
GraphConfigurationController controller = new GraphConfigurationController(graphFactory);
final String graphId = controller.getGraphId();
// Then
assertEquals("id", graphId);
}
use of uk.gov.gchq.gaffer.graph.GraphConfig in project Gaffer by gchq.
the class GraphConfigurationControllerTest method shouldSerialiseAndDeserialiseGetStoreTraits.
@Test
public void shouldSerialiseAndDeserialiseGetStoreTraits() throws SerialisationException {
// Given
Store store = mock(Store.class);
Schema schema = new Schema();
StoreProperties props = new StoreProperties();
Set<StoreTrait> storeTraits = Sets.newHashSet(INGEST_AGGREGATION, PRE_AGGREGATION_FILTERING, POST_AGGREGATION_FILTERING);
when(store.getSchema()).thenReturn(schema);
when(store.getProperties()).thenReturn(props);
when(store.getTraits()).thenReturn(storeTraits);
Graph graph = new Graph.Builder().config(new GraphConfig("id")).addSchema(new Schema()).store(store).build();
when(graphFactory.getGraph()).thenReturn(graph);
GraphConfigurationController controller = new GraphConfigurationController(graphFactory);
// When
byte[] bytes = JSONSerialiser.serialise(controller.getStoreTraits());
final Set<String> traits = JSONSerialiser.deserialise(bytes, Set.class);
// Then
assertEquals(Sets.newHashSet(INGEST_AGGREGATION.name(), PRE_AGGREGATION_FILTERING.name(), POST_AGGREGATION_FILTERING.name()), traits);
}
Aggregations