import tgdb.connection as tgconn # Connection URL \/ Username \/ \/ Password
conn = tgconn.TGConnectionFactory.createConnection("ssl://localhost:8223/{dbName=demodb}", "scott", "scott")
# Recommended to have connect/disconnect in try/finally block
try:
conn.connect() # Connect to the database
testNode1 = conn.graphObjectFactory.createNode("ratenode") # Create a node
testNode1.setAttribute("name", "testNode1") # Java API Syntax
testNode1["age"] = 15 # Python-y Syntax
conn.insertEntity(testNode1) # Inserts the node for next commit
testNode2 = conn.graphObjectFactory.createNode("ratenode") # Create another node
testNode2["name"] = "testNode2"
testNode2["age"] = 16
conn.insertEntity(testNode2)
testEdge = conn.graphObjectFactory.createEdge(testNode1,
testNode2) # Create directed edge from testNode1 to testNode2
testEdge["rate"] = 2
conn.insertEntity(testEdge)
conn.commit() # Commits all changes to the server
query = "gremlin://g.V().haslabel('ratenode').out();" # Query result should be testNode2
results = conn.executeQuery(query)
print(len(results)) # How to get number of results in a ResultSet
num = 0
for result in results: # Recommended to iterate through a ResultSet instead
num += 1 # of calling toCollection() because of amortization
print("Result num {0} has name {1} and age {2}. result == testNode2 is {3}"
.format(num, result["name"], result["age"], # result is expected to be a node due to the query
str(testNode2 == result))) # Can test for node equality
finally:
conn.disconnect()