Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Springboot to integrate Cassandra in the process of using Stargate to access K8ssandra

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

It is believed that many inexperienced people have no idea about how to use Springboot to integrate Cassandra in the process of using Stargate to access K8ssandra. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

1 introduction

Previously we successfully installed K8ssandra on Ubuntu in the article "getting started with K8ssandra-detailed documentation of deploying K8ssandra to Kubernetes on Linux". Now let's take a look at how to access Cassandra.

Stargate, a component of K8ssandra, provides multiple ways of data access, and the corresponding ports are as follows:

8080:GraphQL interface

8081:REST Auth

8082:REST interface

9042:CQL service

We use port 9042, which is the most commonly used. Please refer to the official documentation for the rest.

2 access in three ways

Expose the service first, and then find the corresponding port:

$kubectl expose deployment k8ssandra-dc1-stargate-- type=NodePort-- name=stargate-out$ kubectl get svc stargate-out2.1 cqlsh command

Install the clqsh command:

$pip install cqlsh

Connect to the database:

Cqlsh-u k8ssandra-superuser-p YMEbXcPCW9xxxxxxx 127.0.0.1 30703

The data operation is then performed:

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',' larry@pkslow.com'); INSERT INTO users (username, password, email) values ('admin',' 123456, 'admin@pkslow.com') INSERT INTO users (username, password, email) values ('carol',' 123456, 'carol@pkslow.com'); INSERT INTO users (username, password, email) values (' david', '123456,' david@pkslow.com')

After writing the data, let's query:

2.2 Connect with IDEA

To configure the database, select Cassandra. The connection information is as follows:

Then you can view the relevant data, as follows:

2.3 access through the Java program

Introduce dependencies as follows:

Org.springframework.data spring-data-cassandra 3.2.5

Prepare the entity class:

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 class:

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;@Repositorypublic interface UserRepository extends CassandraRepository {}

At the same time, you need to add:

@ EnableCassandraRepositories (basePackages = "com.pkslow.springboot.cassandra.repository")

Configure the database connection 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

That's basically all right.

Start the program, and the access test is as follows:

For the code, please see: https://github.com/LarryDpk/pkslow-samples

After reading the above, have you mastered how to use Springboot to integrate Cassandra in the process of accessing K8ssandra using Stargate? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report