Search in sources :

Example 61 with Graph

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

the class GetDataFrameOfElementsHandlerTest method checkGetCorrectElementsInDataFrameWithProjection.

@Test
public void checkGetCorrectElementsInDataFrameWithProjection() throws OperationException {
    final Graph graph = getGraph("/schema-DataFrame/dataSchema.json", getElements());
    final SQLContext sqlContext = getSqlContext("checkGetCorrectElementsInDataFrameWithProjection");
    // Get all edges
    final GetDataFrameOfElements dfOperation = new GetDataFrameOfElements.Builder().sqlContext(sqlContext).view(new View.Builder().edge(EDGE_GROUP).build()).build();
    final Dataset<Row> dataFrame = graph.execute(dfOperation, new User());
    // Check get correct rows when ask for src, dst and property2 columns
    Set<Row> results = new HashSet<>(dataFrame.select("src", "dst", "property2").collectAsList());
    final Set<Row> expectedRows = new HashSet<>();
    for (int i = 0; i < NUM_ELEMENTS; i++) {
        MutableList<Object> fields1 = new MutableList<>();
        fields1.appendElem("" + i);
        fields1.appendElem("B");
        fields1.appendElem(3.0F);
        expectedRows.add(Row$.MODULE$.fromSeq(fields1));
        MutableList<Object> fields2 = new MutableList<>();
        fields2.appendElem("" + i);
        fields2.appendElem("C");
        fields2.appendElem(8.0F);
        expectedRows.add(Row$.MODULE$.fromSeq(fields2));
    }
    assertEquals(expectedRows, results);
    // Check get correct rows when ask for just property2 column
    results = new HashSet<>(dataFrame.select("property2").collectAsList());
    expectedRows.clear();
    for (int i = 0; i < NUM_ELEMENTS; i++) {
        MutableList<Object> fields1 = new MutableList<>();
        fields1.appendElem(3.0F);
        expectedRows.add(Row$.MODULE$.fromSeq(fields1));
        MutableList<Object> fields2 = new MutableList<>();
        fields2.appendElem(8.0F);
        expectedRows.add(Row$.MODULE$.fromSeq(fields2));
    }
    assertEquals(expectedRows, results);
    sqlContext.sparkContext().stop();
}
Also used : GetDataFrameOfElements(uk.gov.gchq.gaffer.spark.operation.dataframe.GetDataFrameOfElements) User(uk.gov.gchq.gaffer.user.User) Graph(uk.gov.gchq.gaffer.graph.Graph) MutableList(scala.collection.mutable.MutableList) Row(org.apache.spark.sql.Row) SQLContext(org.apache.spark.sql.SQLContext) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 62 with Graph

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

the class GetJavaRDDOfElementsHandlerTest method checkGetCorrectElementsInJavaRDDForEntitySeed.

