Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "JNoSQL/Diana/Cassandra"

< JNoSQL‎ | Diana
 
Line 83: Line 83:
 
             columnEntityManager.insert(entity);
 
             columnEntityManager.insert(entity);
  
             ColumnQuery query = ColumnQuery.of(COLUMN_FAMILY);
+
             ColumnQuery query = seçect().from(COLUMN_FAMILY).where(ColumnCondition.eq(id)).build();
            query.and(ColumnCondition.eq(id));
+
 
             Optional<ColumnEntity> result = columnEntityManager.singleResult(query);
 
             Optional<ColumnEntity> result = columnEntityManager.singleResult(query);
 
             System.out.println(result);
 
             System.out.println(result);

Latest revision as of 02:54, 17 November 2017

Cassandra

Apache Cassandra is a free and open-source distributed database management system designed to handle large amounts of data across many commodity servers, providing high availability with no single point of failure. Cassandra offers robust support for clusters spanning multiple datacenters, with asynchronous masterless replication allowing low latency operations for all clients.


Maven Project

To use Cassandra driver in a Maven Project:

        <dependency>
            <groupId>org.jnosql.diana</groupId>
            <artifactId>cassandra-driver</artifactId>
            <version>version</version>
        </dependency>

E.g:

        <dependency>
            <groupId>org.jnosql.diana</groupId>
            <artifactId>cassandra-driver</artifactId>
            <version>0.0.1</version>
        </dependency>

Column

The Cassandra column implementation uses the CassandraConfiguration to a configuration. This class has configurations methods such as host, port, timeout, user, password. Beyond the method also it can read a configuration from diana-cassandra.properties file.


  • cassandra-host-: The Cassandra host as prefix, you can set how much you want just setting the number order,

eg: cassandra-host-1 = host, cassandra-host-2 = host2

  • cassandra-query-: The Cassandra query to run when an instance is started, you can set how much you want just

setting the order number, eg: cassandra-query-1=cql, cassandra-query-2=cql2...

  • cassandra-threads-number: The number of executor to run on Async process, if it isn't defined that will use the number of processor
  • cassandra-ssl: Define ssl, the default value is false
  • cassandra-metrics: enable metrics, the default value is true
  • cassandra-jmx: enable JMX, the default value is true


E.g.:

cassandra-host-1=localhost
cassandra-port=9142
cassandra-threads-number=4
cassandra-query-1=CREATE KEYSPACE IF NOT EXISTS newKeySpace WITH replication = {'class': 'SimpleStrategy', replication_factor' : 3};
cassandra-query-2=CREATE TYPE IF NOT EXISTS newKeySpace.fullname ( firstname text, lastname text);
cassandra-query-3=CREATE COLUMNFAMILY IF NOT EXISTS newKeySpace.newColumnFamily (id bigint PRIMARY KEY, version double, options list<int>, name text);
cassandra-query-4=CREATE COLUMNFAMILY IF NOT EXISTS newKeySpace.users ( nickname text PRIMARY KEY, name frozen <fullname>);
cassandra-query-5=CREATE COLUMNFAMILY IF NOT EXISTS newKeySpace.history ( name text PRIMARY KEY, dataStart date, dateEnd timestamp);


Sample Code

public class App {

    public static final String KEY_SPACE = "newKeySpace";
    public static final String COLUMN_FAMILY = "newColumnFamily";

    public static void main(String[] args) {

        ColumnConfiguration condition = new CassandraConfiguration();

        try(ColumnFamilyManagerFactory managerFactory = condition.get()) {
            ColumnFamilyManager columnEntityManager = managerFactory.get(KEY_SPACE);
            ColumnEntity entity = ColumnEntity.of(COLUMN_FAMILY);
            Column id = Column.of("id", 10L);
            entity.add(id);
            entity.add(Column.of("version", 0.001));
            entity.add(Column.of("name", "Diana"));
            entity.add(Column.of("options", Arrays.asList(1, 2, 3)));

            columnEntityManager.insert(entity);

            ColumnQuery query = seçect().from(COLUMN_FAMILY).where(ColumnCondition.eq(id)).build();
            Optional<ColumnEntity> result = columnEntityManager.singleResult(query);
            System.out.println(result);

        }


    }
}


Consistency Level

The Cassandra consistency level is also available in Diana project. With Consistency level, you can delete, update, insert and query a ColumnEntity.


Sample Code

public class CassandraApp {


    public static final String KEY_SPACE = "newKeySpace";
    public static final String COLUMN_FAMILY = "newColumnFamily";

    public static void main(String[] args) {

        CassandraConfiguration condition = new CassandraConfiguration();

        try(CassandraDocumentEntityManagerFactory managerFactory = condition.get()) {
            CassandraColumnFamilyManager columnEntityManager = managerFactory.get(KEY_SPACE);
            ColumnEntity entity = ColumnEntity.of(COLUMN_FAMILY);
            Column id = Column.of("id", 10L);
            entity.add(id);
            entity.add(Column.of("version", 0.001));
            entity.add(Column.of("name", "Diana"));
            entity.add(Column.of("options", Arrays.asList(1, 2, 3)));

            columnEntityManager.insert(entity);

            ColumnQuery query = ColumnQuery.of(COLUMN_FAMILY);
            query.and(ColumnCondition.eq(id));
           List<ColumnEntity> columnEntities = columnEntityManager.select(query, ConsistencyLevel.LOCAL_QUORUM);

            //cassandra implementation
            columnEntityManager.save(entity, ConsistencyLevel.THREE);
            Optional<ColumnEntity> findResult = columnEntityManager.singleResult(query, ConsistencyLevel.LOCAL_QUORUM);

        }


    }

}

Cassandra Query Language

CQL offers a model close to SQL in the sense that data is put in tables containing rows of columns. For that reason, when used in this document, these terms (tables, rows, and columns) have the same definition than they have in SQL.


Sample Code

     public static final String KEY_SPACE = "newKeySpace";
    public static final String COLUMN_FAMILY = "newColumnFamily";

    public static void main(String[] args) {

        CassandraConfiguration condition = new CassandraConfiguration();

        try(CassandraDocumentEntityManagerFactory managerFactory = condition.get()) {
            CassandraColumnFamilyManager columnEntityManager = managerFactory.get(KEY_SPACE);
            ColumnEntity entity = ColumnEntity.of(COLUMN_FAMILY);
            Column id = Column.of("id", 10L);
            entity.add(id);
            entity.add(Column.of("version", 0.001));
            entity.add(Column.of("name", "Diana"));
            entity.add(Column.of("options", Arrays.asList(1, 2, 3)));

            columnEntityManager.insert(entity);

            //cassandra implementation
            List<ColumnEntity> entities = columnEntityManager.cql("select * from newKeySpace.newColumnFamily");
            CassandraPrepareStatment cassandraPrepareStatment = columnEntityManager.nativeQueryPrepare("select * from newKeySpace.newColumnFamily where id ?");
            List<ColumnEntity> entityList = cassandraPrepareStatment.bind(10L).executeQuery();

        }


    }

}



User-defined Type

User-defined types (UDTs) can attach multiple data fields, each named and typed, to a single column. The fields used to create a UDT may be any valid data type, including collections and other existing UDTs. Once created, UDTs may be used to define a column in a table.


Sample Code

     public static final String KEY_SPACE = "newKeySpace";
    public static final String COLUMN_FAMILY = "newColumnFamily";

    public static void main(String[] args) {

        CassandraConfiguration condition = new CassandraConfiguration();

        try(CassandraDocumentEntityManagerFactory managerFactory = condition.get()) {
            CassandraColumnFamilyManager columnEntityManager = managerFactory.get(KEY_SPACE);
            ColumnEntity entity = ColumnEntity.of("users");
            entity.add(Column.of("nickname", "ada"));
            List<Column> columns = new ArrayList<>();
            columns.add(Column.of("firstname", "Ada"));
            columns.add(Column.of("lastname", "Lovelace"));
            UDT udt = UDT.builder().withName("name")
                .withTypeName("fullname")
                .addAll(columns).build();
             entity.add(udt);
           columnEntityManager.save(entity);

        }


    }

}



Links

Back to the top