Search in sources :

Example 46 with VariableOrder

use of org.apache.rya.indexing.pcj.storage.accumulo.VariableOrder in project incubator-rya by apache.

the class KafkaExportIT method nestedWithJoinGroupByManyBindings_averages.

@Test
public void nestedWithJoinGroupByManyBindings_averages() throws Exception {
    // A query that groups what is aggregated by two of the keys.
    final String sparql = "SELECT ?type ?location ?averagePrice ?milkType {" + "FILTER(?averagePrice > 4) " + "?type <urn:hasMilkType> ?milkType ." + "{SELECT ?type ?location (avg(?price) as ?averagePrice) {" + "?id <urn:type> ?type . " + "?id <urn:location> ?location ." + "?id <urn:price> ?price ." + "} " + "GROUP BY ?type ?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:1"), vf.createURI("urn:type"), vf.createURI("urn:blue")), vf.createStatement(vf.createURI("urn:1"), vf.createURI("urn:location"), vf.createLiteral("France")), vf.createStatement(vf.createURI("urn:1"), vf.createURI("urn:price"), vf.createLiteral(8.5)), vf.createStatement(vf.createURI("urn:blue"), vf.createURI("urn:hasMilkType"), vf.createLiteral("cow", XMLSchema.STRING)), vf.createStatement(vf.createURI("urn:2"), vf.createURI("urn:type"), vf.createURI("urn:american")), vf.createStatement(vf.createURI("urn:2"), vf.createURI("urn:location"), vf.createLiteral("USA")), vf.createStatement(vf.createURI("urn:2"), vf.createURI("urn:price"), vf.createLiteral(.99)), vf.createStatement(vf.createURI("urn:3"), vf.createURI("urn:type"), vf.createURI("urn:cheddar")), vf.createStatement(vf.createURI("urn:3"), vf.createURI("urn:location"), vf.createLiteral("USA")), vf.createStatement(vf.createURI("urn:3"), vf.createURI("urn:price"), vf.createLiteral(5.25)), // French items that will be averaged.
    vf.createStatement(vf.createURI("urn:4"), vf.createURI("urn:type"), vf.createURI("urn:goat")), vf.createStatement(vf.createURI("urn:4"), vf.createURI("urn:location"), vf.createLiteral("France")), vf.createStatement(vf.createURI("urn:4"), vf.createURI("urn:price"), vf.createLiteral(6.5)), vf.createStatement(vf.createURI("urn:goat"), vf.createURI("urn:hasMilkType"), vf.createLiteral("goat", XMLSchema.STRING)), vf.createStatement(vf.createURI("urn:5"), vf.createURI("urn:type"), vf.createURI("urn:fontina")), vf.createStatement(vf.createURI("urn:5"), vf.createURI("urn:location"), vf.createLiteral("Italy")), vf.createStatement(vf.createURI("urn:5"), vf.createURI("urn:price"), vf.createLiteral(3.99)), vf.createStatement(vf.createURI("urn:fontina"), vf.createURI("urn:hasMilkType"), vf.createLiteral("cow", XMLSchema.STRING)), vf.createStatement(vf.createURI("urn:6"), vf.createURI("urn:type"), vf.createURI("urn:fontina")), vf.createStatement(vf.createURI("urn:6"), vf.createURI("urn:location"), vf.createLiteral("Italy")), vf.createStatement(vf.createURI("urn:6"), vf.createURI("urn:price"), vf.createLiteral(4.99)));
    // 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 Set<VisibilityBindingSet> expectedResults = new HashSet<>();
    MapBindingSet bs = new MapBindingSet();
    bs.addBinding("type", vf.createURI("urn:blue"));
    bs.addBinding("location", vf.createLiteral("France", XMLSchema.STRING));
    bs.addBinding("averagePrice", vf.createLiteral("8.5", XMLSchema.DECIMAL));
    bs.addBinding("milkType", vf.createLiteral("cow", XMLSchema.STRING));
    expectedResults.add(new VisibilityBindingSet(bs));
    bs = new MapBindingSet();
    bs.addBinding("type", vf.createURI("urn:goat"));
    bs.addBinding("location", vf.createLiteral("France", XMLSchema.STRING));
    bs.addBinding("averagePrice", vf.createLiteral("6.5", XMLSchema.DECIMAL));
    bs.addBinding("milkType", vf.createLiteral("goat", XMLSchema.STRING));
    expectedResults.add(new VisibilityBindingSet(bs));
    bs = new MapBindingSet();
    bs.addBinding("type", vf.createURI("urn:fontina"));
    bs.addBinding("location", vf.createLiteral("Italy", XMLSchema.STRING));
    bs.addBinding("averagePrice", vf.createLiteral("4.49", XMLSchema.DECIMAL));
    bs.addBinding("milkType", vf.createLiteral("cow", XMLSchema.STRING));
    expectedResults.add(new VisibilityBindingSet(bs));
    // Verify the end results of the query match the expected results.
    final Set<VisibilityBindingSet> results = readGroupedResults(pcjId, new VariableOrder("type", "location"));
    assertEquals(expectedResults, results);
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) Statement(org.openrdf.model.Statement) VariableOrder(org.apache.rya.indexing.pcj.storage.accumulo.VariableOrder) 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 47 with VariableOrder

