這很重要
您是否正在尋找一種適用於高擴展性場景的資料庫解決方案,且具有 99.999% 可用性的服務等級協定(SLA)、即時自動擴展,以及跨多個區域的自動容錯切換? 請考慮 適用於 NoSQL 的 Azure Cosmos DB。
您是否想要實作線上分析處理 (OLAP) 圖表或移轉現有的 Apache Gremlin 應用程式? 考慮使用 Microsoft Fabric 中的 Graph。
開始使用 適用於 Python 的 Azure Cosmos DB for Apache Gremlin 用戶端程式庫來儲存、管理和查詢非結構化資料。 請依照本指南中的步驟建立新帳戶、安裝 Python 用戶端程式庫、連線到帳戶、執行一般作業,以及查詢最終範例資料。
先決條件
Azure 訂用帳戶
- 如尚未擁有 Azure 訂用帳戶,請在開始之前先建立免費帳戶。
Azure Cloud Shell 中最新版的 Azure CLI。
- 如果您想要在本機執行 CLI 參考命令,請使用
az login命令登入 Azure CLI。
- 如果您想要在本機執行 CLI 參考命令,請使用
- Python 3.12 或更新版本
設定
首先,設定本指南的帳號和開發環境。 本節將引導您進行創建帳戶、獲取其憑證,然後準備開發環境。
建立帳戶
首先建立 Apache Gremlin 帳戶的 API。 建立帳戶之後,請建立資料庫和圖形資源。
如果您還沒有目標資源群組,請使用命令
az group create在訂用帳戶中建立新的資源群組。az group create \ --name "<resource-group-name>" \ --location "<location>"使用命令
az cosmosdb create來建立具有預設設定的新 Azure Cosmos DB for Apache Gremlin 帳戶。az cosmosdb create \ --resource-group "<resource-group-name>" \ --name "<account-name>" \ --locations "regionName=<location>" \ --capabilities "EnableGremlin"使用
az cosmosdb gremlin database create建立新的資料庫,命名為cosmicworks。az cosmosdb gremlin database create \ --resource-group "<resource-group-name>" \ --account-name "<account-name>" \ --name "cosmicworks"使用指令
az cosmosdb gremlin graph create來建立名為 的新圖形products。az cosmosdb gremlin graph create \ --resource-group "<resource-group-name>" \ --account-name "<account-name>" \ --database-name "cosmicworks" \ --name "products" \ --partition-key-path "/category"
取得認證
現在,取得客戶端程式庫所需的密碼,以建立與最近創建的帳戶的連接。
使用
az cosmosdb show來取得帳戶的主機。az cosmosdb show \ --resource-group "<resource-group-name>" \ --name "<account-name>" \ --query "{host:name}"記錄先前命令輸出中屬性
host的值。 此屬性的值是您稍後在本指南中用來連線到具有程式庫的帳戶的主機。使用
az cosmosdb keys list可獲得帳戶的 金鑰 。az cosmosdb keys list \ --resource-group "<resource-group-name>" \ --name "<account-name>" \ --type "keys"記錄先前命令輸出中屬性
primaryMasterKey的值。 此屬性的值是您稍後在本指南中用來連線到具有程式庫的帳戶的索引鍵。
準備開發環境
然後,使用新專案和用戶端程式庫來設定您的開發環境。 此步驟是繼續本指南其餘部分之前的最後一個必要先決條件。
從空白資料夾中開始。
從 Python 套件索引 (PyPI) 匯入
gremlinpython套件。pip install gremlinpython建立 app.py 檔案。
物件模型
| 說明 | |
|---|---|
GremlinClient |
代表用來連線 Gremlin 伺服器並與之互動的用戶端 |
GraphTraversalSource |
用來建構和執行 Gremlin 周遊 |
程式碼範例
驗證用戶端
首先,使用本指南稍早收集的認證來驗證用戶端。
在整合開發環境 (IDE) 中開啟 app.py 檔案。
從
gremlin_python.driver庫匯入下列類型:gremlin_python.driver.clientgremlin_python.driver.serializer
from gremlin_python.driver import client, serializer為本指南稍早收集的認證建立字串變數。 將變數
hostname命名為primary_key和 。hostname = "<host>" primary_key = "<key>"使用先前步驟中建立的認證和組態變數來建立
Client物件。 將變數client命名為 。client = client.Client( url=f"wss://{hostname}.gremlin.cosmos.azure.com:443/", traversal_source="g", username="/dbs/cosmicworks/colls/products", password=f"{primary_key}", message_serializer=serializer.GraphSONSerializersV2d0() )
插入數據
接下來,將新的頂點和邊緣資料插入到圖形中。 在建立新資料之前,請清除任何現有資料的圖表。
執行
g.V().drop()查詢以清除圖形中的所有頂點和邊緣。client.submit("g.V().drop()").all().result()建立一個新增頂點的 Gremlin 查詢。
insert_vertex_query = ( "g.addV('product')" ".property('id', prop_id)" ".property('name', prop_name)" ".property('category', prop_category)" ".property('quantity', prop_quantity)" ".property('price', prop_price)" ".property('clearance', prop_clearance)" )新增單一產品的頂點。
client.submit( message=insert_vertex_query, bindings={ "prop_id": "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb", "prop_name": "Yamba Surfboard", "prop_category": "gear-surf-surfboards", "prop_quantity": 12, "prop_price": 850.00, "prop_clearance": False, }, ).all().result()再新增兩個頂點,即可獲得兩個額外的產品。
client.submit( message=insert_vertex_query, bindings={ "prop_id": "bbbbbbbb-1111-2222-3333-cccccccccccc", "prop_name": "Montau Turtle Surfboard", "prop_category": "gear-surf-surfboards", "prop_quantity": 5, "prop_price": 600.00, "prop_clearance": True, }, ).all().result() client.submit( message=insert_vertex_query, bindings={ "prop_id": "cccccccc-2222-3333-4444-dddddddddddd", "prop_name": "Noosa Surfboard", "prop_category": "gear-surf-surfboards", "prop_quantity": 31, "prop_price": 1100.00, "prop_clearance": False, }, ).all().result()建立另一個新增邊緣的 Gremlin 查詢。
insert_edge_query = ( "g.V([prop_partition_key, prop_source_id])" ".addE('replaces')" ".to(g.V([prop_partition_key, prop_target_id]))" )新增兩個邊緣。
client.submit( message=insert_edge_query, bindings={ "prop_partition_key": "gear-surf-surfboards", "prop_source_id": "bbbbbbbb-1111-2222-3333-cccccccccccc", "prop_target_id": "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb", }, ).all().result() client.submit( message=insert_edge_query, bindings={ "prop_partition_key": "gear-surf-surfboards", "prop_source_id": "bbbbbbbb-1111-2222-3333-cccccccccccc", "prop_target_id": "cccccccc-2222-3333-4444-dddddddddddd", }, ).all().result()
讀取資料
然後,讀取先前插入圖表的資料。
建立查詢,使用唯一識別碼和分區鍵值讀取頂點。
read_vertex_query = "g.V([prop_partition_key, prop_id])"然後,透過提供必要的參數來讀取頂點。
matched_item = client.submit( message=read_vertex_query, bindings={ "prop_partition_key": "gear-surf-surfboards", "prop_id": "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb" } ).one()
查詢數據
最後,使用查詢來尋找與圖形中特定遍歷或篩選器相符的所有資料。
建立查詢,以尋找從特定頂點延伸出去的所有頂點。
find_vertices_query = ( "g.V().hasLabel('product')" ".has('category', prop_partition_key)" ".has('name', prop_name)" ".outE('replaces').inV()" )執行指定
Montau Turtle Surfboard產品的查詢。find_results = client.submit( message=find_vertices_query, bindings={ "prop_partition_key": "gear-surf-surfboards", "prop_name": "Montau Turtle Surfboard", }, ).all().result()逐一查看查詢結果。
for result in find_results: # Do something here with each result
執行程式碼
使用應用程式目錄中的終端機執行新建立的應用程式。
python app.py
清除資源
當您不再需要帳戶時,請從 Azure 訂用帳戶中移除帳戶,方法是 刪除 該資源。
az cosmosdb delete \
--resource-group "<resource-group-name>" \
--name "<account-name>"