如何搭配 Azure Cosmos DB for Apache Cassandra 使用 Spring Data
本文示範如何建立一個範例應用程式,使用 Spring Data 並透過適用於 Apache Cassandra 的 Azure Cosmos DB來存取資訊。
先決條件
Azure 訂用帳戶 - 建立一個免費的帳戶。
Java Development Kit (JDK)8 或更高版本。
建立 Azure Cosmos DB 帳戶
下列程式會在 Azure 入口網站中建立及設定 Azure Cosmos DB 帳戶。
使用 Azure 入口網站建立 Azure Cosmos DB 帳戶
注意
您可以在 Azure Cosmos DB 檔中閱讀建立帳戶的詳細資訊。
在 https://portal.azure.com/ 流覽至 Azure 入口網站並登入。
選取 [建立資源],然後 [啟動],然後選取 [Azure Cosmos DB]。
在 [選擇 API 選項] 畫面上,選取 [Cassandra]。
指定以下資訊:
- 訂用帳戶:指定要使用的 Azure 訂用帳戶。
- 資源群組:指定是要建立新的資源群組,還是選擇現有的資源群組。
- 帳戶名稱:為您的 Azure Cosmos DB 帳戶選擇唯一名稱;此名稱會用來建立完整網域名稱,例如 wingtiptoyscassandra.documents.azure.com。
- API:為此教學指定 Cassandra。
- 位置:指定資料庫最接近的地理區域。
在輸入所有上述信息後,請按一下 檢閱 + 建立。
如果檢閱頁面上的所有項目看起來都正確,請點擊 建立。
部署資料庫需要幾分鐘的時間。
將關鍵字空間 (keyspace) 新增至您的 Azure Cosmos DB 帳戶
在 https://portal.azure.com/ 流覽至 Azure 入口網站並登入。
選取 [[所有資源],然後選取您建立的 Azure Cosmos DB 帳戶。
選取 [資料總管],選取向下箭號,然後選取 [[新增金鑰空間]。 輸入 Keyspace 識別碼的唯一標識符,然後選取 [確定] [確定]。
擷取 Azure Cosmos DB 帳戶的連線設定
在 https://portal.azure.com/ 流覽至 Azure 入口網站並登入。
選取 [[所有資源],然後選取您建立的 Azure Cosmos DB 帳戶。
選取 [連接字串],然後複製 [聯繫人點]、[埠]、[使用者名稱] 和 [主要密碼] 字段的值:您稍後會使用這些值來設定應用程式。
設定範例應用程式
接下來的步驟將配置測試應用程式。
開啟命令殼層,並使用 git 命令複製範例專案,如下列範例所示:
git clone https://github.com/Azure-Samples/spring-data-cassandra-on-azure.git
在範例專案的 資源 目錄中,找出 application.properties 檔案,或如果檔案不存在,請建立檔案。
在文本編輯器中開啟 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
指定您在本文前面提到的 主要密碼。 儲存並關閉 application.properties 檔案。
封裝及測試範例應用程式
流覽至包含 pom.xml 檔案的目錄,以建置及測試應用程式。
使用 Maven 建置範例應用程式;例如:
mvn clean package
啟動範例應用程式;例如:
java -jar target/spring-data-cassandra-on-azure-0.1.0-SNAPSHOT.jar
使用命令提示字元
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'}.
使用命令提示字元
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"}]
總結
在本教學課程中,您已建立使用 Spring Data 來儲存和擷取資訊的範例 Java 應用程式,並使用適用於 Apache Cassandra 的 Azure Cosmos DB 來儲存和擷取資訊。
清除資源
不再需要時,請使用 Azure 入口網站 刪除本文中建立的資源,以避免產生非預期的費用。
後續步驟
若要深入瞭解 Spring 和 Azure,請繼續前往 Azure 上的 Spring 檔中心。
另請參閱
如需搭配 Java 使用 Azure 的詳細資訊,請參閱 Azure for Java Developers 和與 Azure DevOps 和 Java 搭配工作的指南 。