use of org.apache.rya.indexing.pcj.storage.accumulo.VariableOrder in project incubator-rya by apache.

the class PcjMetadataRenderer method render.

/**
 * Pretty render the state of a PCJ.
 *
 * @param queryId - The ID of the query the metadata is from. (not null)
 * @param metadata - Metadata about one of the PCJs. (not null)
 * @return A pretty render of a PCJ's state.
 * @throws Exception The SPARQL within the metadata could not be parsed or
 *  it could not be rendered.
 */
public String render(final String queryId, final PcjMetadata metadata) throws Exception {
    checkNotNull(metadata);
    // Pretty format the cardinality.
    final String cardinality = NumberFormat.getInstance().format(metadata.getCardinality());
    // Pretty format and split the SPARQL query into lines.
    final SPARQLParser parser = new SPARQLParser();
    final SPARQLQueryRenderer renderer = new SPARQLQueryRenderer();
    final ParsedQuery pq = parser.parseQuery(metadata.getSparql(), null);
    final String prettySparql = renderer.render(pq);
    final String[] sparqlLines = StringUtils.split(prettySparql, '\n');
    // Split the variable orders into lines.
    final String[] varOrderLines = new String[metadata.getVarOrders().size()];
    int i = 0;
    for (final VariableOrder varOrder : metadata.getVarOrders()) {
        varOrderLines[i++] = varOrder.toString();
    }
    // Create the report.
    final Report.Builder builder = Report.builder();
    builder.appendItem(new ReportItem("Query ID", queryId));
    builder.appendItem(new ReportItem("Cardinality", cardinality));
    builder.appendItem(new ReportItem("Export Variable Orders", varOrderLines));
    builder.appendItem(new ReportItem("SPARQL", sparqlLines));
    return builder.build().toString();
}
Also used : SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) VariableOrder(org.apache.rya.indexing.pcj.storage.accumulo.VariableOrder) SPARQLQueryRenderer(org.openrdf.queryrender.sparql.SPARQLQueryRenderer) ReportItem(org.apache.rya.indexing.pcj.fluo.client.util.Report.ReportItem)

Example 48 with VariableOrder

use of org.apache.rya.indexing.pcj.storage.accumulo.VariableOrder in project incubator-rya by apache.

the class PcjMetadataRendererTest method formatManyMetdata.

@Test
public void formatManyMetdata() throws Exception {
    // Create the PcjMetadata that will be formatted as a report.
    final PcjMetadata metadata1 = new PcjMetadata("SELECT ?x ?y " + "WHERE { " + "?x <http://talksTo> <http://Eve>. " + "?y <http://worksAt> <http://Chipotle>." + "}", 12233423L, Sets.<VariableOrder>newHashSet(new VariableOrder("x", "y"), new VariableOrder("y", "x")));
    final PcjMetadata metadata2 = new PcjMetadata("SELECT ?x " + "WHERE { " + "?x <http://likes> <http://cookies>" + "}", 2342L, Sets.<VariableOrder>newHashSet(new VariableOrder("x")));
    final Map<String, PcjMetadata> metadata = new LinkedHashMap<>();
    metadata.put("query1", metadata1);
    metadata.put("query2", metadata2);
    // Run the test.
    final String expected = "---------------------------------------------------------------------\n" + "| Query ID               | query1                                   |\n" + "| Cardinality            | 12,233,423                               |\n" + "| Export Variable Orders | y;x                                      |\n" + "|                        | x;y                                      |\n" + "| SPARQL                 | select ?x ?y                             |\n" + "|                        | where {                                  |\n" + "|                        |   ?x <http://talksTo> <http://Eve>.      |\n" + "|                        |   ?y <http://worksAt> <http://Chipotle>. |\n" + "|                        | }                                        |\n" + "---------------------------------------------------------------------\n" + "\n" + "------------------------------------------------------------------\n" + "| Query ID               | query2                                |\n" + "| Cardinality            | 2,342                                 |\n" + "| Export Variable Orders | x                                     |\n" + "| SPARQL                 | select ?x                             |\n" + "|                        | where {                               |\n" + "|                        |   ?x <http://likes> <http://cookies>. |\n" + "|                        | }                                     |\n" + "------------------------------------------------------------------\n" + "\n";
    final PcjMetadataRenderer formatter = new PcjMetadataRenderer();
    assertEquals(expected, formatter.render(metadata));
}
Also used : VariableOrder(org.apache.rya.indexing.pcj.storage.accumulo.VariableOrder) PcjMetadataRenderer(org.apache.rya.indexing.pcj.fluo.client.util.PcjMetadataRenderer) PcjMetadata(org.apache.rya.indexing.pcj.storage.PcjMetadata) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 49 with VariableOrder

use of org.apache.rya.indexing.pcj.storage.accumulo.VariableOrder in project incubator-rya by apache.

