Search in sources :

Example 76 with MapBindingSet

use of org.openrdf.query.impl.MapBindingSet in project incubator-rya by apache.

the class KafkaExportIT method average.

@Test
public void average() throws Exception {
    // A query that finds the average price for an item that is in the inventory.
    final String sparql = "SELECT (avg(?price) as ?averagePrice) { " + "?item <urn:price> ?price . " + "}";
    // 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:apple"), vf.createURI("urn:price"), vf.createLiteral(3)), vf.createStatement(vf.createURI("urn:gum"), vf.createURI("urn:price"), vf.createLiteral(4)), vf.createStatement(vf.createURI("urn:sandwich"), vf.createURI("urn:price"), vf.createLiteral(8)));
    // Create the PCJ in Fluo and load the statements into Rya.
    final String pcjId = loadDataAndCreateQuery(sparql, statements);
    // Create the expected results of the SPARQL query once the PCJ has been computed.
    final MapBindingSet expectedResult = new MapBindingSet();
    expectedResult.addBinding("averagePrice", vf.createLiteral("5", XMLSchema.DECIMAL));
    // Ensure the last result matches the expected result.
    final VisibilityBindingSet result = readLastResult(pcjId);
    assertEquals(expectedResult, result);
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) Statement(org.openrdf.model.Statement) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) ValueFactory(org.openrdf.model.ValueFactory) MapBindingSet(org.openrdf.query.impl.MapBindingSet) Test(org.junit.Test)

Example 77 with MapBindingSet

use of org.openrdf.query.impl.MapBindingSet in project incubator-rya by apache.

the class KafkaExportIT method readGroupedResults.

private Set<VisibilityBindingSet> readGroupedResults(final String pcjId, final VariableOrder groupByVars) {
    requireNonNull(pcjId);
    // Read the results from the Kafka topic. The last one for each set of Group By values is an aggregation result.
    // The key in this map is a Binding Set containing only the group by variables.
    final Map<BindingSet, VisibilityBindingSet> results = new HashMap<>();
    try (final KafkaConsumer<String, VisibilityBindingSet> consumer = makeConsumer(pcjId)) {
        final ConsumerRecords<String, VisibilityBindingSet> records = consumer.poll(5000);
        final Iterator<ConsumerRecord<String, VisibilityBindingSet>> recordIterator = records.iterator();
        while (recordIterator.hasNext()) {
            final VisibilityBindingSet visBindingSet = recordIterator.next().value();
            final MapBindingSet key = new MapBindingSet();
            for (final String groupByBar : groupByVars) {
                key.addBinding(visBindingSet.getBinding(groupByBar));
            }
            results.put(key, visBindingSet);
        }
    }
    return Sets.newHashSet(results.values());
}
Also used : MapBindingSet(org.openrdf.query.impl.MapBindingSet) VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) BindingSet(org.openrdf.query.BindingSet) VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) HashMap(java.util.HashMap) MapBindingSet(org.openrdf.query.impl.MapBindingSet) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord)

Example 78 with MapBindingSet

use of org.openrdf.query.impl.MapBindingSet in project incubator-rya by apache.

the class QueryIT method nestedJoinPeriodicQueryWithAggregationAndGroupBy.

