使用Stargate訪問K8ssandra,Springboot整合Cassandra
1 簡介
之前我們在文章《K8ssandra入門-詳細記錄在Linux上部署K8ssandra到Kubernetes》成功地在Ubuntu上安裝了K8ssandra,現在我們來看看如何訪問Cassandra。
K8ssandra的元件Stargate提供了多種方式的資料訪問,對應埠如下:
- 8080:GraphQL interface
- 8081:REST Auth
- 8082:REST interface
- 9042:CQL service
我們使用最常用的9042埠,其它請參考官方文件。
2 三種方式訪問
先暴露服務,然後找到對應的埠:
bash
$ kubectl expose deployment k8ssandra-dc1-stargate --type=NodePort --name=stargate-out
$ kubectl get svc stargate-out
2.1 cqlsh命令
安裝clqsh命令:
bash
$ pip install cqlsh
連線資料庫:
bash
cqlsh -u k8ssandra-superuser -p YMEbXcPCW9xxxxxxx 127.0.0.1 30703
接著進行資料操作:
```bash CREATE KEYSPACE pkslow WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};
use pkslow;
CREATE TABLE users (username text primary key, password text, email text);
INSERT INTO users (username, password, email) values ('larry', 'larry123', '[email protected]'); INSERT INTO users (username, password, email) values ('admin', '123456', '[email protected]'); INSERT INTO users (username, password, email) values ('carol', '123456', '[email protected]'); INSERT INTO users (username, password, email) values ('david', '123456', '[email protected]'); ```
寫入了資料後,我們查詢看看:
2.2 用IDEA連線
配置資料庫,選擇Cassandra,連線資訊如下:
接著就可以檢視相關的資料了,如下:
2.3 通過Java程式訪問
引入依賴如下:
xml
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-cassandra</artifactId>
<version>3.2.5</version>
</dependency>
準備實體類:
```java package com.pkslow.springboot.cassandra.entity;
import org.springframework.data.annotation.Id; import org.springframework.data.cassandra.core.mapping.Table;
@Table(value = "users") public class User { @Id private String username; private String password; private String email; } ```
Reposity類:
```java package com.pkslow.springboot.cassandra.repository;
import com.pkslow.springboot.cassandra.entity.User; import org.springframework.data.cassandra.repository.CassandraRepository; import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends CassandraRepository
同時需要在配置類中加上:
java
@EnableCassandraRepositories(basePackages = "com.pkslow.springboot.cassandra.repository")
配置一下資料庫連線屬性:
properties
server.port=8080spring.data.cassandra.contact-points=8.134.124.38:30703spring.data.cassandra.username=k8ssandra-superuserspring.data.cassandra.password=YMEbXcPCW9xrfxxxxxspring.data.cassandra.local-datacenter=dc1spring.data.cassandra.keyspace-name=pkslow
這樣就基本可以了。
啟動程式,訪問測試如下:
3 總結
程式碼請檢視:https://github.com/LarryDpk/pkslow-samples
- Flutter開發個人經驗收集整理
- 通過Docker啟動DB2,並在Spring Boot整合DB2
- Spring Cloud服務發現元件Eureka
- Spring Cloud Service Discovery with Netflix Eureka
- Mac uses pstree to show the process tree
- 在GCP上建立Cloud SQL的三種方式(Console,gcloud,Terraform)
- Three ways to create GCE on GCP(Console,gcloud,Terraform)
- Provision the Google Kubernetes Engine(GKE) with gcloud and access with kubectl
- Manage Terraform State on Google Cloud Storage(GCS)
- Manage GCP Pubsub with Terraform
- 初始化一個GCP專案並用gcloud訪問操作
- Spring Boot Actuator show the git and build info
- Spring Boot通過Actuator顯示git和build的資訊
- 超級好用的輕量級JSON處理命令jq
- 設計模式整理與總結、心得與體會
- Apache Beam入門及Java SDK開發初體驗
- 使用Stargate訪問K8ssandra,Springboot整合Cassandra
- 使用Stargate訪問K8ssandra,Springboot整合Cassandra
- K8ssandra入門-詳細記錄在Linux上部署K8ssandra到Kubernetes
- 在Mac上安裝Istio並使用,有豐富的監控Kiali、Grafana、Jaeger