如何搭配 Azure Cosmos DB for Apache Cassandra 使用 Spring Data

本文說明如何使用 Spring Data 搭配 Azure Cosmos DB for Apache Cassandra,建立一個能儲存和檢索資料的 Java 範例應用程式。

先決條件

  • Git 用戶端。

  • cURL 或類似的 HTTP 公用程式來測試功能。

建立 Azure Cosmos DB 帳戶

下列程式會在 Azure 入口網站中建立及設定 Azure Cosmos DB 帳戶。

用 Azure portal 建立 Azure Cosmos DB 帳戶

注意

欲了解更多關於建立帳號的資訊,請參閱 Azure Cosmos DB 文件

  1. https://portal.azure.com/ 流覽至 Azure 入口網站並登入。

  2. 選擇建立資源,選擇開始,然後選擇 Azure Cosmos DB

  3. 在 [選擇 API 選項] 畫面上,選取 [Cassandra]。

    Azure portal API 選項頁面的截圖,選取 Cassandra 後。

  4. 指定以下資訊:

    • 訂用帳戶:指定要使用的 Azure 訂用帳戶。
    • 資源群組:指定是建立新的資源群組,還是選擇現有的資源群組。
    • 帳號名稱:為你的 Azure Cosmos DB 帳號選擇一個獨特的名稱。 這個名稱用於建立一個完全合格的網域名稱,例如 wingtiptoyscassandra.documents.azure.com
    • API:為此教學指定 Cassandra
    • 位置:指定資料庫最接近的地理區域。

    Azure portal 入面 Azure Cosmos DB 帳戶設定嘅截圖。

  5. 輸入上述所有資訊後,選擇 「檢視+建立」。

  6. 如果評論頁面上一切正常,請選擇 建立

部署資料庫需要幾分鐘的時間。

將關鍵字空間 (keyspace) 新增至您的 Azure Cosmos DB 帳戶

  1. https://portal.azure.com/ 流覽至 Azure 入口網站並登入。

  2. 選擇「所有資源」,然後選擇你建立的 Azure Cosmos DB 帳號。

  3. 選擇 Data Explorer,選擇向下箭頭,然後選擇 New Keyspace。 輸入 Keyspace ID 的唯一識別碼,然後選擇 確定

    Data Explorer 選中新鍵空間選項的截圖。

    建立 Azure Cosmos DB 鍵空間的對話截圖。

擷取 Azure Cosmos DB 帳戶的連線設定

  1. https://portal.azure.com/ 流覽至 Azure 入口網站並登入。

  2. 選擇「所有資源」,然後選擇你建立的 Azure Cosmos DB 帳號。

  3. 選擇 連線字串,並複製 聯絡點口、 使用者名稱主要密碼 欄位的數值。 你之後用這些值來設定你的應用程式。

    Azure入口Azure Cosmos DB 連接字串設定截圖。

設定範例應用程式

接下來的步驟將配置測試應用程式。

  1. 開啟命令殼層,並使用 git 命令複製範例專案,如下列範例所示:

    git clone https://github.com/Azure-Samples/spring-data-cassandra-on-azure.git
    
  2. 在範例專案的 資源 目錄中,找出 application.properties 檔案,或如果檔案不存在,請建立檔案。

  3. 在文本編輯器中開啟 application.properties 檔案,並在檔案中新增或設定下列幾行,並以先前的適當值取代範例值:

    spring.data.cassandra.contact-points=wingtiptoyscassandra.cassandra.cosmos.azure.com
    spring.data.cassandra.port=10350
    spring.data.cassandra.username=wingtiptoyscassandra
    spring.data.cassandra.password=********
    

    哪裡:

    參數 描述
    spring.data.cassandra.contact-points 指定本文前面提到的 接觸點
    spring.data.cassandra.port 指定本文前面提到的
    spring.data.cassandra.username 指定您在本文稍早提及的 Username
    spring.data.cassandra.password 指定您在本文前面提到的 主要密碼
  4. 儲存並關閉 application.properties 檔案。

封裝及測試範例應用程式

流覽至包含 pom.xml 檔案的目錄,以建置及測試應用程式。

  1. 使用 Maven 建置範例應用程式;例如:

    mvn clean package
    
  2. 啟動範例應用程式;例如:

    java -jar target/spring-data-cassandra-on-azure-0.1.0-SNAPSHOT.jar
    
  3. 透過 curl 命令提示字元建立新紀錄。 以下範例說明如何建立紀錄:

    curl -s -d "{\"name\":\"dog\",\"species\":\"canine\"}" -H "Content-Type: application/json" -X POST http://localhost:8080/pets
    
    curl -s -d "{\"name\":\"cat\",\"species\":\"feline\"}" -H "Content-Type: application/json" -X POST http://localhost:8080/pets
    

    您的應用程式應該會傳回類似下列範例的值:

    Added Pet{id=60fa8cb0-0423-11e9-9a70-39311962166b, name='dog', species='canine'}.
    
    Added Pet{id=72c1c9e0-0423-11e9-9a70-39311962166b, name='cat', species='feline'}.
    
  4. 請使用 curl 命令提示字元檢索所有現有紀錄。 以下範例展示了如何擷取紀錄:

    curl -s http://localhost:8080/pets
    

    您的應用程式應該會傳回值,例如下列範例:

    [{"id":"60fa8cb0-0423-11e9-9a70-39311962166b","name":"dog","species":"canine"},{"id":"72c1c9e0-0423-11e9-9a70-39311962166b","name":"cat","species":"feline"}]
    

總結

在這個教學中,你創建了一個範例 Java 應用程式,使用 Spring Data 來儲存和檢索資訊,方法是使用 Azure Cosmos DB for Apache Cassandra。

清除資源

當你不再需要資源時,使用 Azure 入口網站刪除它們。 刪除資源有助於避免意外收費。

後續步驟

若要深入瞭解 Spring 和 Azure,請繼續前往 Azure 上的 Spring 檔中心。

另請參閱

如需搭配 Java 使用 Azure 的詳細資訊,請參閱 Azure for Java Developers 和與 Azure DevOps 和 Java 搭配工作的指南