@Test
public void nestedJoinPeriodicQueryWithAggregationAndGroupBy() throws Exception {
    final String query = // n
    "prefix function: <http://org.apache.rya/function#> " + // n
    "prefix time: <http://www.w3.org/2006/time#> " + "select ?location ?total ?population " + "where { Filter(?total > 1)" + "?location <uri:hasPopulation> ?population . {" + // n
    "select ?location (count(?obs) as ?total) where {" + // n
    "Filter(function:periodic(?time, 2, .5, time:hours)) " + // n
    "?obs <uri:hasTime> ?time. " + // n
    "?obs <uri:hasLoc> ?location } group by ?location }}";
    // Create the Statements that will be loaded into Rya.
    final ValueFactory vf = new ValueFactoryImpl();
    final DatatypeFactory dtf = DatatypeFactory.newInstance();
    final ZonedDateTime time = ZonedDateTime.now();
    final long currentTime = time.toInstant().toEpochMilli();
    final ZonedDateTime zTime1 = time.minusMinutes(30);
    final String time1 = zTime1.format(DateTimeFormatter.ISO_INSTANT);
    final ZonedDateTime zTime2 = zTime1.minusMinutes(30);
    final String time2 = zTime2.format(DateTimeFormatter.ISO_INSTANT);
    final ZonedDateTime zTime3 = zTime2.minusMinutes(30);
    final String time3 = zTime3.format(DateTimeFormatter.ISO_INSTANT);
    final ZonedDateTime zTime4 = zTime3.minusMinutes(30);
    final String time4 = zTime4.format(DateTimeFormatter.ISO_INSTANT);
    final Collection<Statement> statements = Sets.newHashSet(vf.createStatement(vf.createURI("urn:obs_1"), vf.createURI("uri:hasTime"), vf.createLiteral(dtf.newXMLGregorianCalendar(time1))), vf.createStatement(vf.createURI("urn:obs_1"), vf.createURI("uri:hasLoc"), vf.createURI("uri:loc_1")), vf.createStatement(vf.createURI("uri:loc_1"), vf.createURI("uri:hasPopulation"), vf.createLiteral(3500)), vf.createStatement(vf.createURI("uri:loc_2"), vf.createURI("uri:hasPopulation"), vf.createLiteral(8000)), vf.createStatement(vf.createURI("urn:obs_2"), vf.createURI("uri:hasTime"), vf.createLiteral(dtf.newXMLGregorianCalendar(time2))), vf.createStatement(vf.createURI("urn:obs_2"), vf.createURI("uri:hasLoc"), vf.createURI("uri:loc_2")), vf.createStatement(vf.createURI("urn:obs_3"), vf.createURI("uri:hasTime"), vf.createLiteral(dtf.newXMLGregorianCalendar(time3))), vf.createStatement(vf.createURI("urn:obs_3"), vf.createURI("uri:hasLoc"), vf.createURI("uri:loc_3")), vf.createStatement(vf.createURI("urn:obs_4"), vf.createURI("uri:hasTime"), vf.createLiteral(dtf.newXMLGregorianCalendar(time4))), vf.createStatement(vf.createURI("urn:obs_4"), vf.createURI("uri:hasLoc"), vf.createURI("uri:loc_4")), vf.createStatement(vf.createURI("urn:obs_5"), vf.createURI("uri:hasTime"), vf.createLiteral(dtf.newXMLGregorianCalendar(time4))), vf.createStatement(vf.createURI("urn:obs_5"), vf.createURI("uri:hasLoc"), vf.createURI("uri:loc_1")), vf.createStatement(vf.createURI("urn:obs_6"), vf.createURI("uri:hasTime"), vf.createLiteral(dtf.newXMLGregorianCalendar(time3))), vf.createStatement(vf.createURI("urn:obs_6"), vf.createURI("uri:hasLoc"), vf.createURI("uri:loc_2")));
    // Create the expected results of the SPARQL query once the PCJ has been computed.
    final Set<BindingSet> expectedResults = new HashSet<>();
    final long period = 1800000;
    final long binId = currentTime / period * period;
    MapBindingSet bs = new MapBindingSet();
    bs.addBinding("total", vf.createLiteral("2", XMLSchema.INTEGER));
    bs.addBinding("location", vf.createURI("uri:loc_1"));
    bs.addBinding("population", vf.createLiteral("3500", XMLSchema.INTEGER));
    bs.addBinding("periodicBinId", vf.createLiteral(binId));
    expectedResults.add(bs);
    bs = new MapBindingSet();
    bs.addBinding("total", vf.createLiteral("2", XMLSchema.INTEGER));
    bs.addBinding("location", vf.createURI("uri:loc_2"));
    bs.addBinding("population", vf.createLiteral("8000", XMLSchema.INTEGER));
    bs.addBinding("periodicBinId", vf.createLiteral(binId));
    expectedResults.add(bs);
    bs = new MapBindingSet();
    bs.addBinding("total", vf.createLiteral("2", XMLSchema.INTEGER));
    bs.addBinding("location", vf.createURI("uri:loc_2"));
    bs.addBinding("population", vf.createLiteral("8000", XMLSchema.INTEGER));
    bs.addBinding("periodicBinId", vf.createLiteral(binId + period));
    expectedResults.add(bs);
    // Verify the end results of the query match the expected results.
    runTest(query, statements, expectedResults, ExportStrategy.PERIODIC);
}
Also used : MapBindingSet(org.openrdf.query.impl.MapBindingSet) BindingSet(org.openrdf.query.BindingSet) DatatypeFactory(javax.xml.datatype.DatatypeFactory) ZonedDateTime(java.time.ZonedDateTime) Statement(org.openrdf.model.Statement) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) ValueFactory(org.openrdf.model.ValueFactory) MapBindingSet(org.openrdf.query.impl.MapBindingSet) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 79 with MapBindingSet