@Test
public void checkGetCorrectElementsInJavaRDDForEntitySeed() throws OperationException, IOException {
    final Graph graph1 = new Graph.Builder().addSchema(getClass().getResourceAsStream("/schema/dataSchema.json")).addSchema(getClass().getResourceAsStream("/schema/dataTypes.json")).addSchema(getClass().getResourceAsStream("/schema/storeTypes.json")).storeProperties(getClass().getResourceAsStream("/store.properties")).build();
    final List<Element> elements = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        final Entity entity = new Entity(ENTITY_GROUP);
        entity.setVertex("" + i);
        final Edge edge1 = new Edge(EDGE_GROUP);
        edge1.setSource("" + i);
        edge1.setDestination("B");
        edge1.setDirected(false);
        edge1.putProperty("count", 2);
        final Edge edge2 = new Edge(EDGE_GROUP);
        edge2.setSource("" + i);
        edge2.setDestination("C");
        edge2.setDirected(false);
        edge2.putProperty("count", 4);
        elements.add(edge1);
        elements.add(edge2);
        elements.add(entity);
    }
    final User user = new User();
    graph1.execute(new AddElements(elements), user);
    final SparkConf sparkConf = new SparkConf().setMaster("local").setAppName("testCheckGetCorrectElementsInJavaRDDForEntitySeed").set("spark.serializer", "org.apache.spark.serializer.KryoSerializer").set("spark.kryo.registrator", "uk.gov.gchq.gaffer.spark.serialisation.kryo.Registrator").set("spark.driver.allowMultipleContexts", "true");
    final JavaSparkContext sparkContext = new JavaSparkContext(sparkConf);
    // Create Hadoop configuration and serialise to a string
    final Configuration configuration = new Configuration();
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    configuration.write(new DataOutputStream(baos));
    final String configurationString = new String(baos.toByteArray(), CommonConstants.UTF_8);
    // Check get correct edges for "1"
    GetJavaRDDOfElements<EntitySeed> rddQuery = new GetJavaRDDOfElements.Builder<EntitySeed>().javaSparkContext(sparkContext).seeds(Collections.singleton(new EntitySeed("1"))).build();
    rddQuery.addOption(AbstractGetRDDHandler.HADOOP_CONFIGURATION_KEY, configurationString);
    JavaRDD<Element> rdd = graph1.execute(rddQuery, user);
    if (rdd == null) {
        fail("No RDD returned");
    }
    final Set<Element> results = new HashSet<>(rdd.collect());
    final Set<Element> expectedElements = new HashSet<>();
    final Entity entity1 = new Entity(ENTITY_GROUP);
    entity1.setVertex("1");
    final Edge edge1B = new Edge(EDGE_GROUP);
    edge1B.setSource("1");
    edge1B.setDestination("B");
    edge1B.setDirected(false);
    edge1B.putProperty("count", 2);
    final Edge edge1C = new Edge(EDGE_GROUP);
    edge1C.setSource("1");
    edge1C.setDestination("C");
    edge1C.setDirected(false);
    edge1C.putProperty("count", 4);
    expectedElements.add(entity1);
    expectedElements.add(edge1B);
    expectedElements.add(edge1C);
    assertEquals(expectedElements, results);
    // Check get correct edges for "1" when specify entities only
    rddQuery = new GetJavaRDDOfElements.Builder<EntitySeed>().javaSparkContext(sparkContext).seeds(Collections.singleton(new EntitySeed("1"))).view(new View.Builder().entity(ENTITY_GROUP).build()).build();
    rddQuery.addOption(AbstractGetRDDHandler.HADOOP_CONFIGURATION_KEY, configurationString);
    rdd = graph1.execute(rddQuery, user);
    if (rdd == null) {
        fail("No RDD returned");
    }
    results.clear();
    results.addAll(rdd.collect());
    expectedElements.clear();
    expectedElements.add(entity1);
    assertEquals(expectedElements, results);
    // Check get correct edges for "1" when specify edges only
    rddQuery = new GetJavaRDDOfElements.Builder<EntitySeed>().javaSparkContext(sparkContext).seeds(Collections.singleton(new EntitySeed("1"))).view(new View.Builder().edge(EDGE_GROUP).build()).build();
    rddQuery.addOption(AbstractGetRDDHandler.HADOOP_CONFIGURATION_KEY, configurationString);
    rdd = graph1.execute(rddQuery, user);
    if (rdd == null) {
        fail("No RDD returned");
    }
    results.clear();
    results.addAll(rdd.collect());
    expectedElements.clear();
    expectedElements.add(edge1B);
    expectedElements.add(edge1C);
    assertEquals(expectedElements, results);
    // Check get correct edges for "1" and "5"
    Set<EntitySeed> seeds = new HashSet<>();
    seeds.add(new EntitySeed("1"));
    seeds.add(new EntitySeed("5"));
    rddQuery = new GetJavaRDDOfElements.Builder<EntitySeed>().javaSparkContext(sparkContext).seeds(seeds).build();
    rddQuery.addOption(AbstractGetRDDHandler.HADOOP_CONFIGURATION_KEY, configurationString);
    rdd = graph1.execute(rddQuery, user);
    if (rdd == null) {
        fail("No RDD returned");
    }
    results.clear();
    results.addAll(rdd.collect());
    final Entity entity5 = new Entity(ENTITY_GROUP);
    entity5.setVertex("5");
    final Edge edge5B = new Edge(EDGE_GROUP);
    edge5B.setSource("5");
    edge5B.setDestination("B");
    edge5B.setDirected(false);
    edge5B.putProperty("count", 2);
    final Edge edge5C = new Edge(EDGE_GROUP);
    edge5C.setSource("5");
    edge5C.setDestination("C");
    edge5C.setDirected(false);
    edge5C.putProperty("count", 4);
    expectedElements.clear();
    expectedElements.add(entity1);
    expectedElements.add(edge1B);
    expectedElements.add(edge1C);
    expectedElements.add(entity5);
    expectedElements.add(edge5B);
    expectedElements.add(edge5C);
    assertEquals(expectedElements, results);
    sparkContext.stop();
}
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) Configuration(org.apache.hadoop.conf.Configuration) DataOutputStream(java.io.DataOutputStream) Element(uk.gov.gchq.gaffer.data.element.Element) ArrayList(java.util.ArrayList) GetJavaRDDOfElements(uk.gov.gchq.gaffer.spark.operation.javardd.GetJavaRDDOfElements) JavaSparkContext(org.apache.spark.api.java.JavaSparkContext) HashSet(java.util.HashSet) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) Graph(uk.gov.gchq.gaffer.graph.Graph) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) Edge(uk.gov.gchq.gaffer.data.element.Edge) SparkConf(org.apache.spark.SparkConf) Test(org.junit.Test)

