1 introduction

Previously we successfully installed K8ssandra on Ubuntu in our article “Getting Started with K8ssandra – Detailed Documentation of deploying K8ssandra to Kubernetes on Linux.” Now let’s look at how to access Cassandra.

The Stargate component of K8ssandra provides a variety of data access modes, corresponding ports are as follows:

  • 8080: GraphQL interface
  • 8081: REST Auth
  • 8082: REST interface
  • 9042: CQL service

We use the most common 9042 port, please refer to the official documentation for the rest.

2 Three access modes

First expose the service and then find the corresponding port:

$ kubectl expose deployment k8ssandra-dc1-stargate --type=NodePort --name=stargate-out
$ kubectl get svc stargate-out
Copy the code

2.1 CQLSH command

To install CLQSH:

$ pip install cqlsh
Copy the code

Connect to database:

CQLSH -u k8ssandra-superuser -p YMEbXcPCW9xxxxxxx 127.0.0.1 30703Copy the code

Next data manipulation:

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]');
Copy the code

After writing the data, let’s query:

2.2 Connection using IDEA

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

Then you can view the relevant data, as follows:

2.3 Access through Java programs

Import dependencies as follows:

<dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-cassandra</artifactId>
  <version>3.2.5</version>
</dependency>
Copy the code

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;
}
Copy the code

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;

@Repository
public interface UserRepository extends CassandraRepository<User.String> {}Copy the code

You also need to add the following to the configuration class:

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

Configure the database connection properties:

server.port=8080 spring. Data. Cassandra. Contact - points = 8.134.124.38:30703 spring. Data. Cassandra. Username = k8ssandra - superuserspring. Data .cassandra.password=YMEbXcPCW9xrfxxxxxspring.data.cassandra.local-datacenter=dc1spring.data.cassandra.keyspace-name=pksl ow
Copy the code

That’s pretty much it.

Start the program with the following access tests:

3 summary

Please check the code: github.com/LarryDpk/pk…