Using Python to Operate TigerGraph
To operate the TigerGraph database using Python, you need to use the TigerGraph library, which provides a Python client to connect and interact with the TigerGraph database. The following is a complete example code that demonstrates how to use Python to connect to a TigerGraph database and perform data insertion, querying, modification, and deletion.
Firstly, you need to use the 'pip install pyTigerGraph' command to install the tigerGraph library.
python
from pyTigerGraph.pyTigerGraph import TigerGraphConnection
#1 Create a connection object to the TigerGraph database
conn = TigerGraphConnection(host="your_tigergraph_host", graphname="your_graph_name", username="your_username", password="your_password")
#2 Insert Data Example
conn.upsertVertex("person", {"name": "John", "age": 30, "gender": "male"})
conn.upsertVertex("person", {"name": "Jane", "age": 28, "gender": "female"})
#3 Query Data Example
query = conn.runInstalledQuery("getPersons")
print(query[0]["Answer"])
#4 Example of modifying data
conn.updateVertex("person", "name", "John", {"age": 32})
#5 Delete Data Example
conn.deleteVertex("person", "name", "Jane")
#6 Close the connection to the TigerGraph database
conn.close()
In the above example code:
-Step 1, we created a TigerGraphConnection object and passed in the host address, graph name, username, and password of the TigerGraph database.
-Step 2, use the 'upsertVertex' function to insert a vertex named 'person' into the database.
-Step 3, we use the 'runInstalledQuery' function to run an installed query and print the query results.
-Step 4: Use the 'updateVertex' function to modify the attribute values of existing vertices.
-Step 5, use the 'deleteVertex' function to delete existing vertices.
-Step 6, use the 'close' function to close the connection to the TigerGraph database.
Please note that in the above code example, "person" refers to a vertex type in a TigerGraph graph, and you need to adjust these parameters according to your graph structure.
In addition, you can learn about other methods and functions provided by the tigergraph library by querying the documentation to meet your specific needs.