Cassandra Database(5)Update Version and DataStax
1. Install and Update Version
There is no wget on MAC, so I use this command to get the latest binary file.
>curl -O http://apache.mirror.quintex.com/cassandra/2.0.4/apache-cassandra-2.0.4-bin.tar.gz
Or I can install the wget first.
>curl -O http://ftp.gnu.org/gnu/wget/wget-1.15.tar.gz
>tar -zxvf wget-1.15.tar.gz
>cd wget-1.15
>./configure --with-ssl=openssl
>make
>sudo make install
Verify the installation
>wget —version
Or
>wget —help
>wget http://apache.mirror.quintex.com/cassandra/2.0.4/apache-cassandra-2.0.4-bin.tar.gz
>tar zxvf apache-cassandra-2.0.4-bin.tar.gz
>mv apache-cassandra-2.0.4 /Users/carl/tool/
>sudo ln -s /Users/carl/tool/apache-cassandra-2.0.4 /opt/cassandra-2.0.4
>sudo ln -s /opt/cassandra-2.0.4 /opt/cassandra
>sudo mkdir /var/lib/cassandra
>sudo chown -R carl /var/lib/cassandra
Try to start the cassandra DB
>cassandra -f ./conf/cassandra.yaml
2. Using cqlsh
I can use this command
>cqlsh
Alternatively
>cqlsh host port
It will show as follow:
Connected to Test Cluster at localhost:9160. [cqlsh 4.1.0 | Cassandra 2.0.4 | CQL spec 3.1.1 | Thrift protocol 19.39.0] Use HELP for help
cqlsh>help
It will give me all the command list.
Create and use a demo keyspace
cqlsh> create keyspace mykeyspace with replication = { 'class': 'SimpleStrategy', 'replication_factor':1};
cqlsh> use mykeyspace;
cqlsh:mykeyspace>
The latest version of cassandra, since we have CQLSH, it is supporting something like SQL, that is cool.
Create table in the keyspace
cqlsh:mykeyspace> create table users ( user_id int primary key, firstname text, lastname text );
Directly use the SQL statement is fine here.
cqlsh:mykeyspace> insert into users(user_id, firstname, lastname) values (1,'carl','luo');
cqlsh:mykeyspace> insert into users(user_id, firstname, lastname) values (2,'ray','luo');
cqlsh:mykeyspace> insert into users(user_id, firstname, lastname) values (3,'kiko','kang');
cqlsh:mykeyspace> select * from users; user_id | firstname | lastname ---------+-----------+----------
1 | carl | luo
2 | ray | luo
3 | kiko | kang
Build the index
create index on users (lastname);
Then we can use the where clause
select * from users where lastname = 'luo';
User_ID is primary key
cqlsh:mykeyspace> select * from users where user_id = 1;
Here is more detail about the SQL
http://cassandra.apache.org/doc/cql3/CQL.html
http://www.datastax.com/documentation/cql/3.0/webhelp/index.html
3. Writing our Application
https://github.com/datastax
https://github.com/datastax/java-driver
http://www.datastax.com/documentation/developer/java-driver/1.0/webhelp/index.html
Download the java driver and take a look at it.
>git clone https://github.com/datastax/java-driver.git
>mvn clean install
>mvn eclipse:eclipse
And I import these project with my Eclipse, there are 3 projects cassandra-driver-core, cassandra-driver-dse, cassandra-driver-examples-stress.
I am using the latest driver in the build.sbt
"com.datastax.cassandra" % "cassandra-driver-core" % "2.0.0-rc2"
There is one thing need to pay attention. There are 2 ports, one is old RPC port 9160 which support cassandra-cli and cqlsh.
Another port is New binary port 9042 which support the java library datastax.
Schema Demo
Here is the sample codes for connect to the DB and insert data
package com.sillycat.easycassandraserver.apps
import com.datastax.driver.core.Cluster
import com.datastax.driver.core.exceptions.AlreadyExistsException
import com.datastax.driver.core._
import org.slf4j.LoggerFactory
import scala.collection.JavaConversions._
object CassandraDataStaxSchemaApp extends App{
val log = LoggerFactory.getLogger("CassandraConfig")
val hosts = “carl.sillycat.com"
val nativePort = 9042 //9042, 9160
val keyspaceName = "books"
val columnFamilyName = "books"
val replicationStrategy = "SimpleStrategy"
val replicationFactor = 2
val compactionStrategy = "LeveledCompactionStrategy"
val keyspaceCql = s"""
create keyspace $keyspaceName
with replication = { 'class': '$replicationStrategy', 'replication_factor': $replicationFactor }
"""
val tableCql = s"""
create table $columnFamilyName (
brandCode text,
deviceId text,
unixtime bigint,
notes text,
primary key ((brandCode, deviceId), unixtime)
) with compact storage
and compression = { 'sstable_compression' : '' }
and compaction = { 'class' : '$compactionStrategy', 'sstable_size_in_mb' : 10 }
and clustering order by (unixtime desc)
"""
lazy val cluster: Cluster = {
val b = Cluster.builder().
addContactPoints(hosts.split(","): _*).
withPort(nativePort)
val option = new SocketOptions()
option.setReadTimeoutMillis(12000)
b.withSocketOptions(option)
val c = b.build()
val s = c.connect()
try {
s.execute(keyspaceCql)
} catch {
case x: AlreadyExistsException => log.info(x.getMessage)
}
c
}
lazy val session: Session = {
val s = cluster.connect(keyspaceName)
try {
s.execute(tableCql)
} catch {
case x: AlreadyExistsException => log.info(x.getMessage)
}
s
}
//insert
val insertSQL = session.prepare(
"""
| insert into books ( brandCode, deviceId, unixtime, notes) values ( ?, ?, ?, ? )
""".stripMargin)
session.execute(insertSQL.bind("sillycat","iphone5", 1L:java.lang.Long, "There is a book there."))
session.execute(insertSQL.bind("sillycat","iphone5", 2L:java.lang.Long, "I update the os to 6.0"))
session.execute(insertSQL.bind("sillycat","iphone5", 3L:java.lang.Long, "I update the os to 7.0"))
session.execute(insertSQL.bind("sillycat","android", 1L:java.lang.Long, "I update the os 2.1"))
session.execute(insertSQL.bind("sillycat","itouch", 2L:java.lang.Long, "I update the os 2.2"))
cluster.shutdown()
}
The Query Demo
package com.sillycat.easycassandraserver.apps
import org.slf4j.LoggerFactory
import com.datastax.driver.core.{Session, SocketOptions, Cluster}
import com.datastax.driver.core.exceptions.AlreadyExistsException
import scala.collection.JavaConversions._
object CassandraDataStaxQueryApp extends App{
val log = LoggerFactory.getLogger("CassandraConfig")
val hosts = "carl.sillycat.com"
val nativePort = 9042 //9042, 9160
val keyspaceName = "books"
val columnFamilyName = "books"
val replicationStrategy = "SimpleStrategy"
val replicationFactor = 2
val compactionStrategy = "LeveledCompactionStrategy"
val keyspaceCql = s"""
create keyspace $keyspaceName
with replication = { 'class': '$replicationStrategy', 'replication_factor': $replicationFactor }
"""
val tableCql = s"""
create table $columnFamilyName (
brandCode text,
deviceId text,
unixtime bigint,
notes text,
primary key ((brandCode, deviceId), unixtime)
) with compact storage
and compression = { 'sstable_compression' : '' }
and compaction = { 'class' : '$compactionStrategy', 'sstable_size_in_mb' : 10 }
and clustering order by (unixtime desc)
"""
lazy val cluster: Cluster = {
val b = Cluster.builder().
addContactPoints(hosts.split(","): _*).
withPort(nativePort)
val option = new SocketOptions()
option.setReadTimeoutMillis(12000)
b.withSocketOptions(option)
val c = b.build()
val s = c.connect()
try {
s.execute(keyspaceCql)
} catch {
case x: AlreadyExistsException => log.info(x.getMessage)
}
c
}
lazy val session: Session = {
val s = cluster.connect(keyspaceName)
try {
s.execute(tableCql)
} catch {
case x: AlreadyExistsException => log.info(x.getMessage)
}
s
}
//query1
val querySQL1 = session.prepare("""
select notes from books where brandCode = ? and deviceId = ?
limit 1
""")
val result1 = session.execute(querySQL1.bind("sillycat", "iphone5"))
val resultString1 = result1.one().getString("notes")
println("resultString1 = " + resultString1)
//query2
val querySQL2 = session.prepare("""
select notes from books where brandCode = ? and deviceId = ?
""")
val result2 = session.execute(querySQL2.bind("sillycat", "iphone5"))
result2.all().foreach{ row=>
println("resultString2 = " + row.getString("notes"))
}
cluster.shutdown()
}
References:
http://cassandra.apache.org/doc/cql/CQL.html
http://cassandra.apache.org/download/
http://cassandra.apache.org/doc/cql3/CQL-1.2.html
http://cassandra.apache.org/doc/cql3/CQL.html
Install wget
http://osxdaily.com/2012/05/22/install-wget-mac-os-x/
Cassandra 1
http://sillycat.iteye.com/blog/1870661
http://stackoverflow.com/questions/16783725/error-while-connecting-to-cassandra-using-java-driver-for-apache-cassandra-1-0-f
相关推荐
cassandra-cpp这是一个经过维护的Rust项目,在一个有点理智的cra中公开了DataStax cpp驱动程序,网址为https://github.com/datastax/cpp-driver/。 https://github.com/datastax/cpp-driver/上的驱动程序安装在一个...
一个现代、功能丰富且高度可调的 C/C++ 客户端库,适用于 Apache Cassandra 2.1+,仅使用 Cassandra 的二进制协议和 Cassandra 查询语言 v3。此驱动程序也可以与其他 DataStax 产品一起使用: 特征 异步 API 简单、...
datastax-cassandra-driver-demo 使用 Datastax Cassandra Driver 的演示
datastax的cassandra,Community Edition
刚学cassandra数据库,然后这是安装包,刚学cassandra数据库,然后这是安装包,
本文档详细介绍了Cassandra,DataStax的原理,以及在各种环境下的安装方法。
In 2013 he became the very first certified Cassandra developer, and in 2014 DataStax selected him as an Apache Cassandra MVP. Robbie has been an active speaker and writer in the Cassandra community ...
Pioneering Cassandra developers and Datastax MVPs Russell Bradberry and Eric Lubow walk you through every step of building a real production application that can store enormous amounts of structured, ...
适用于ApacheCassandra:registered:的DataStax Node.js驱动程序一种现代,功能丰富且高度可调的Node.js客户端库,适用于Apache Cassandra和DSE,仅使用Cassandra的二进制协议和Cassandra查询语言。 用于Apache...
一个现代、功能丰富和高度可调优的Cassandra 和DataStax的Python客户端库
Beginning Apache Cassandra Development takes you through the Apache Cassandra NoSQL database and how to build applications that leverage its capabilities. As Java, PHP, Python, and JavaScript are the ...
该库使用 DataStax 官方 c++ 驱动程序在构建 php-cassandra 之前,您应该下载并安装适用于 Apache Cassandra 的 DataStax C++ 驱动程序。 git clone ...
DataStax C# Driver for Apache Cassandra
The rising popularity of Apache Cassandra rests on its ability to handle very large data sets that include hundreds of terabytes -- and that's why this distributed database has been chosen by ...
DevCenter cassandra客户端 DevCenter cassandra客户端 DevCenter cassandra客户端
适用于Apache Cassandra的DataStax驱动程序 一个现代的,且高度可调的Python客户端库,仅使用Cassandra的二进制协议和Cassandra Query Language v3,适用于Apache Cassandra(2.1+)和DataStax Enterprise(4.7+)...
Cassandra is a distributed database that stands out thanks to its robust feature set and intuitive interface, while providing high availability and scalability of a distributed data store. This book ...
Cassandra Java 示例 这个 repo 展示了一堆使用 Apache Cassandra 和 Datastax Java Driver for Apache Cassandra 的例子
Build, manage, and configure high-performing, reliable NoSQL database for your applications with Cassandra Key Features Write programs more efficiently using Cassandra's features with the help of ...