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;
}
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;
}
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();
}
}
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);
}
}
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);
;
}
Aggregations