use of org.apache.rya.api.RdfCloudTripleStoreConstants.TABLE_LAYOUT in project incubator-rya by apache.
the class StatementPatternStorage method addInferredRanges.
protected void addInferredRanges(String tablePrefix, Job job) throws IOException {
logger.info("Adding inferences to statement pattern[subject:" + subject_value + ", predicate:" + predicate_value + ", object:" + object_value + "]");
// inference engine
AccumuloRyaDAO ryaDAO = new AccumuloRyaDAO();
InferenceEngine inferenceEngine = new InferenceEngine();
try {
AccumuloRdfConfiguration rdfConf = new AccumuloRdfConfiguration(job.getConfiguration());
rdfConf.setTablePrefix(tablePrefix);
ryaDAO.setConf(rdfConf);
try {
if (!mock) {
ryaDAO.setConnector(new ZooKeeperInstance(inst, zookeepers).getConnector(user, userP.getBytes(StandardCharsets.UTF_8)));
} else {
ryaDAO.setConnector(new MockInstance(inst).getConnector(user, userP.getBytes(StandardCharsets.UTF_8)));
}
} catch (Exception e) {
throw new IOException(e);
}
ryaDAO.init();
inferenceEngine.setConf(rdfConf);
inferenceEngine.setRyaDAO(ryaDAO);
inferenceEngine.setSchedule(false);
inferenceEngine.init();
// is it subclassof or subpropertyof
if (RDF.TYPE.equals(predicate_value)) {
// try subclassof
Collection<URI> parents = inferenceEngine.findParents(inferenceEngine.getSubClassOfGraph(), (URI) object_value);
if (parents != null && parents.size() > 0) {
// add all relationships
for (URI parent : parents) {
Map.Entry<TABLE_LAYOUT, Range> temp = createRange(subject_value, predicate_value, parent);
Range range = temp.getValue();
if (logger.isDebugEnabled()) {
logger.debug("Found subClassOf relationship [type:" + object_value + " is subClassOf:" + parent + "]");
}
addRange(range);
}
}
} else if (predicate_value != null) {
// subpropertyof check
Set<URI> parents = inferenceEngine.findParents(inferenceEngine.getSubPropertyOfGraph(), (URI) predicate_value);
for (URI parent : parents) {
Map.Entry<TABLE_LAYOUT, Range> temp = createRange(subject_value, parent, object_value);
Range range = temp.getValue();
if (logger.isDebugEnabled()) {
logger.debug("Found subPropertyOf relationship [type:" + predicate_value + " is subPropertyOf:" + parent + "]");
}
addRange(range);
}
}
} catch (Exception e) {
logger.error("Exception in adding inferred ranges", e);
throw new IOException(e);
} finally {
if (inferenceEngine != null) {
try {
inferenceEngine.destroy();
} catch (InferenceEngineException e) {
logger.error("Exception closing InferenceEngine", e);
}
}
if (ryaDAO != null) {
try {
ryaDAO.destroy();
} catch (RyaDAOException e) {
logger.error("Exception closing ryadao", e);
}
}
}
}
use of org.apache.rya.api.RdfCloudTripleStoreConstants.TABLE_LAYOUT in project incubator-rya by apache.
the class JoinSelectStatisticsTest method testMap1.
@Test
public void testMap1() throws Exception {
init();
System.out.println("*****************************Test1**************************** ");
BatchWriter bw_table1 = c.createBatchWriter("rya_spo", new BatchWriterConfig());
for (int i = 1; i < 3; i++) {
RyaStatement rs = new RyaStatement(new RyaURI(uri + i), new RyaURI(uri + 5), new RyaType(uri + (i + 2)));
Map<TABLE_LAYOUT, TripleRow> tripleRowMap = ryaContext.serializeTriple(rs);
TripleRow tripleRow = tripleRowMap.get(TABLE_LAYOUT.SPO);
Mutation m = JoinSelectStatsUtil.createMutation(tripleRow);
bw_table1.addMutation(m);
}
bw_table1.close();
BatchWriter bw_table2 = c.createBatchWriter("rya_prospects", new BatchWriterConfig());
for (int i = 1; i < 6; i++) {
int j = 1;
for (String s : cardList) {
Mutation m = new Mutation(new Text(s + DELIM + uri + i + DELIM + i));
m.put(new Text(), new Text(), new Value(new IntWritable(i + j).toString().getBytes()));
bw_table2.addMutation(m);
j++;
}
}
bw_table2.close();
Assert.assertEquals(0, ToolRunner.run(new JoinSelectTestDriver(), new String[] { "" }));
Scanner scan = c.createScanner("rya_selectivity", new Authorizations());
scan.setRange(new Range());
for (Map.Entry<Key, Value> entry : scan) {
System.out.println("Key row string is " + entry.getKey().getRow().toString());
System.out.println("Join type is " + entry.getKey().getColumnFamily().toString());
System.out.println("Value is " + entry.getKey().getColumnQualifier().toString());
}
Scanner scan1 = c.createScanner("rya_selectivity", new Authorizations());
scan1.setRange(Range.prefix("predicate" + DELIM + uri + 5));
int i = 5;
for (Map.Entry<Key, Value> entry : scan1) {
int val1 = 5 + 2 * i;
int val2 = 5 + 2 * (i - 1);
int val = Integer.parseInt(entry.getKey().getColumnQualifier().toString());
if (i < 3) {
Assert.assertTrue(val == val1);
}
if (i >= 3 && i < 6) {
Assert.assertTrue(val == val2);
}
i--;
}
Assert.assertTrue(i == -1);
Scanner scan2 = c.createScanner("rya_selectivity", new Authorizations());
scan2.setRange(Range.prefix("object" + DELIM + uri + 3));
int j = 5;
for (Map.Entry<Key, Value> entry : scan2) {
int val1 = 5 + (j - 2);
int val2 = 2 + j;
int val = Integer.parseInt(entry.getKey().getColumnQualifier().toString());
if (j < 3) {
Assert.assertTrue(val == val2);
}
if (j >= 3 && j < 6) {
Assert.assertTrue(val == val1);
}
j--;
}
Assert.assertTrue(j == -1);
Scanner scan3 = c.createScanner("rya_selectivity", new Authorizations());
scan3.setRange(Range.prefix("objectsubject" + DELIM + uri + 3 + DELIM + uri + 1));
int k = 8;
for (Map.Entry<Key, Value> entry : scan3) {
int val = Integer.parseInt(entry.getKey().getColumnQualifier().toString());
Assert.assertTrue(val == k);
k--;
}
Assert.assertTrue(k == 5);
}
use of org.apache.rya.api.RdfCloudTripleStoreConstants.TABLE_LAYOUT in project incubator-rya by apache.
the class JoinSelectStatisticsTest method testMap2.
@Test
public void testMap2() throws Exception {
System.out.println("*********************Test2******************* ");
init();
BatchWriter bw_table1 = c.createBatchWriter("rya_spo", new BatchWriterConfig());
for (int i = 1; i < 4; i++) {
RyaStatement rs = new RyaStatement(new RyaURI(uri + 1), new RyaURI(uri + 2), new RyaType(uri + i));
Map<TABLE_LAYOUT, TripleRow> tripleRowMap = ryaContext.serializeTriple(rs);
TripleRow tripleRow = tripleRowMap.get(TABLE_LAYOUT.SPO);
Mutation m = JoinSelectStatsUtil.createMutation(tripleRow);
bw_table1.addMutation(m);
}
bw_table1.close();
BatchWriter bw_table2 = c.createBatchWriter("rya_prospects", new BatchWriterConfig());
for (int i = 1; i < 4; i++) {
for (String s : cardList) {
Mutation m = new Mutation(new Text(s + DELIM + uri + i + DELIM + i));
m.put(new Text(), new Text(), new Value(new IntWritable(i + 2).toString().getBytes()));
bw_table2.addMutation(m);
}
}
bw_table2.close();
Assert.assertEquals(0, ToolRunner.run(new JoinSelectTestDriver(), new String[] { "" }));
Scanner scan1 = c.createScanner("rya_selectivity", new Authorizations());
scan1.setRange(Range.prefix("subject" + DELIM + uri + 1));
int i = 0;
for (Map.Entry<Key, Value> entry : scan1) {
Assert.assertTrue(entry.getKey().getColumnQualifier().toString().equals("12"));
i++;
}
Assert.assertTrue(i == 6);
Scanner scan2 = c.createScanner("rya_selectivity", new Authorizations());
scan2.setRange(Range.prefix("predicate" + DELIM + uri + 2));
int j = 0;
for (Map.Entry<Key, Value> entry : scan2) {
if (j < 3) {
Assert.assertTrue(entry.getKey().getColumnQualifier().toString().equals("12"));
}
if (j > 3 && j < 6) {
Assert.assertTrue(entry.getKey().getColumnQualifier().toString().equals("9"));
}
j++;
}
Assert.assertTrue(j == 6);
Scanner scan3 = c.createScanner("rya_selectivity", new Authorizations());
scan3.setRange(Range.prefix("predicateobject" + DELIM + uri + 2 + DELIM + uri + 2));
int k = 0;
for (Map.Entry<Key, Value> entry : scan3) {
Assert.assertTrue(entry.getKey().getColumnQualifier().toString().equals("3"));
k++;
}
Assert.assertTrue(k == 3);
}
use of org.apache.rya.api.RdfCloudTripleStoreConstants.TABLE_LAYOUT in project incubator-rya by apache.
the class JoinSelectStatisticsTest method testMap4.
@Test
public void testMap4() throws Exception {
init();
System.out.println("*************************Test4**************************** ");
System.out.println("*************************Test4**************************** ");
BatchWriter bw_table1 = c.createBatchWriter("rya_spo", new BatchWriterConfig());
for (int i = 1; i < 3; i++) {
for (int j = 1; j < 3; j++) {
for (int k = 1; k < 3; k++) {
if (j == 1 && k == 2) {
break;
}
RyaStatement rs = new RyaStatement(new RyaURI(uri + i), new RyaURI(uri + (j)), new RyaType(uri + k));
Map<TABLE_LAYOUT, TripleRow> tripleRowMap = ryaContext.serializeTriple(rs);
TripleRow tripleRow = tripleRowMap.get(TABLE_LAYOUT.SPO);
Mutation m = JoinSelectStatsUtil.createMutation(tripleRow);
bw_table1.addMutation(m);
}
}
}
bw_table1.close();
BatchWriter bw_table2 = c.createBatchWriter("rya_prospects", new BatchWriterConfig());
for (int i = 1; i < 3; i++) {
int k = 1;
for (String s : cardList) {
Mutation m = new Mutation(new Text(s + DELIM + uri + i + DELIM + i));
m.put(new Text(), new Text(), new Value(new IntWritable(i + k).toString().getBytes()));
bw_table2.addMutation(m);
k++;
}
for (int j = 1; j < 3; j++) {
k = 1;
for (String s : aggCardList) {
Mutation m = new Mutation(new Text(s + DELIM + uri + i + DELIM + uri + j + DELIM + i));
m.put(new Text(), new Text(), new Value(new IntWritable(i + k + 2 * j).toString().getBytes()));
bw_table2.addMutation(m);
k++;
}
}
}
bw_table2.close();
Assert.assertEquals(0, ToolRunner.run(new JoinSelectTestDriver(), new String[] { "" }));
Scanner scan = c.createScanner("rya_selectivity", new Authorizations());
scan.setRange(new Range());
for (Map.Entry<Key, Value> entry : scan) {
System.out.println("Key row string is " + entry.getKey().getRow().toString());
System.out.println("Join type is " + entry.getKey().getColumnFamily().toString());
System.out.println("Value is " + entry.getKey().getColumnQualifier().toString());
}
Scanner scan1 = c.createScanner("rya_selectivity", new Authorizations());
scan1.setRange(Range.prefix("subject" + DELIM + uri + 1));
int i = 0;
for (Map.Entry<Key, Value> entry : scan1) {
Key key = entry.getKey();
String s = key.getColumnFamily().toString();
int val = Integer.parseInt(key.getColumnQualifier().toString());
if (s.equals("predicatepredicate")) {
Assert.assertTrue(val == 11);
}
if (s.equals("objectobject")) {
Assert.assertTrue(val == 13);
}
if (s.equals("predicateobjectobjectpredicate")) {
Assert.assertTrue(val == 26);
}
if (s.equals("predicateobjectpredicateobject")) {
Assert.assertTrue(val == 25);
}
if (s.equals("predicateobjectsubjectpredicate")) {
Assert.assertTrue(val == 19);
}
if (s.equals("predicateobjectpredicatesubject")) {
Assert.assertTrue(val == 20);
}
i++;
}
Assert.assertTrue(i == 12);
Scanner scan2 = c.createScanner("rya_selectivity", new Authorizations());
scan2.setRange(Range.prefix("predicate" + DELIM + uri + 1));
int j = 0;
for (Map.Entry<Key, Value> entry : scan2) {
Key key = entry.getKey();
String s = key.getColumnFamily().toString();
int val = Integer.parseInt(key.getColumnQualifier().toString());
if (s.equals("subjectsubject")) {
Assert.assertTrue(val == 5);
}
if (s.equals("objectobject")) {
Assert.assertTrue(val == 8);
}
if (s.equals("objectsubjectsubjectpredicate")) {
Assert.assertTrue(val == 11);
}
if (s.equals("objectsubjectpredicateobject")) {
Assert.assertTrue(val == 15);
}
if (s.equals("objectsubjectobjectsubject")) {
Assert.assertTrue(val == 9);
}
if (s.equals("objectsubjectsubjectobject")) {
Assert.assertTrue(val == 10);
}
j++;
}
Assert.assertTrue(j == 12);
}
use of org.apache.rya.api.RdfCloudTripleStoreConstants.TABLE_LAYOUT in project incubator-rya by apache.
the class RyaContextTest method testHashedTripleRowSerialization.
public void testHashedTripleRowSerialization() throws Exception {
RyaURI subj = new RyaURI("urn:test#subj");
RyaURI pred = new RyaURI("urn:test#pred");
RyaType obj = new RyaType("mydata");
RyaStatement statement = new RyaStatement(subj, pred, obj);
MockRdfCloudConfiguration config = new MockRdfCloudConfiguration();
config.set(MockRdfCloudConfiguration.CONF_PREFIX_ROW_WITH_HASH, Boolean.TRUE.toString());
RyaTripleContext instance = RyaTripleContext.getInstance(config);
Map<TABLE_LAYOUT, TripleRow> map = instance.serializeTriple(statement);
TripleRow tripleRow = map.get(TABLE_LAYOUT.SPO);
assertEquals(statement, instance.deserializeTriple(TABLE_LAYOUT.SPO, tripleRow));
}
Aggregations