TinkerPop:顶点添加Graph API与Traversal API对比疑问
Great question! It makes total sense to question the recommendation when your initial speed tests show the Graph API outperforming the Traversal API for a simple vertex addition. Let’s break down the key advantages of the Traversal API that make it the go-to choice for most graph operations, even with those initial timing numbers.
Key Advantages of the Traversal API
Portability Across Graph Databases
Traversal APIs like Gremlin are vendor-agnostic standards. If you ever need to switch your graph database (say, from Neo4j to JanusGraph or Amazon Neptune), your Traversal API code will require minimal changes. Graph APIs, by contrast, are database-specific—code written for one database’s Graph API won’t work with another, locking you into a single vendor.Expressive Power for Complex Operations
Your test focused on a simple single-vertex creation, where the Graph API’s lower-level access can skip some abstraction overhead. But for complex workflows—like adding a vertex and linking it to existing vertices based on specific conditions, or batch-processing hundreds of nodes with dynamic relationships—the Traversal API shines. You can encapsulate the entire logic in a single traversal, avoiding the need for multiple round-trip calls to the database (critical if you’re working with a remote graph instance).Example of a complex traversal that’s far simpler with Traversal API:
g.inject(['abc', 'def', 'ghi']) .addV('Test').property('title1', it) .addE('linked_to').to(V().has('Category', 'default'))Doing this with Graph API would require looping through each value, creating vertices, then separately creating edges—adding unnecessary complexity and overhead.
Built-In Transactional Consistency
Most Traversal API implementations handle transactions natively for multi-step operations. If part of your traversal fails (e.g., an edge can’t be created because the target vertex doesn’t exist), the entire operation rolls back automatically. With Graph API, you’d need to manually manage transaction boundaries, which increases the risk of partial failures leaving your graph in an inconsistent state.Reusability & Composability
Traversals are modular—you can build reusable traversal fragments (like a helper to create a vertex with standard properties) and combine them across different parts of your application. Graph API calls are typically one-off and harder to reuse, leading to duplicated code over time.
Why Your Simple Test Showed Graph API as Faster
For trivial operations like creating a single vertex, the Graph API’s direct, low-level access avoids the abstraction layer of the Traversal API. This means less processing overhead for the database to interpret the traversal, resulting in faster execution times. But as your operations grow in complexity, the Traversal API’s ability to batch logic, reduce network calls, and maintain consistency will far outweigh this small initial speed difference.
内容的提问来源于stack exchange,提问作者Srinath Ganesh

