Search in sources :

Example 1 with RyaSubGraph

use of org.apache.rya.api.domain.RyaSubGraph in project incubator-rya by apache.

the class RyaSubGraphSerializer method read.

/**
 * Reads RyaSubGraph from {@link Input} stream
 * @param input - stream that subgraph is read from
 * @return subgraph read from input stream
 */
public static RyaSubGraph read(Input input) {
    RyaSubGraph bundle = new RyaSubGraph(input.readString());
    int numStatements = input.readInt();
    for (int i = 0; i < numStatements; i++) {
        bundle.addStatement(RyaStatementSerializer.read(input));
    }
    return bundle;
}
Also used : RyaSubGraph(org.apache.rya.api.domain.RyaSubGraph)

Example 2 with RyaSubGraph

use of org.apache.rya.api.domain.RyaSubGraph in project incubator-rya by apache.

the class RyaSubGraphSerializer method read.

/**
 * Uses Kryo to read RyaSubGraph from {@link Input} stream
 * @param kryo - used to read subgraph from input stream
 * @param input - stream that subgraph is read from
 * @param type - class of object to be read from input stream (RyaSubgraph)
 * @return subgraph read from input stream
 */
@Override
public RyaSubGraph read(Kryo kryo, Input input, Class<RyaSubGraph> type) {
    RyaSubGraph bundle = new RyaSubGraph(input.readString());
    int numStatements = input.readInt();
    for (int i = 0; i < numStatements; i++) {
        bundle.addStatement(RyaStatementSerializer.readFromKryo(kryo, input, RyaStatement.class));
    }
    return bundle;
}
Also used : RyaSubGraph(org.apache.rya.api.domain.RyaSubGraph) RyaStatement(org.apache.rya.api.domain.RyaStatement)

Example 3 with RyaSubGraph

use of org.apache.rya.api.domain.RyaSubGraph in project incubator-rya by apache.

the class KafkaRyaSubGraphExporterFactory method build.

/**
 * Builds a {@link KafkaRyaSubGraphExporter}.
 * @param context - {@link Context} object used to pass configuration parameters
 * @return an Optional consisting of an IncrementalSubGraphExproter if it can be constructed
 * @throws IncrementalExporterFactoryException
 * @throws ConfigurationException
 */
@Override
public Optional<IncrementalResultExporter> build(final Context context) throws IncrementalExporterFactoryException, ConfigurationException {
    final KafkaSubGraphExporterParameters exportParams = new KafkaSubGraphExporterParameters(context.getObserverConfiguration().toMap());
    log.info("Exporter is enabled: {}", exportParams.getUseKafkaSubgraphExporter());
    if (exportParams.getUseKafkaSubgraphExporter()) {
        // Setup Kafka connection
        final KafkaProducer<String, RyaSubGraph> producer = new KafkaProducer<String, RyaSubGraph>(exportParams.listAllConfig());
        // Create the exporter
        final IncrementalRyaSubGraphExporter exporter = new KafkaRyaSubGraphExporter(producer);
        return Optional.of(exporter);
    } else {
        return Optional.absent();
    }
}
Also used : KafkaProducer(org.apache.kafka.clients.producer.KafkaProducer) RyaSubGraph(org.apache.rya.api.domain.RyaSubGraph) IncrementalRyaSubGraphExporter(org.apache.rya.indexing.pcj.fluo.app.export.IncrementalRyaSubGraphExporter)

Example 4 with RyaSubGraph

use of org.apache.rya.api.domain.RyaSubGraph in project incubator-rya by apache.

the class ConstructQueryResultUpdater method updateConstructQueryResults.

/**
 * Updates the Construct Query results by applying the {@link ConnstructGraph} to
 * create a {@link RyaSubGraph} and then writing the subgraph to {@link FluoQueryColumns#CONSTRUCT_STATEMENTS}.
 * @param tx - transaction used to write the subgraph
 * @param bs - BindingSet that the ConstructProjection expands into a subgraph
 * @param metadata - metadata that the ConstructProjection is extracted from
 */
