Searching for a Member of the House
Once the graph is complete you can search for the given node and get its characteristics. In the exercise you will search for a member of the House and get his or her characteristics.
import java.text.SimpleDateFormat; import java.util.Calendar; import com.tibco.tgdb.connection.TGConnection; import com.tibco.tgdb.connection.TGConnectionFactory; import com.tibco.tgdb.model.TGAttribute; import com.tibco.tgdb.model.TGEdge; import com.tibco.tgdb.model.TGEntity; import com.tibco.tgdb.model.TGGraphObjectFactory; import com.tibco.tgdb.model.TGKey; import com.tibco.tgdb.model.TGNode; /** * Search for a member in the House of Bonaparte graph * and display the member attributes and children * * Usage : java SearchGraph -memberName "Carlo Bonaparte" * */ public class SearchGraph { static String url = "tcp://127.0.0.1:8222"; static String user = "napoleon"; static String pwd = "bonaparte"; static String memberName = "Napoleon Bonaparte"; public static void main(String[] args) throws Exception { for (int i=0; i<args.length; i++) { if (args[i].equals("-memberName")) memberName = args[i+1]; } TGConnection conn = null; try { conn = TGConnectionFactory.getInstance().createConnection(url, user, pwd, null); conn.connect(); TGGraphObjectFactory gof = conn.getGraphObjectFactory(); if (gof == null) { throw new Exception("Graph object not found"); } conn.getGraphMetadata(true); TGKey houseKey = gof.createCompositeKey("houseMemberType"); houseKey.setAttribute("memberName", memberName); System.out.printf("Searching for member '%s'...\n",memberName); TGEntity houseMember = conn.getEntity(houseKey, null); if (houseMember != null) { SimpleDateFormat simpleFormat = new SimpleDateFormat("dd MMM yyyy"); System.out.printf("House member '%s' found\n",houseMember.getAttribute("memberName").getAsString()); for (TGAttribute attr : houseMember.getAttributes()) { if (attr.getValue() == null) System.out.printf("\t%s: %s\n", attr.getAttributeType().getName(), ""); else System.out.printf("\t%s: %s\n", attr.getAttributeType().getName(), (attr.getValue() instanceof Calendar)?(simpleFormat.format(((Calendar)attr.getValue()).getTime())):attr.getValue()); } for (TGEdge relation : ((TGNode)houseMember).getEdges()) { if (relation.getVertices()[0].getAttribute("memberName") == houseMember.getAttribute("memberName") && relation.getAttribute("relType").getAsString().equals("child")) System.out.printf("\tchild: %s\n", relation.getVertices()[1].getAttribute("memberName").getAsString()); } } else { System.out.printf("House member '%s' not found", memberName); } } finally { if (conn != null) conn.disconnect(); } } }
Procedure
Copyright © 2020. Cloud Software Group, Inc. All Rights Reserved.