Commit 1d1531ef by Harish Butani

add support for raw search

parent ea38da10
......@@ -24,6 +24,7 @@ import org.apache.hadoop.metadata.service.Service;
import org.apache.hadoop.metadata.storage.RepositoryException;
import java.util.List;
import java.util.Map;
/**
* An interface for persisting metadata into a blueprints enabled graph db.
......@@ -36,4 +37,14 @@ public interface MetadataRepository extends Service {
ITypedReferenceableInstance getEntityDefinition(String guid) throws RepositoryException;
List<String> getEntityList(String entityType) throws RepositoryException;
/**
* Assumes the User is familar with the persistence structure of the Repository.
* The given query is run uninterpreted against the underlying Graph Store.
* The results are returned as a List of Rows. each row is a Map of Key,Value pairs.
* @param gremlinQuery
* @return
* @throws RepositoryException
*/
List<Map<String,String>> rawSearch(String gremlinQuery) throws RepositoryException;
}
......@@ -25,6 +25,7 @@ import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Graph;
import com.tinkerpop.blueprints.TransactionalGraph;
import com.tinkerpop.blueprints.Vertex;
import org.apache.commons.collections.map.HashedMap;
import org.apache.hadoop.metadata.IReferenceableInstance;
import org.apache.hadoop.metadata.ITypedInstance;
import org.apache.hadoop.metadata.ITypedReferenceableInstance;
......@@ -47,6 +48,10 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.inject.Inject;
import javax.script.Bindings;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
......@@ -153,6 +158,55 @@ public class GraphBackedMetadataRepository implements MetadataRepository {
}
}
public List<Map<String,String>> rawSearch(String gremlinQuery) throws RepositoryException {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("gremlin-groovy");
Bindings bindings = engine.createBindings();
bindings.put("g", graphService.getTransactionalGraph());
try {
Object o = engine.eval(gremlinQuery, bindings);
if ( !(o instanceof List)) {
throw new RepositoryException(String.format("Cannot process gremlin result %s", o.toString()));
}
List l = (List) o;
List<Map<String,String>> result = new ArrayList<>();
for(Object r : l) {
Map<String,String> oRow = new HashedMap();
if ( r instanceof Map ) {
Map<Object,Object> iRow = (Map) r;
for(Map.Entry e : iRow.entrySet()) {
Object k = e.getKey();
Object v = e.getValue();
oRow.put(k.toString(), v.toString());
}
} else if ( r instanceof TitanVertex) {
Iterable<TitanProperty> ps = ((TitanVertex)r).getProperties();
for(TitanProperty tP : ps) {
String pName = tP.getPropertyKey().getName();
Object pValue = ((TitanVertex)r).getProperty(pName);
if ( pValue != null ) {
oRow.put(pName, pValue.toString());
}
}
} else if ( r instanceof String ) {
oRow.put("", r.toString());
} else {
throw new RepositoryException(String.format("Cannot process gremlin result %s", o.toString()));
}
result.add(oRow);
}
return result;
}catch(ScriptException se) {
throw new RepositoryException(se);
}
}
@Override
public List<String> getEntityList(String entityType) throws RepositoryException {
LOG.info("Retrieving entity list for type={}", entityType);
......
......@@ -103,6 +103,30 @@ public class GraphBackedMetadataRepositoryTest extends RepositoryModuleBaseTest
Assert.assertEquals(entityList.size(), 0); // as this is not implemented yet
}
@Test (enabled = false)
public void testRawSearch1() throws Exception {
Referenceable hrDept = createDeptEg1(ts);
ClassType deptType = ts.getDataType(ClassType.class, "Department");
ITypedReferenceableInstance hrDept2 = deptType.convert(hrDept, Multiplicity.REQUIRED);
guid = repositoryService.createEntity(hrDept2, ENTITY_TYPE);
// Query for all Vertices in Graph
Object r = repositoryService.rawSearch("g.V.toList()");
//System.out.println(r);
// Query for all Vertices of a Type
r = repositoryService.rawSearch("g.V.filter{it.typeName == 'Department'}.toList()");
//System.out.println(r);
// Property Query: list all Person names
r = repositoryService.rawSearch("g.V.filter{it.typeName == 'Person'}.'Person.name'.toList()");
//System.out.println(r);
}
/*
* Class Hierarchy is:
* Department(name : String, employees : Array[Person])
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment