I tried Cassandra's Object Mapper for Java

I tried Cassandra's Object Mapper for Java

A memo when I cut and pasted the sample code in Official DataStax Manual and moved it. ..

Premise

Preparation

Cassandra side

  1. Install Cassandra

    $ wget http://ftp.jaist.ac.jp/pub/apache/cassandra/3.10/apache-cassandra-3.10-bin.tar.gz
    $ tar -zxvf apache-cassandra-3.10-bin.tar.gz
    $ sudo mv apache-cassandra-3.10/ /opt/cassandra
    
  2. Start Cassandra server

    $ /opt/cassandra/bin/cassandra
    
  3. Create Keyspace (name: ks)

    $ /opt/cassandra/bin/cqlsh 127.0.0.1 -e "\
    CREATE KEYSPACE ks\
      WITH REPLICATION = { 'class': 'SimpleStrategy', 'replication_factor': 1 };"
    
  4. Create Table (name: ʻusers`)

    $ /opt/cassandra/bin/cqlsh 127.0.0.1 -e "\
    CREATE TABLE ks.users (\
      user_id uuid,\
      name text,\
      strength int,\
      PRIMARY KEY(user_id, name)\
    );"
    

Java side

--Main class

    ```Main.java
    package sample;

    import com.datastax.driver.core.Cluster;
    import com.datastax.driver.core.Session;
    import com.datastax.driver.mapping.Mapper;
    import com.datastax.driver.mapping.MappingManager;
    import java.util.UUID;

    public class Main {

      public static void main(String[] args) {
        Cluster cluster = null;
        try {
          cluster = Cluster.builder()
              .addContactPoint("127.0.0.1")
              .build();
          Session session = cluster.connect();
          MappingManager manager = new MappingManager(session);
          Mapper<User> mapper = manager.mapper(User.class);

          User u = new User(UUID.randomUUID(), "John Doe", 9999);
          mapper.save(u);

        } finally {
          if (cluster != null) {
            cluster.close();
          }
        }
      }
    }
    ```

Execution result

After executing the above main class, check from cqlsh whether data has been inserted.

$ /opt/cassandra/bin/cqlsh 127.0.0.1 -e "SELECT * FROM ks.users;"

 user_id                              | name     | strength
--------------------------------------+----------+----------
 e918961b-57c1-4f5d-8c4e-9d08e9ea4e7a | John Doe |     9999

(1 rows)

reference

Recommended Posts

I tried Cassandra's Object Mapper for Java
I tried Drools (Java, InputStream)
I tried using Java REPL
I tried using an extended for statement in Java
I tried metaprogramming in Java
I tried to interact with Java
I tried UDP communication with Java
I tried the Java framework "Quarkus"
I tried using Java8 Stream API
I tried using JWT in Java
I tried to summarize Java learning (1)
I tried to summarize Java 8 now
I tried using Java memo LocalDate
I tried using GoogleHttpClient of Java
Java14 came out, so I tried record for the time being
I tried using Elasticsearch API in Java
Java for All! I read everyone's Java #minjava
I tried to summarize Java lambda expressions
Java9 was included, so I tried jshell.
I tried the new era in Java
I tried using OpenCV with Java + Tomcat
I tried Google's entrance exam (unofficial) [java]
Java12 came out, so I tried the switch expression for the time being
I made a Diff tool for Java files
I tried using Docker for the first time
I tried putting Java on my Mac easily
I tried to make Basic authentication with Java
[Java] What should I use for writing files?
For JAVA learning (2018-03-16-01)
java I tried to break a simple block
I studied for 3 weeks and passed Java Bronze
I tried hitting a Java method from ABCL
I tried to implement deep learning in Java
[Must see !!!] I tried to summarize object orientation!
I tried Spring.
2017 IDE for Java
I tried touching Docker for the first time
I tried tomcat
I tried to output multiplication table in Java
I tried youtubeDataApi.
I tried refactoring ①
[Azure] I tried to create a Java application for free-Web App creation- [Beginner]
I tried to create Alexa skill in Java
[Java] Object class
I tried FizzBuzz.
Java for statement
I tried to break a block with java (1)
I tried running Java on a Mac terminal
I tried JHipster 5.1
I tried "Visual Studio Code Installer for Java" which can create Java development environment immediately
[Azure] I tried to create a Java application for free ~ Connect with FTP ~ [Beginner]
I tried Java Lambda input / output type ~ POJO edition ~
I tried Mastodon's Toot and Streaming API in Java
I tried to implement TCP / IP + BIO with JAVA
[Java 11] I tried to execute Java without compiling with javac
I tried using Google Cloud Vision API in Java
[Java] I tried to solve Paiza's B rank problem
I tried to operate SQS using AWS Java SDK
# 2 [Note] I tried to calculate multiplication tables in Java.
I tried using the Migration Toolkit for Application Binaries
I tried Java Lambda input / output type ~ Stream version ~