the class FluoQueryMetadataDAOIT method periodicQueryMetadataTest.

@Test
public void periodicQueryMetadataTest() {
    final FluoQueryMetadataDAO dao = new FluoQueryMetadataDAO();
    // Create the object that will be serialized.
    final PeriodicQueryMetadata originalMetadata = PeriodicQueryMetadata.builder().setNodeId("nodeId").setParentNodeId("parentNodeId").setVarOrder(new VariableOrder("a", "b", "c")).setChildNodeId("childNodeId").setPeriod(10).setWindowSize(20).setUnit(TimeUnit.DAYS).setTemporalVariable("a").build();
    try (FluoClient fluoClient = FluoFactory.newClient(super.getFluoConfiguration())) {
        // Write it to the Fluo table.
        try (Transaction tx = fluoClient.newTransaction()) {
            dao.write(tx, originalMetadata);
            tx.commit();
        }
        // Read it from the Fluo table.
        PeriodicQueryMetadata storedMetadata = null;
        try (Snapshot sx = fluoClient.newSnapshot()) {
            storedMetadata = dao.readPeriodicQueryMetadata(sx, "nodeId");
        }
        // Ensure the deserialized object is the same as the serialized one.
        assertEquals(originalMetadata, storedMetadata);
    }
}
Also used : Snapshot(org.apache.fluo.api.client.Snapshot) FluoClient(org.apache.fluo.api.client.FluoClient) Transaction(org.apache.fluo.api.client.Transaction) VariableOrder(org.apache.rya.indexing.pcj.storage.accumulo.VariableOrder) Test(org.junit.Test)

Example 50 with VariableOrder

use of org.apache.rya.indexing.pcj.storage.accumulo.VariableOrder in project incubator-rya by apache.

the class FluoQueryMetadataDAOIT method aggregationMetadataTest_withGroupByVarOrders.

@Test
public void aggregationMetadataTest_withGroupByVarOrders() {
    final FluoQueryMetadataDAO dao = new FluoQueryMetadataDAO();
    // Create the object that will be serialized.
    final AggregationMetadata originalMetadata = AggregationMetadata.builder("nodeId").setVarOrder(new VariableOrder("totalCount")).setParentNodeId("parentNodeId").setChildNodeId("childNodeId").setGroupByVariableOrder(new VariableOrder("a", "b", "c")).addAggregation(new AggregationElement(AggregationType.COUNT, "count", "totalCount")).addAggregation(new AggregationElement(AggregationType.AVERAGE, "privae", "avgPrice")).build();
    try (FluoClient fluoClient = FluoFactory.newClient(super.getFluoConfiguration())) {
        // Write it to the Fluo table.
        try (Transaction tx = fluoClient.newTransaction()) {
            dao.write(tx, originalMetadata);
            tx.commit();
        }
        // Read it from the Fluo table.
        AggregationMetadata storedMetadata = null;
        try (Snapshot sx = fluoClient.newSnapshot()) {
            storedMetadata = dao.readAggregationMetadata(sx, "nodeId");
        }
    }
}
Also used : Snapshot(org.apache.fluo.api.client.Snapshot) AggregationElement(org.apache.rya.api.function.aggregation.AggregationElement) FluoClient(org.apache.fluo.api.client.FluoClient) Transaction(org.apache.fluo.api.client.Transaction) VariableOrder(org.apache.rya.indexing.pcj.storage.accumulo.VariableOrder) Test(org.junit.Test)

Aggregations

VariableOrder (org.apache.rya.indexing.pcj.storage.accumulo.VariableOrder)79 Test (org.junit.Test)47 HashSet (java.util.HashSet)18 MapBindingSet (org.openrdf.query.impl.MapBindingSet)18 VisibilityBindingSet (org.apache.rya.api.model.VisibilityBindingSet)17 PcjMetadata (org.apache.rya.indexing.pcj.storage.PcjMetadata)16 QueryBindingSet (org.openrdf.query.algebra.evaluation.QueryBindingSet)15 Bytes (org.apache.fluo.api.data.Bytes)14 BindingSet (org.openrdf.query.BindingSet)14 Column (org.apache.fluo.api.data.Column)13 FluoClient (org.apache.fluo.api.client.FluoClient)12 URIImpl (org.openrdf.model.impl.URIImpl)12 Transaction (org.apache.fluo.api.client.Transaction)11 Snapshot (org.apache.fluo.api.client.Snapshot)10 ShiftVarOrderFactory (org.apache.rya.indexing.pcj.storage.accumulo.ShiftVarOrderFactory)10 AccumuloPcjSerializer (org.apache.rya.indexing.pcj.storage.accumulo.AccumuloPcjSerializer)9 PrecomputedJoinStorage (org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage)8 Connector (org.apache.accumulo.core.client.Connector)5 AccumuloPcjStorage (org.apache.rya.indexing.pcj.storage.accumulo.AccumuloPcjStorage)5 RowColumn (org.apache.fluo.api.data.RowColumn)4