use of org.openrdf.query.impl.MapBindingSet in project incubator-rya by apache.

the class QueryIT method periodicQueryTestWithAggregationAndGroupBy.

@Test
public void periodicQueryTestWithAggregationAndGroupBy() throws Exception {
    final String query = // n
    "prefix function: <http://org.apache.rya/function#> " + // n
    "prefix time: <http://www.w3.org/2006/time#> " + // n
    "select ?id (count(?obs) as ?total) where {" + // n
    "Filter(function:periodic(?time, 2, .5, time:hours)) " + // n
    "?obs <uri:hasTime> ?time. " + // n
    "?obs <uri:hasId> ?id } group by ?id";
    // Create the Statements that will be loaded into Rya.
    final ValueFactory vf = new ValueFactoryImpl();
    final DatatypeFactory dtf = DatatypeFactory.newInstance();
    final ZonedDateTime time = ZonedDateTime.now();
    final long currentTime = time.toInstant().toEpochMilli();
    final ZonedDateTime zTime1 = time.minusMinutes(30);
    final String time1 = zTime1.format(DateTimeFormatter.ISO_INSTANT);
    final ZonedDateTime zTime2 = zTime1.minusMinutes(30);
    final String time2 = zTime2.format(DateTimeFormatter.ISO_INSTANT);
    final ZonedDateTime zTime3 = zTime2.minusMinutes(30);
    final String time3 = zTime3.format(DateTimeFormatter.ISO_INSTANT);
    final ZonedDateTime zTime4 = zTime3.minusMinutes(30);
    final String time4 = zTime4.format(DateTimeFormatter.ISO_INSTANT);
    final Collection<Statement> statements = Sets.newHashSet(vf.createStatement(vf.createURI("urn:obs_1"), vf.createURI("uri:hasTime"), vf.createLiteral(dtf.newXMLGregorianCalendar(time1))), vf.createStatement(vf.createURI("urn:obs_1"), vf.createURI("uri:hasId"), vf.createLiteral("id_1")), vf.createStatement(vf.createURI("urn:obs_2"), vf.createURI("uri:hasTime"), vf.createLiteral(dtf.newXMLGregorianCalendar(time2))), vf.createStatement(vf.createURI("urn:obs_2"), vf.createURI("uri:hasId"), vf.createLiteral("id_2")), vf.createStatement(vf.createURI("urn:obs_3"), vf.createURI("uri:hasTime"), vf.createLiteral(dtf.newXMLGregorianCalendar(time3))), vf.createStatement(vf.createURI("urn:obs_3"), vf.createURI("uri:hasId"), vf.createLiteral("id_3")), vf.createStatement(vf.createURI("urn:obs_4"), vf.createURI("uri:hasTime"), vf.createLiteral(dtf.newXMLGregorianCalendar(time4))), vf.createStatement(vf.createURI("urn:obs_4"), vf.createURI("uri:hasId"), vf.createLiteral("id_4")), vf.createStatement(vf.createURI("urn:obs_1"), vf.createURI("uri:hasTime"), vf.createLiteral(dtf.newXMLGregorianCalendar(time4))), vf.createStatement(vf.createURI("urn:obs_1"), vf.createURI("uri:hasId"), vf.createLiteral("id_1")), vf.createStatement(vf.createURI("urn:obs_2"), vf.createURI("uri:hasTime"), vf.createLiteral(dtf.newXMLGregorianCalendar(time3))), vf.createStatement(vf.createURI("urn:obs_2"), vf.createURI("uri:hasId"), vf.createLiteral("id_2")));
    // Create the expected results of the SPARQL query once the PCJ has been computed.
    final Set<BindingSet> expectedResults = new HashSet<>();
    final long period = 1800000;
    final long binId = currentTime / period * period;
    MapBindingSet bs = new MapBindingSet();
    bs.addBinding("total", vf.createLiteral("2", XMLSchema.INTEGER));
    bs.addBinding("id", vf.createLiteral("id_1", XMLSchema.STRING));
    bs.addBinding("periodicBinId", vf.createLiteral(binId));
    expectedResults.add(bs);
    bs = new MapBindingSet();
    bs.addBinding("total", vf.createLiteral("2", XMLSchema.INTEGER));
    bs.addBinding("id", vf.createLiteral("id_2", XMLSchema.STRING));
    bs.addBinding("periodicBinId", vf.createLiteral(binId));
    expectedResults.add(bs);
    bs = new MapBindingSet();
    bs.addBinding("total", vf.createLiteral("1", XMLSchema.INTEGER));
    bs.addBinding("id", vf.createLiteral("id_3", XMLSchema.STRING));
    bs.addBinding("periodicBinId", vf.createLiteral(binId));
    expectedResults.add(bs);
    bs = new MapBindingSet();
    bs.addBinding("total", vf.createLiteral("1", XMLSchema.INTEGER));
    bs.addBinding("id", vf.createLiteral("id_4", XMLSchema.STRING));
    bs.addBinding("periodicBinId", vf.createLiteral(binId));
    expectedResults.add(bs);
    bs = new MapBindingSet();
    bs.addBinding("total", vf.createLiteral("1", XMLSchema.INTEGER));
    bs.addBinding("id", vf.createLiteral("id_1", XMLSchema.STRING));
    bs.addBinding("periodicBinId", vf.createLiteral(binId + period));
    expectedResults.add(bs);
    bs = new MapBindingSet();
    bs.addBinding("total", vf.createLiteral("2", XMLSchema.INTEGER));
    bs.addBinding("id", vf.createLiteral("id_2", XMLSchema.STRING));
    bs.addBinding("periodicBinId", vf.createLiteral(binId + period));
    expectedResults.add(bs);
    bs = new MapBindingSet();
    bs.addBinding("total", vf.createLiteral("1", XMLSchema.INTEGER));
    bs.addBinding("id", vf.createLiteral("id_3", XMLSchema.STRING));
    bs.addBinding("periodicBinId", vf.createLiteral(binId + period));
    expectedResults.add(bs);
    bs = new MapBindingSet();
    bs.addBinding("total", vf.createLiteral("1", XMLSchema.INTEGER));
    bs.addBinding("id", vf.createLiteral("id_1", XMLSchema.STRING));
    bs.addBinding("periodicBinId", vf.createLiteral(binId + 2 * period));
    expectedResults.add(bs);
    bs = new MapBindingSet();
    bs.addBinding("total", vf.createLiteral("1", XMLSchema.INTEGER));
    bs.addBinding("id", vf.createLiteral("id_2", XMLSchema.STRING));
    bs.addBinding("periodicBinId", vf.createLiteral(binId + 2 * period));
    expectedResults.add(bs);
    bs = new MapBindingSet();
    bs.addBinding("total", vf.createLiteral("1", XMLSchema.INTEGER));
    bs.addBinding("id", vf.createLiteral("id_1", XMLSchema.STRING));
    bs.addBinding("periodicBinId", vf.createLiteral(binId + 3 * period));
    expectedResults.add(bs);
    // Verify the end results of the query match the expected results.
    runTest(query, statements, expectedResults, ExportStrategy.PERIODIC);
}
Also used : MapBindingSet(org.openrdf.query.impl.MapBindingSet) BindingSet(org.openrdf.query.BindingSet) DatatypeFactory(javax.xml.datatype.DatatypeFactory) ZonedDateTime(java.time.ZonedDateTime) Statement(org.openrdf.model.Statement) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) ValueFactory(org.openrdf.model.ValueFactory) MapBindingSet(org.openrdf.query.impl.MapBindingSet) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 80 with MapBindingSet