public void updateConstructQueryResults(TransactionBase tx, VisibilityBindingSet bs, ConstructQueryMetadata metadata) {
    String nodeId = metadata.getNodeId();
    VariableOrder varOrder = metadata.getVariableOrder();
    Column column = FluoQueryColumns.CONSTRUCT_STATEMENTS;
    ConstructGraph graph = metadata.getConstructGraph();
    String parentId = metadata.getParentNodeId();
    // Create the Row Key for the emitted binding set. It does not contain visibilities.
    final Bytes resultRow = makeRowKey(nodeId, varOrder, bs);
    // If this is a new binding set, then emit it.
    if (tx.get(resultRow, column) == null || varOrder.getVariableOrders().size() < bs.size()) {
        Set<RyaStatement> statements = graph.createGraphFromBindingSet(bs);
        RyaSubGraph subgraph = new RyaSubGraph(parentId, statements);
        final Bytes nodeValueBytes = Bytes.of(serializer.toBytes(subgraph));
        log.trace("Transaction ID: " + tx.getStartTimestamp() + "\n" + "New Binding Set: " + subgraph + "\n");
        tx.set(resultRow, column, nodeValueBytes);
    }
}
Also used : Bytes(org.apache.fluo.api.data.Bytes) RyaSubGraph(org.apache.rya.api.domain.RyaSubGraph) Column(org.apache.fluo.api.data.Column) VariableOrder(org.apache.rya.indexing.pcj.storage.accumulo.VariableOrder) RyaStatement(org.apache.rya.api.domain.RyaStatement)

Example 5 with RyaSubGraph

use of org.apache.rya.api.domain.RyaSubGraph in project incubator-rya by apache.

the class KafkaRyaSubGraphExportIT method nestedConstructQuery.