Example 63 with Graph

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

the class GafferResultCacheUtil method createGraph.

public static Graph createGraph(final String cacheStorePropertiesPath, final Long timeToLive) {
    if (null == cacheStorePropertiesPath) {
        throw new IllegalArgumentException("Gaffer result cache Store properties are required");
    }
    final Graph.Builder graphBuilder = new Graph.Builder().storeProperties(cacheStorePropertiesPath).addSchema(createSchema(timeToLive));
    final Graph graph = graphBuilder.build();
    if (!graph.hasTrait(StoreTrait.STORE_VALIDATION)) {
        LOGGER.warn("Gaffer JSON export graph does not have " + StoreTrait.STORE_VALIDATION.name() + " trait so results may not be aged off.");
    }
    return graph;
}
Also used : Graph(uk.gov.gchq.gaffer.graph.Graph)

Example 64 with Graph

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

the class GafferResultCacheUtilTest method shouldCreateGraphWithValidSchemaWithoutAgeOff.

@Test
public void shouldCreateGraphWithValidSchemaWithoutAgeOff() {
    // Given
    final Graph graph = GafferResultCacheUtil.createGraph(StreamUtil.STORE_PROPERTIES, null);
    final Schema schema = graph.getSchema();
    // When
    final boolean isValid = schema.validate();
    // Then
    assertTrue(isValid);
    assertNull(schema.getType("timestamp").getValidator());
    assertTrue(new ElementValidator(schema).validate(validEdge));
    assertTrue(new ElementValidator(schema).validate(oldEdge));
    assertTrue(schema.validate());
}
Also used : Graph(uk.gov.gchq.gaffer.graph.Graph) Schema(uk.gov.gchq.gaffer.store.schema.Schema) ElementValidator(uk.gov.gchq.gaffer.store.ElementValidator) Test(org.junit.Test)

Example 65 with Graph

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

the class JcsJobTrackerIT method setup.

@Before
public void setup() throws Exception {
    final StoreProperties storeProps = StoreProperties.loadStoreProperties(StreamUtil.storeProps(getClass()));
    store = Class.forName(storeProps.getStoreClass()).asSubclass(Store.class).newInstance();
    store.initialise(new Schema(), storeProps);
    graph = new Graph.Builder().store(store).build();
    clearJobTracker();
}
Also used : Graph(uk.gov.gchq.gaffer.graph.Graph) Schema(uk.gov.gchq.gaffer.store.schema.Schema) StoreProperties(uk.gov.gchq.gaffer.store.StoreProperties) Before(org.junit.Before)

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