使用 .NET 連線至 Neptune 資料庫執行個體 - Amazon Neptune

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用 .NET 連線至 Neptune 資料庫執行個體

如果可以的話,請始終使用您的引擎版本支持的最新版本的 Apache TinkerPop NET Grim lin 客戶端。較新的版本包含許多錯誤修正,其可以改善用戶端的穩定性、效能和可用性。要使用的Gremlin.Net版 TinkerPop本通常會與 Java Gemlin 客戶端的表格中描述的版本保持一致。

下節包含以 C# 撰寫的程式碼範例,其會連線至 Neptune 資料庫執行個體並執行 Gremlin 周遊。

必須從與您的 Neptune 資料庫執行個體位於同一虛擬私有雲端 (VPC) 的 Amazon EC2 執行個體連線至 Amazon Neptune。此範本程式碼已在執行 Ubuntu 的 Amazon EC2 執行個體上測試。

開始之前,請執行以下動作:

  • 在 Amazon EC2 執行個體上安裝 .NET。如需在包括 Windows、Linux 和 macOS 等多重作業系統上安裝 .NET 的指示,請參閱開始使用 .NET

  • 請執行套件的 dotnet add package gremlin.net以安裝 Gremlin.NET。如需詳細資訊,請參閱說明文件中的 Gemlin.net。 TinkerPop

使用 Gremlin.NET 連線至 Neptune
  1. 建立新的 .NET 專案。

    dotnet new console -o gremlinExample
  2. 將目錄變更為新的專案目錄。

    cd gremlinExample
  3. 將以下內容複製到 Program.cs 檔案。將 your-neptune-endpoint 取代為 Neptune 資料庫執行個體的地址。

    如需尋找 Neptune 資料庫執行個體地址的相關資訊,請參閱 連線至 Amazon Neptune 端點 一節。

    using System; using System.Threading.Tasks; using System.Collections.Generic; using Gremlin.Net; using Gremlin.Net.Driver; using Gremlin.Net.Driver.Remote; using Gremlin.Net.Structure; using static Gremlin.Net.Process.Traversal.AnonymousTraversalSource; namespace gremlinExample { class Program { static void Main(string[] args) { try { var endpoint = "your-neptune-endpoint"; // This uses the default Neptune and Gremlin port, 8182 var gremlinServer = new GremlinServer(endpoint, 8182, enableSsl: true ); var gremlinClient = new GremlinClient(gremlinServer); var remoteConnection = new DriverRemoteConnection(gremlinClient, "g"); var g = Traversal().WithRemote(remoteConnection); g.AddV("Person").Property("Name", "Justin").Iterate(); g.AddV("Custom Label").Property("name", "Custom id vertex 1").Iterate(); g.AddV("Custom Label").Property("name", "Custom id vertex 2").Iterate(); var output = g.V().Limit<Vertex>(3).ToList(); foreach(var item in output) { Console.WriteLine(item); } } catch (Exception e) { Console.WriteLine("{0}", e); } } } }
  4. 輸入下列命令以執行範例:

    dotnet run

    本範例結尾的 Gremlin 查詢會傳回單一頂點的數量,以進行測試。接著會列印至主控台。

    注意

    Gremlin 查詢最後的部分 Next() 用來提交周遊至伺服器,以供進行評估。如果您未包含該方法或其他同等方法,該查詢不會提交到 Neptune 資料庫執行個體。

    以下方法會查詢提交至 Neptune 資料庫執行個體:

    • ToList()

    • ToSet()

    • Next()

    • NextTraverser()

    • Iterate()

    如果您需要序列化並傳回查詢結果,請使用 Next(),或者如果不需要,則使用 Iterate()

    上述範例會使用 g.V().Limit(3).ToList() 周遊傳回清單。若要查詢其他內容,將其換成其他使用其中一個適當之結束方法的 Gremlin 周遊。