@Test
public void nestedConstructQuery() throws Exception {
    // A query that groups what is aggregated by one of the keys.
    final String sparql = "CONSTRUCT { " + "_:b a <urn:highSpeedTrafficArea> . " + "_:b <urn:hasCount> ?obsCount . " + "_:b <urn:hasLocation> ?location ." + "_:b <urn:hasAverageVelocity> ?avgVelocity ." + "} WHERE { " + "FILTER(?obsCount > 1) " + "{ " + "SELECT ?location (count(?obs) AS ?obsCount) (avg(?velocity) AS ?avgVelocity) " + "WHERE { " + "FILTER(?velocity > 75) " + "?obs <urn:hasVelocity> ?velocity. " + "?obs <urn:hasLocation> ?location. " + "}GROUP BY ?location }}";
    // Create the Statements that will be loaded into Rya.
    final ValueFactory vf = new ValueFactoryImpl();
    final Collection<Statement> statements = Sets.newHashSet(vf.createStatement(vf.createURI("urn:obs1"), vf.createURI("urn:hasVelocity"), vf.createLiteral(77)), vf.createStatement(vf.createURI("urn:obs1"), vf.createURI("urn:hasLocation"), vf.createLiteral("OldTown")), vf.createStatement(vf.createURI("urn:obs2"), vf.createURI("urn:hasVelocity"), vf.createLiteral(81)), vf.createStatement(vf.createURI("urn:obs2"), vf.createURI("urn:hasLocation"), vf.createLiteral("OldTown")), vf.createStatement(vf.createURI("urn:obs3"), vf.createURI("urn:hasVelocity"), vf.createLiteral(70)), vf.createStatement(vf.createURI("urn:obs3"), vf.createURI("urn:hasLocation"), vf.createLiteral("OldTown")), vf.createStatement(vf.createURI("urn:obs5"), vf.createURI("urn:hasVelocity"), vf.createLiteral(87)), vf.createStatement(vf.createURI("urn:obs5"), vf.createURI("urn:hasLocation"), vf.createLiteral("Rosslyn")), vf.createStatement(vf.createURI("urn:obs6"), vf.createURI("urn:hasVelocity"), vf.createLiteral(81)), vf.createStatement(vf.createURI("urn:obs6"), vf.createURI("urn:hasLocation"), vf.createLiteral("Rosslyn")), vf.createStatement(vf.createURI("urn:obs7"), vf.createURI("urn:hasVelocity"), vf.createLiteral(67)), vf.createStatement(vf.createURI("urn:obs7"), vf.createURI("urn:hasLocation"), vf.createLiteral("Clarendon")), vf.createStatement(vf.createURI("urn:obs8"), vf.createURI("urn:hasVelocity"), vf.createLiteral(77)), vf.createStatement(vf.createURI("urn:obs8"), vf.createURI("urn:hasLocation"), vf.createLiteral("Ballston")), vf.createStatement(vf.createURI("urn:obs9"), vf.createURI("urn:hasVelocity"), vf.createLiteral(87)), vf.createStatement(vf.createURI("urn:obs9"), vf.createURI("urn:hasLocation"), vf.createLiteral("FallsChurch")));
    // Create the PCJ in Fluo and load the statements into Rya.
    final String pcjId = loadStatements(sparql, statements);
    // Verify the end results of the query match the expected results.
    final Set<RyaSubGraph> results = readAllResults(pcjId);
    RyaStatement statement1 = new RyaStatement(new RyaURI("urn:obs1"), new RyaURI("urn:hasCount"), new RyaType(XMLSchema.INTEGER, "2"));
    RyaStatement statement2 = new RyaStatement(new RyaURI("urn:obs1"), new RyaURI("urn:hasAverageVelocity"), new RyaType(XMLSchema.DECIMAL, "84"));
    RyaStatement statement3 = new RyaStatement(new RyaURI("urn:obs1"), new RyaURI("urn:hasLocation"), new RyaType("Rosslyn"));
    RyaStatement statement4 = new RyaStatement(new RyaURI("urn:obs1"), new RyaURI(RDF.TYPE.toString()), new RyaURI("urn:highSpeedTrafficArea"));
    RyaStatement statement5 = new RyaStatement(new RyaURI("urn:obs2"), new RyaURI("urn:hasCount"), new RyaType(XMLSchema.INTEGER, "2"));
    RyaStatement statement6 = new RyaStatement(new RyaURI("urn:obs2"), new RyaURI("urn:hasAverageVelocity"), new RyaType(XMLSchema.DECIMAL, "79"));
    RyaStatement statement7 = new RyaStatement(new RyaURI("urn:obs2"), new RyaURI("urn:hasLocation"), new RyaType("OldTown"));
    RyaStatement statement8 = new RyaStatement(new RyaURI("urn:obs2"), new RyaURI(RDF.TYPE.toString()), new RyaURI("urn:highSpeedTrafficArea"));
    final Set<RyaSubGraph> expectedResults = new HashSet<>();
    RyaSubGraph subGraph1 = new RyaSubGraph(pcjId);
    Set<RyaStatement> stmnts1 = new HashSet<>(Arrays.asList(statement1, statement2, statement3, statement4));
    subGraph1.setStatements(stmnts1);
    expectedResults.add(subGraph1);
    RyaSubGraph subGraph2 = new RyaSubGraph(pcjId);
    Set<RyaStatement> stmnts2 = new HashSet<>(Arrays.asList(statement5, statement6, statement7, statement8));
    subGraph2.setStatements(stmnts2);
    expectedResults.add(subGraph2);
    Assert.assertEquals(expectedResults.size(), results.size());
    ConstructGraphTestUtils.subGraphsEqualIgnoresBlankNode(expectedResults, results);
    ;
}
Also used : RyaSubGraph(org.apache.rya.api.domain.RyaSubGraph) RyaURI(org.apache.rya.api.domain.RyaURI) Statement(org.openrdf.model.Statement) RyaStatement(org.apache.rya.api.domain.RyaStatement) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) RyaStatement(org.apache.rya.api.domain.RyaStatement) ValueFactory(org.openrdf.model.ValueFactory) RyaType(org.apache.rya.api.domain.RyaType) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

RyaSubGraph (org.apache.rya.api.domain.RyaSubGraph)15 RyaStatement (org.apache.rya.api.domain.RyaStatement)10 RyaURI (org.apache.rya.api.domain.RyaURI)7 Test (org.junit.Test)7 HashSet (java.util.HashSet)5 RyaType (org.apache.rya.api.domain.RyaType)2 ResultExportException (org.apache.rya.indexing.pcj.fluo.app.export.IncrementalBindingSetExporter.ResultExportException)2 Statement (org.openrdf.model.Statement)2 ValueFactory (org.openrdf.model.ValueFactory)2 ValueFactoryImpl (org.openrdf.model.impl.ValueFactoryImpl)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HashMap (java.util.HashMap)1 Properties (java.util.Properties)1 Bytes (org.apache.fluo.api.data.Bytes)1 Column (org.apache.fluo.api.data.Column)1 KafkaConsumer (org.apache.kafka.clients.consumer.KafkaConsumer)1 KafkaProducer (org.apache.kafka.clients.producer.KafkaProducer)1 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)1 RecordMetadata (org.apache.kafka.clients.producer.RecordMetadata)1 StringDeserializer (org.apache.kafka.common.serialization.StringDeserializer)1