use of org.openrdf.query.impl.MapBindingSet in project incubator-rya by apache.

the class QueryIT method periodicQueryTestWithoutAggregation.

@Test
public void periodicQueryTestWithoutAggregation() throws Exception {
    final String query = // n
    "prefix function: <http://org.apache.rya/function#> " + // n
    "prefix time: <http://www.w3.org/2006/time#> " + // n
    "select ?id where {" + // n
    "Filter(function:periodic(?time, 2, .5, time:hours)) " + // n
    "?obs <uri:hasTime> ?time. " + // n
    "?obs <uri:hasId> ?id }";
    // Create the Statements that will be loaded into Rya.
    final ValueFactory vf = new ValueFactoryImpl();
    final DatatypeFactory dtf = DatatypeFactory.newInstance();
    final ZonedDateTime time = ZonedDateTime.now();
    final long currentTime = time.toInstant().toEpochMilli();
    final ZonedDateTime zTime1 = time.minusMinutes(30);
    final String time1 = zTime1.format(DateTimeFormatter.ISO_INSTANT);
    final ZonedDateTime zTime2 = zTime1.minusMinutes(30);
    final String time2 = zTime2.format(DateTimeFormatter.ISO_INSTANT);
    final ZonedDateTime zTime3 = zTime2.minusMinutes(30);
    final String time3 = zTime3.format(DateTimeFormatter.ISO_INSTANT);
    final ZonedDateTime zTime4 = zTime3.minusMinutes(30);
    final String time4 = zTime4.format(DateTimeFormatter.ISO_INSTANT);
    final Collection<Statement> statements = Sets.newHashSet(vf.createStatement(vf.createURI("urn:obs_1"), vf.createURI("uri:hasTime"), vf.createLiteral(dtf.newXMLGregorianCalendar(time1))), vf.createStatement(vf.createURI("urn:obs_1"), vf.createURI("uri:hasId"), vf.createLiteral("id_1")), vf.createStatement(vf.createURI("urn:obs_2"), vf.createURI("uri:hasTime"), vf.createLiteral(dtf.newXMLGregorianCalendar(time2))), vf.createStatement(vf.createURI("urn:obs_2"), vf.createURI("uri:hasId"), vf.createLiteral("id_2")), vf.createStatement(vf.createURI("urn:obs_3"), vf.createURI("uri:hasTime"), vf.createLiteral(dtf.newXMLGregorianCalendar(time3))), vf.createStatement(vf.createURI("urn:obs_3"), vf.createURI("uri:hasId"), vf.createLiteral("id_3")), vf.createStatement(vf.createURI("urn:obs_4"), vf.createURI("uri:hasTime"), vf.createLiteral(dtf.newXMLGregorianCalendar(time4))), vf.createStatement(vf.createURI("urn:obs_4"), vf.createURI("uri:hasId"), vf.createLiteral("id_4")));
    // Create the expected results of the SPARQL query once the PCJ has been computed.
    final Set<BindingSet> expectedResults = new HashSet<>();
    final long period = 1800000;
    final long binId = currentTime / period * period;
    MapBindingSet bs = new MapBindingSet();
    bs.addBinding("id", vf.createLiteral("id_1", XMLSchema.STRING));
    bs.addBinding("periodicBinId", vf.createLiteral(binId));
    expectedResults.add(bs);
    bs = new MapBindingSet();
    bs.addBinding("id", vf.createLiteral("id_1", XMLSchema.STRING));
    bs.addBinding("periodicBinId", vf.createLiteral(binId + period));
    expectedResults.add(bs);
    bs = new MapBindingSet();
    bs.addBinding("id", vf.createLiteral("id_1", XMLSchema.STRING));
    bs.addBinding("periodicBinId", vf.createLiteral(binId + 2 * period));
    expectedResults.add(bs);
    bs = new MapBindingSet();
    bs.addBinding("id", vf.createLiteral("id_1", XMLSchema.STRING));
    bs.addBinding("periodicBinId", vf.createLiteral(binId + 3 * period));
    expectedResults.add(bs);
    bs = new MapBindingSet();
    bs.addBinding("id", vf.createLiteral("id_2", XMLSchema.STRING));
    bs.addBinding("periodicBinId", vf.createLiteral(binId));
    expectedResults.add(bs);
    bs = new MapBindingSet();
    bs.addBinding("id", vf.createLiteral("id_2", XMLSchema.STRING));
    bs.addBinding("periodicBinId", vf.createLiteral(binId + period));
    expectedResults.add(bs);
    bs = new MapBindingSet();
    bs.addBinding("id", vf.createLiteral("id_2", XMLSchema.STRING));
    bs.addBinding("periodicBinId", vf.createLiteral(binId + 2 * period));
    expectedResults.add(bs);
    bs = new MapBindingSet();
    bs.addBinding("id", vf.createLiteral("id_3", XMLSchema.STRING));
    bs.addBinding("periodicBinId", vf.createLiteral(binId));
    expectedResults.add(bs);
    bs = new MapBindingSet();
    bs.addBinding("id", vf.createLiteral("id_3", XMLSchema.STRING));
    bs.addBinding("periodicBinId", vf.createLiteral(binId + period));
    expectedResults.add(bs);
    bs = new MapBindingSet();
    bs.addBinding("id", vf.createLiteral("id_4", XMLSchema.STRING));
    bs.addBinding("periodicBinId", vf.createLiteral(binId));
    expectedResults.add(bs);
    // Verify the end results of the query match the expected results.
    runTest(query, statements, expectedResults, ExportStrategy.PERIODIC);
}
Also used : MapBindingSet(org.openrdf.query.impl.MapBindingSet) BindingSet(org.openrdf.query.BindingSet) DatatypeFactory(javax.xml.datatype.DatatypeFactory) ZonedDateTime(java.time.ZonedDateTime) Statement(org.openrdf.model.Statement) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) ValueFactory(org.openrdf.model.ValueFactory) MapBindingSet(org.openrdf.query.impl.MapBindingSet) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

