Kyle  M. Farish

Kyle M. Farish

1562558524

How to connect ScyllaDB with Java

From Wikipedia: "Scylla is an open-source distributed NoSQL data store. It was designed to be compatible with Apache Cassandra while achieving significantly higher throughputs and lower latencies. It supports the same protocols as Cassandra (CQL and Thrift) and the same file formats (SSTable), but is a completely rewritten implementation, using the C++17 language replacing Cassandra's Java, and the Seastar asynchronous programming library replacing threads, shared memory, mapped files, and other classic Linux programming techniques."

This post will test the ScyllaDB with Java and give you the first impression of this database, which is compatible with Apache Cassandra.

Installation

The first step in this tutorial is to install the Scylla database. To make this process easier, we will use Docker, which simplifies a lot of the installation process with just this command:

docker run -d --name scylladb-instance -p 9042:9042 scylladb/scylla

Infrastructure Code

In a Maven project, we need to set the dependency project. Eclipse JNoSQL has two layers: one for mapping and the other one for communication. Once ScyllaDB is compatible with Cassandra, this post will use the Cassandra driver as the dependency for communication. Furthermore, it important to set a CDI implementation.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <artifactId>scylla</artifactId>
  <name>Artemis Demo using Java SE scylla</name>

  <parent>
    <groupId>org.jnosql.artemis</groupId>
    <artifactId>artemis-demo-java-se</artifactId>
    <version>0.0.9</version>
  </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>

    <dependency>
      <groupId>org.jnosql.artemis</groupId>
      <artifactId>cassandra-extension</artifactId>
      <version>${project.version}</version>
    </dependency>
  </dependencies>

</project>

Show Me the Code

For this sample, we have the Person entity with idname, and phones as fields:

import org.jnosql.artemis.Column;
import org.jnosql.artemis.Entity;
import org.jnosql.artemis.Id;

import java.util.List;

@Entity(“Person”)
public class Person {

  @Id(“id”)
  private long id;

  @Column
  private String name;

  @Column
  private List<String> phones;

}

There is a Repository interface that implements the basic operations in the database. Also, it has the method query, which gives the method that Eclipse JNoSQL will implement to the Java developer:

import org.jnosql.artemis.Repository;

public interface PersonRepository extends Repository<Person, Long> {

}

The next step is to make a connection, an entity manager, available to CDI.

import org.jnosql.diana.cassandra.column.CassandraColumnFamilyManager;
import org.jnosql.diana.cassandra.column.CassandraColumnFamilyManagerFactory;
import org.jnosql.diana.cassandra.column.CassandraConfiguration;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;

@ApplicationScoped
public class ScyllaProducer {

private static final String KEY_SPACE = "developers";

private CassandraConfiguration cassandraConfiguration;

private CassandraColumnFamilyManagerFactory managerFactory;

@PostConstruct

public void init() {
    cassandraConfiguration = new CassandraConfiguration();
    managerFactory = cassandraConfiguration.get();
}

@Produces
public CassandraColumnFamilyManager getManagerCassandra() {
    return managerFactory.get(KEY_SPACE);
}

}

The ScyllaProducer class makes a manager entity class available and ready for the application use. Once the code does not define the Settings, it will use the default behavior, so it will read from the diana-cassandra.properties file. This file has the configuration startup, thus the port and host to connect. Scylla as Cassandra is not schemaless, which means we need to create the structure before we use it. The properties file has Cassandra Query Language to create the keyspace and the column family structure.

cassandra.host.1=localhost
cassandra.query.1=CREATE KEYSPACE IF NOT EXISTS developers WITH replication = {‘class’: ‘SimpleStrategy’, ‘replication_factor’ : 3};
cassandra.query.2=CREATE COLUMNFAMILY IF NOT EXISTS developers.Person (id bigint PRIMARY KEY, name text, phones list<text>);

The whole configuration process and the code are ready, so let’s run the code!


import org.jnosql.artemis.cassandra.column.CassandraTemplate;
import org.jnosql.artemis.column.ColumnTemplate;
import org.jnosql.diana.api.column.ColumnQuery;

import javax.enterprise.inject.se.SeContainer;
import javax.enterprise.inject.se.SeContainerInitializer;
import java.util.Arrays;
import java.util.Optional;

import static org.jnosql.diana.api.column.query.ColumnQueryBuilder.select;

public class App {

public static void main(String[] args) {

    try (SeContainer container = SeContainerInitializer.newInstance().initialize()) {
        Person person = Person.builder().withPhones(Arrays.asList("234", "432"))
                .withName("Ada Lovelace").withId(1).build();
        ColumnTemplate template = container.select(CassandraTemplate.class).get();
        Person saved = template.insert(person);
        System.out.println("Person saved" + saved);

        ColumnQuery query = select().from("Person").where("id").eq(1L).build();

        Optional&lt;Person&gt; result = template.singleResult(query);
        System.out.println("Entity found: " + result);

    }
}

private App() {
}

}

import javax.enterprise.inject.se.SeContainer;
import javax.enterprise.inject.se.SeContainerInitializer;
import java.util.Arrays;
import java.util.Optional;

import static org.jnosql.artemis.DatabaseQualifier.ofColumn;

public class App2 {

public static void main(String[] args) {

    try(SeContainer container = SeContainerInitializer.newInstance().initialize()) {
        Person person = Person.builder().withPhones(Arrays.asList("234", "432"))
                .withName("Ada Lovelace").withId(1).build();
        PersonRepository repository = container.select(PersonRepository.class).select(ofColumn()).get();
        Person saved = repository.save(person);
        System.out.println("Person saved" + saved);

        Optional&lt;Person&gt; result = repository.findById(1L);
        System.out.println("Entity found: " + person);

    }
}

private App2() {}

}

We now have the ability to run a particular behavior in a NoSQL database matter! That’s why the Eclipse JNoSQL worries about the extensibility of both Cassandra and ScyllaDB and worries about the support to native query the CQL and operation with consistency level. To support it and move resources, there is the CassandraTemplate, which is an extension of ColumnTemplate and allows features from Cassandra as a consequence on ScyllaDB.

import com.datastax.driver.core.ConsistencyLevel;
import org.jnosql.artemis.cassandra.column.CassandraTemplate;
import org.jnosql.diana.api.column.ColumnQuery;

import javax.enterprise.inject.se.SeContainer;
import javax.enterprise.inject.se.SeContainerInitializer;
import java.util.Arrays;
import java.util.List;

import static org.jnosql.diana.api.column.query.ColumnQueryBuilder.select;

public class App3 {

public static void main(String[] args) {

    try (SeContainer container = SeContainerInitializer.newInstance().initialize()) {
        Person person = Person.builder().withPhones(Arrays.asList("234", "432"))
                .withName("Ada Lovelace").withId(1).build();
        CassandraTemplate cassandraTemplate = container.select(CassandraTemplate.class).get();
        Person saved = cassandraTemplate.save(person, ConsistencyLevel.ONE);
        System.out.println("Person saved" + saved);
        List&lt;Person&gt; people = cassandraTemplate.cql("select * from developers.Person where id = 1");
        System.out.println("Entity found: " + people);

    }
}

private App3() {
}

}

This post gives you an overview of ScyllaDB and how to connect with Java using Jakarta EE. ScyllaDB has compatibility with Cassandra that allows us to use the same Cassandra API without issues. 

Thank you for reading!


Originally published on https://dzone.com

#java #mongodb #sql #xcode

What is GEEK

Buddha Community

How to connect ScyllaDB with Java
Tyrique  Littel

Tyrique Littel

1600135200

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK?

OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE). It contains the virtual machine, the Java Class Library, and the Java compiler. The difference between the Oracle OpenJDK and Oracle JDK is that OpenJDK is a source code reference point for the open-source model. Simultaneously, the Oracle JDK is a continuation or advanced model of the OpenJDK, which is not open source and requires a license to use.

In this article, we will be installing OpenJDK on Centos 8.

#tutorials #alternatives #centos #centos 8 #configuration #dnf #frameworks #java #java development kit #java ee #java environment variables #java framework #java jdk #java jre #java platform #java sdk #java se #jdk #jre #open java development kit #open source #openjdk #openjdk 11 #openjdk 8 #openjdk runtime environment

Samanta  Moore

Samanta Moore

1620458875

Going Beyond Java 8: Local Variable Type Inference (var) - DZone Java

According to some surveys, such as JetBrains’s great survey, Java 8 is currently the most used version of Java, despite being a 2014 release.

What you are reading is one in a series of articles titled ‘Going beyond Java 8,’ inspired by the contents of my book, Java for Aliens. These articles will guide you step-by-step through the most important features introduced to the language, starting from version 9. The aim is to make you aware of how important it is to move forward from Java 8, explaining the enormous advantages that the latest versions of the language offer.

In this article, we will talk about the most important new feature introduced with Java 10. Officially called local variable type inference, this feature is better known as the **introduction of the word **var. Despite the complicated name, it is actually quite a simple feature to use. However, some observations need to be made before we can see the impact that the introduction of the word var has on other pre-existing characteristics.

#java #java 11 #java 10 #java 12 #var #java 14 #java 13 #java 15 #verbosity

Samanta  Moore

Samanta Moore

1620462686

Spring Boot and Java 16 Records

In this article, we will discuss Java 16’s newest feature, Records. Then we will apply this knowledge and use it in conjunction with a Spring Boot application.

On March 16th, 2021, Java 16 was GA. With this new release, tons of new exciting features have been added. Check out the release notes to know more about these changes in detail. This article’s focus will be on Java Records, which got delivered with JEP 395. Records were first introduced in JDK 14 as a preview feature proposed by JEP 359, and with JDK 15, they remained in preview with JEP 384. However, with JDK 16, Records are no longer in preview.

I have picked Records because they are definitely the most favored feature added in Java 16, according to this Twitter poll by Java Champion Mala Gupta.

I also conducted a similar survey, but it was focused on features from Java 8 onwards. The results were not unexpected, as Java 8 is still widely used. Very unfortunate, though, as tons of new features and improvements are added to newer Java versions. But in terms of features, Java 8 was definitely a game-changer from a developer perspective.

So let’s discuss what the fuss is about Java Records.

#java #springboot #java programming #records #java tutorials #java programmer #java records #java 16

Seamus  Quitzon

Seamus Quitzon

1602637135

Learning by Doing: How to Learn Java Basics by Building Your Own Project

Java is not the hardest language to start with. So, it becomes way popular among novice developers joining the ranks of Java coders every single day. If you are reading this blog post, you might be interested in learning Java.

Java is widely used across industry, and especially in the area of Enterprise software, which results in many high paying job opportunities and makes this programming language a common language for newbies. A general promotion of it within colleges and other institutions providing a formal Computer Science education also contributes to its popularity.

However, these are not the only advantages of Java — among other things, it allows you to adopt good practices and makes it way easier to learn other languages in the future. And with no doubt, you can easily learn it if you’re following the right approach. In this post, I am going to share some of them with you.

The Importance of Practice in Programming

Beyond all doubt, practice is important and valuable. But, before we get to the advantages of hands-on experience, I want to draw your attention to one essential thing I often tell my students.

New programmers who are just learning and start implementing things, without being supervised, often end up adapting bad practices. To avoid that, especially when you are making your first steps in programming, I recommend looking for a person who will supervise you and teach you. A strong mentorship with someone engaged in a serious project, as well as communication within the community in the form of sharing code and asking for feedback, is worth the effort. Similarly, when you are applying for your first job, you want to be looking for a company with a strong team and a good leader who would be keen on investing into your learning.

Now, let’s return to practical experience. Learning by doing is different from learning by passively consuming the information. To make sure we can use all the newly acquired technology, we should put our skills to test and write tons of code. The benefits of hands-on experience are almost endless.

Efficiency and Productivity

By practicing, you get a clear understanding of what programming is. Consequently, you start doing better with each new hands-on task, complete it faster, and thus become more productive.

Even if you are not working on real-world projects yet, it’s important to get used to having deadlines. They are inextricably linked to the programming process. My recommendation is to set up your own deadlines while practicing stage and follow them as closely as possible.

#java #learn java #java code #learn java in easy way #learn java course #learn java development

Samanta  Moore

Samanta Moore

1624948542

Lambda Expression in Java 8

In this blog we will understand what is the lambda expression and why we need lambda expression and how we use lambda and about the functional interface.

What is Lambda Expression :
  • It is an anonymous function.
  • Not having name
  • No return type and no modifiers.

#functional programming #java #functional java #java #java 8 #java8 #lambda expressions in java