1、構建圖譜schema,流程包括圖創建、實體構建以及關系構建。
? ? ? 創建圖時需要指定圖庫名稱以及主鍵字段。
???????實體構建時需要指定主鍵字段,每個屬性需要指定數據類型,是否非空以及默認值。關系構建時需要包括關系名稱、指向頭實體的標簽,指向尾實體的標簽等字段。
Java代碼展示:
import com.gridgraph.driver.GridGraphAuthenticationException;
import com.gridgraph.driver.GridGraphSecureCluster;
import org.apache.tinkerpop.gremlin.driver.Client;
public class AddSchema {public static void main(String[] args) throws Exception {try{String address = "localhost";int port = 9999;String username = "";String password = "";String database = "實體1";GridGraphSecureCluster cluster = new GridGraphSecureCluster(address, port, username, password);Client client = cluster.connect(true);StringBuilder sb = new StringBuilder();sb.append(String.format("graph=GridGraphFactory.createGraph('%s');graph.createPrimaryKey('id');", database));sb.append("equipSchema = graph.createSchema('實體1', SchemaType.VERTEX);");sb.append("equipSchema.createProperty('屬性1', GridDataType.STRING, false, false, null);");sb.append("equipSchema.createProperty('屬性2', GridDataType.STRING, false, false, null);");sb.append("manuSchema = graph.createSchema('實體2', SchemaType.VERTEX);");sb.append("manuSchema.createProperty('屬性1', GridDataType.STRING, false, false, null);");sb.append("manuSchema.createProperty('屬性2', GridDataType.STRING, false, false, null);");sb.append("subSchema = graph.createSchema('實體3', SchemaType.VERTEX);");sb.append("subSchema.createProperty('屬性1', GridDataType.STRING, false, false, null);");sb.append("subSchema.createProperty('屬性2', GridDataType.STRING, false, false, null);");sb.append("relSchema = graph.createSchema('關系', SchemaType.EDGE);");sb.append("relSchema.createProperty('h_table', GridDataType.STRING, false, false, null);");sb.append("relSchema.createProperty('t_table', GridDataType.STRING, false, false, null);");sb.append("relSchema.createProperty('r', GridDataType.STRING, false, false, null);");client.submit(sb.toString());client.close();} catch (GridGraphAuthenticationException e) {throw new RuntimeException(e);}}
}
Python代碼展示:
#encoding=utf8
from gremlin_python.driver import clientif __name__ == '__main__':client = client.Client('ws://localhost:9999/gremlin', None, username='', password='')database = "實體1"prefix = f"graph=GridGraphFactory.createGraph('{database}');graph.createPrimaryKey('id');" client.submit(prefix +f'''equipSchema = graph.createSchema('實體1', SchemaType.VERTEX);equipSchema.createProperty('屬性1', GridDataType.STRING, false, false, null);equipSchema.createProperty('屬性2', GridDataType.STRING, false, false, null);manuSchema = graph.createSchema('實體2', SchemaType.VERTEX);manuSchema.createProperty('屬性1', GridDataType.STRING, false, false, null);manuSchema.createProperty('屬性2', GridDataType.STRING, false, false, null);subSchema = graph.createSchema('實體3', SchemaType.VERTEX);subSchema.createProperty('屬性1', GridDataType.STRING, false, false, null);subSchema.createProperty('屬性2', GridDataType.STRING, false, false, null);relSchema = graph.createSchema('關系', SchemaType.EDGE)relSchema.createProperty('h_table', GridDataType.STRING, false, false, null)relSchema.createProperty('t_table', GridDataType.STRING, false, false, null)relSchema.createProperty('r', GridDataType.STRING, false, false, null)''')client.close()
2、填入數據,添加實體時,需要指定實體主鍵、屬性以及對應的屬性值,添加關系時,需要指定頭實體、尾實體以及關系名,最終形成知識圖譜。
Java代碼展示:
import com.gridgraph.driver.GridGraphAuthenticationException;
import com.gridgraph.driver.GridGraphSecureCluster;
import org.apache.tinkerpop.gremlin.driver.Client;
public class AddData {public static void main(String[] args) throws Exception {try{String address = "localhost";int port = 9999;String username = "";String password = "";String database = "實體1";GridGraphSecureCluster cluster = new GridGraphSecureCluster(address, port, username, password);Client client = cluster.connect(true);StringBuilder sb = new StringBuilder();sb.append(String.format("graph=GridGraphFactory.openGraph('%s');", database));sb.append("e1 = graph.addVertex(T.label, '實體1', 'id', '1', '屬性1', '1', '屬性2', '2');");sb.append("e2 = graph.addVertex(T.label, '實體2', 'id', '2', '屬性1', '1', '屬性2', '2');");sb.append("e3 = graph.addVertex(T.label, '實體3', 'id', '3', '屬性1', '1', '屬性2', '2');");sb.append("e1.addEdge('關系', e2, 'h_table', '實體1', 't_table', '實體3', 'r', 'r1');");sb.append("e1.addEdge('關系', e3, 'h_table', '實體1', 't_table', '實體3', 'r', 'r2');");sb.append("graph.tx().commit();");client.submit(sb.toString());client.close();} catch (GridGraphAuthenticationException e) {throw new RuntimeException(e);}}
Python代碼展示:
#encoding=utf8
from gremlin_python.driver import client
if __name__ == '__main__':client = client.Client('ws://localhost:9999/gremlin', None, username='', password='')database = "實體1"prefix = f"graph=GridGraphFactory.openGraph('{database}');g=graph.traversal();"client.submit(prefix +f'''e1 = graph.addVertex(T.label, '實體1', 'id', '1', '屬性1', '1', '屬性2', '2') e2 = graph.addVertex(T.label, '實體2', 'id', '2', '屬性1', '1', '屬性2', '2')e3 = graph.addVertex(T.label, '實體3', 'id', '3', '屬性1', '1', '屬性2', '2')e1.addEdge('關系', e2, 'h_table', '實體1', 't_table', '實體3', 'r', 'r1')e1.addEdge('關系', e3, 'h_table', '實體1', 't_table', '實體3', 'r', 'r2')graph.tx().commit();''')client.close()
3、打印schema結構
對于每一個實體,遍歷圖數據庫中所有的schema,同時遍歷每一個schema中的每一個屬性,生成“實體類型(屬性1,屬性2,屬性3)”的結構;
對于每一個關系,可以遍歷所有關系數據中的 頭標簽、關系名、尾標簽,對其進行去重,生成“頭標簽--[r:關系名]-->尾標簽”的結構。
Java代碼展示:
import com.gridgraph.driver.GridGraphAuthenticationException;
import com.gridgraph.driver.GridGraphSecureCluster;
import org.apache.tinkerpop.gremlin.driver.Client;
import org.apache.tinkerpop.gremlin.driver.Result;
import org.apache.tinkerpop.gremlin.driver.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class SchemaPrompt {public static void main(String[] args) throws Exception {try{String address = "localhost";int port = 9999;String username = "";String password = "";String database = "實體1";GridGraphSecureCluster cluster = new GridGraphSecureCluster(address, port, username, password);Client client = cluster.connect(true);client.submit(String.format("graph=GridGraphFactory.openGraph('%s');g=graph.traversal();return;", database));ResultSet resultSet = client.submit("return graph.schemas().stream().map{s -> s.getName()};");StringBuilder entity_str = new StringBuilder("實體有:");StringBuilder rel_str = new StringBuilder("關系有:");List<String> resultList = resultSet.all().get().stream().map(Result::getString).collect(Collectors.toList());List<String> entities = new ArrayList<>();List<String> rels = new ArrayList<>();for(String table:resultList){ResultSet typeSet = client.submit(String.format("return graph.getSchema('%s').getType().toString();", table));List<String> typeList = typeSet.all().get().stream().map(Result::getString).collect(Collectors.toList());if(typeList.get(0).equals("VERTEX")){ //實體類型StringBuilder entity = new StringBuilder();entity.append(table);ResultSet prosSet = client.submit(String.format("return graph.getSchema('%s').getProperties().stream().map{s -> s.getName()};", table));List<String> prosList = prosSet.all().get().stream().map(Result::getString).collect(Collectors.toList());entity.append("(");for(int i = 0; i < prosList.size(); i++) {String pro = prosList.get(i);if (pro.equals("_id"))continue;entity.append(pro);if(i < prosList.size() - 1)entity.append(',');}entity.append(")");entities.add(entity.toString());}else if(typeList.get(0).equals("EDGE")){ResultSet relationSet = client.submit(String.format("return g.E().hasLabel('%s').valueMap('h_table', 'r', 't_table').dedup();", table));List<Result> relationList = relationSet.all().get();for(Result rel:relationList){Map<String, String> a_rel = rel.get(Map.class);String h_table = a_rel.get("h_table");String t_table = a_rel.get("t_table");String r = a_rel.get("r");rels.add(String.format("%s--[r:%s]->%s", h_table, r, t_table));}}}entity_str.append(String.join(",", entities));rel_str.append(String.join(",", rels));System.out.println(entity_str);System.out.println(rel_str);
client.close();} catch (GridGraphAuthenticationException e) {throw new RuntimeException(e);}}
}
Python代碼展示:
#encoding=utf8
from gremlin_python.driver import client
if __name__ == '__main__':client = client.Client('ws://localhost:9999/gremlin', None, username='', password='')database = "實體1"prefix = f'graph=GridGraphFactory.openGraph("{database}");g=graph.traversal();'tables = client.submit(prefix + "graph.schemas().stream().map{s -> s.getName()};").all().result()entities, rels = [], []for table in tables:tp = client.submit(prefix + f"graph.getSchema('{table}').getType().toString();").one()[0]if tp == 'VERTEX': #實體類型pros = client.submit(prefix + f"graph.getSchema('{table}')" + ".getProperties().stream().map{s -> s.getName()};").all().result()pros = [pro for pro in pros if pro != '_id']entities.append(f"{table}({','.join(pros)})")elif tp == 'EDGE':rs = client.submit(prefix + f"g.E().hasLabel('{table}').valueMap('h_table', 'r', 't_table').dedup();").all().result()for mp in rs:rels.append(f"{mp['h_table']}--[r:{mp['r']}]-->{mp['t_table']}")
''' 也可以用:g.E().hasLabel('關系').toList().stream().map{e -> e.outVertex().schema().getName()+"--[r:"+e.values('r').next()+ "]-->" + e.inVertex().schema().getName()}.distinct()
'''print("實體有:" + ",".join(entities))print("關系有:" + ",".join(rels))client.close()