MapBindingSet (org.openrdf.query.impl.MapBindingSet)174 Test (org.junit.Test)155 ValueFactory (org.openrdf.model.ValueFactory)99 VisibilityBindingSet (org.apache.rya.api.model.VisibilityBindingSet)96 ValueFactoryImpl (org.openrdf.model.impl.ValueFactoryImpl)91 BindingSet (org.openrdf.query.BindingSet)84 HashSet (java.util.HashSet)81 Statement (org.openrdf.model.Statement)43 URIImpl (org.openrdf.model.impl.URIImpl)31 ArrayList (java.util.ArrayList)30 VisibilityStatement (org.apache.rya.api.model.VisibilityStatement)24 UUID (java.util.UUID)23 PrecomputedJoinStorage (org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage)20 TopologyFactory (org.apache.rya.streams.kafka.topology.TopologyFactory)20 TopologyBuilder (org.apache.kafka.streams.processor.TopologyBuilder)19 RandomUUIDFactory (org.apache.rya.api.function.projection.RandomUUIDFactory)19 Connector (org.apache.accumulo.core.client.Connector)18 AccumuloPcjStorage (org.apache.rya.indexing.pcj.storage.accumulo.AccumuloPcjStorage)16 PcjMetadata (org.apache.rya.indexing.pcj.storage.PcjMetadata)15 RyaURI (org.apache.rya.api.domain.RyaURI)14