TIBCO® Graph Database - Building Applications
By:
Last updated:
6:11pm Aug 05, 2018

 

TIBCO® Graph Database - Building Applications

Javadocs

Sample Application

​/**
 * Copyright 2016 TIBCO Software Inc. All rights reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); You may not use this file except 
 * in compliance with the License.
 * A copy of the License is included in the distribution package with this file.
 * You also may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

import com.tibco.tgdb.connection.TGConnection;
import com.tibco.tgdb.connection.TGConnectionFactory;
import com.tibco.tgdb.log.TGLogManager;
import com.tibco.tgdb.log.TGLogger;
import com.tibco.tgdb.model.TGEdge;
import com.tibco.tgdb.model.TGEntity;
import com.tibco.tgdb.model.TGGraphMetadata;
import com.tibco.tgdb.model.TGGraphObjectFactory;
import com.tibco.tgdb.model.TGKey;
import com.tibco.tgdb.model.TGNode;
import com.tibco.tgdb.model.TGNodeType;

public class CreateAndUpdate {
    public String url = "tcp://scott@localhost:8222";
    public String passwd = "scott";
    public TGLogger.TGLevel logLevel = TGLogger.TGLevel.Info;
    
    void run() throws Exception {
    	System.out.printf("Using url : %s, password : %s, log level : %s\n", url, passwd, logLevel.toString());
    	TGLogger logger = TGLogManager.getInstance().getLogger();
    	logger.setLevel(logLevel);

        TGConnection conn = TGConnectionFactory.getInstance().createConnection(url, null, passwd, null);
        conn.connect();

        TGGraphObjectFactory gof = conn.getGraphObjectFactory();
        TGGraphMetadata gmd = conn.getGraphMetadata(true);
       	TGNodeType testNodeType = gmd.getNodeType("testnode");

      	System.out.println("Start transaction 1");
      	System.out.println("Create node1");
        TGNode node1 = gof.createNode(testNodeType);
        node1.setAttribute("name", "john doe");
        node1.setAttribute("rate", 3.3);
        node1.setAttribute("nickname", "fast");
        node1.setAttribute("thing1", true);
        conn.insertEntity(node1);

      	System.out.println("Create node2");
        TGNode node2 = gof.createNode(testNodeType);
        node2.setAttribute("name", "julie");
        node2.setAttribute("factor", 3.3);
        node2.setAttribute("thing2", false);
        conn.insertEntity(node2);

      	System.out.println("Create node3");
        TGNode node3 = gof.createNode(testNodeType);
        node3.setAttribute("name", "margo");
        node3.setAttribute("factor", 2.3);
        node3.setAttribute("nickname", "quick");
        conn.insertEntity(node3);

      	System.out.println("Create edge1");
        TGEdge edge1 = gof.createEdge(node1, node2, TGEdge.DirectionType.BiDirectional);
        edge1.setAttribute("name", "spouse");
        conn.insertEntity(edge1);

      	System.out.println("Create edge2");
        TGEdge edge2 = gof.createEdge(node1, node3, TGEdge.DirectionType.Directed);
        edge2.setAttribute("name", "daughter");
        conn.insertEntity(edge2);

      	System.out.println("Commit transaction 1");
        conn.commit(); //----- write data to database ----------. Everything is create

      	System.out.println("Commit transaction 1 completed");

      	System.out.println("Start transaction 2");
        // updates
      	System.out.println("Update node1");
        node1.setAttribute("age", 40);
        conn.updateEntity(node1);
        
      	System.out.println("Delete edge1");
        conn.deleteEntity(edge1);
        
      	System.out.println("Update edge2");
        //add a new attribute
        edge2.setAttribute("extra", true);
        //update existing one
        edge2.setAttribute("name", "kid");
        conn.updateEntity(edge2);
        
      	System.out.println("Create node4");
        TGNode node4 = gof.createNode(testNodeType);
        node4.setAttribute("name", "McEnroe");
        node4.setAttribute("level", 3.0);
        conn.insertEntity(node4);

      	System.out.println("Create edge3");
        TGEdge edge3 = gof.createEdge(node1, node4, TGEdge.DirectionType.BiDirectional);
        edge3.setAttribute("name", "Tennis Partner");
        conn.insertEntity(edge3);

      	System.out.println("Commit transaction 2");
        conn.commit(); // update John doe's value --------.
      	System.out.println("Commit transaction 2 completed");

        //	conn.getGraphMetadata(true);
        System.out.println("Start unique get operation");
        gmd = conn.getGraphMetadata(true);
        TGKey key = gof.createCompositeKey("testnode");
        key.setAttribute("name", "margo");
        TGEntity ent = conn.getEntity(key, null);
        if (ent != null) {
        	System.out.printf("name : %s\n", ent.getAttribute("name").getValue());
        	for (TGEdge edge : ((TGNode) ent).getEdges()) {
        		TGNode[] nodes = edge.getVertices();
        		for (int i=0; i<nodes.length; i++) {
        			if (ent.getAttribute("name") == nodes[i].getAttribute("name")) {
        				continue;
        			}
        			nodes[i].getAttribute("name");
        			System.out.printf("related node name : %s\n", nodes[i].getAttribute("name").getValue());
        		}
        	}
        } else {
            System.out.println("getEntity for 'margo' returns nothing");
        }
        System.out.println("End unique get operation");

        conn.disconnect();
        System.out.println("Connection disconnected.");
    }

    public static void main(String[] args) throws Exception {
    	CreateAndUpdate cau = new CreateAndUpdate();

    	cau.run();
    }
}
​

To compile : javac -cp <tgdbdir>/lib/tgdb-client.jar CreateAndUpdate.java

To run : java -cp <tgdbdir>/lib/tgdb-client.jar:./ CreateAndUpdate

Attachments

AttachmentSize
Binary Data javadocs.tar.gz65.13 KB