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
相关推荐
适用于 Apache Cassandra 的 DataStax Python 驱动程序 Apache Cassandra 的 DataStax 驱动程序一个现代的、功能丰富且高度可调的 Python 客户端库,适用于 Apache Cassandra(2.1+)和 DataStax Enterprise(4.7+)...
Scylladb或Cassandra 客户端工具 DevCenter
Datastax是一家专注于Cassandra的公司,他们提供了商业版本的Cassandra,同时维护了一个社区版本——datastax-cassandra。社区版通常包含了最新的开源Cassandra特性,适合开发者进行测试、学习和轻量级生产环境使用...
总而言之,Cassandra DataStax原理及安装的知识点涵盖了Cassandra的开源和DataStax的商业版DSE的介绍,它们的安装方法,以及如何使用OpsCenter、DevCenter等工具来管理和使用Cassandra集群。文档通过详细的步骤和...
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 简单、...
刚学cassandra数据库,然后这是安装包,刚学cassandra数据库,然后这是安装包,
5. **Tunable Consistency**:Cassandra提供了可调整的一致性级别,允许开发者在数据一致性和可用性之间做出权衡。例如,QUORUM、ONE、EACH_QUORUM等一致性级别。 6. **DevCenter**:DevCenter是DataStax提供的一款...
Python驱动的Cassandra和DataStax客户端库是一个强大的工具,为Python开发者提供了与Apache Cassandra数据库和DataStax Enterprise (DSE)交互的能力。这个库,通常被称为`cassandra-driver`,是一个高性能、全面特性...
**数据存储与Cassandra驱动器:Datastax Cassandra Driver 演示** 在现代大数据处理领域,Apache Cassandra 是一种广泛采用的分布式数据库系统,尤其适用于处理大规模、高并发的数据读写场景。Datastax Cassandra ...
适用于ApacheCassandra:registered:的DataStax Node.js驱动程序一种现代,功能丰富且高度可调的Node.js客户端库,适用于Apache Cassandra和DSE,仅使用Cassandra的二进制协议和Cassandra查询语言。 用于Apache...
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#驱动程序用于Apache Cassandra的深度解析》 Apache Cassandra,作为一个高度可扩展的分布式NoSQL数据库,已经成为大数据存储领域的热门选择。而DataStax C#驱动程序则是Cassandra与C#应用程序之间的...
在本示例中,我们将深入探讨如何使用 Java 和 Datastax Java 驱动程序与 Cassandra 进行交互。 Datastax Java 驱动程序是 Cassandra 官方推荐的 Java 开发工具,它提供了高效、健壮且易于使用的 API,使得 Java ...
该库使用 DataStax 官方 c++ 驱动程序在构建 php-cassandra 之前,您应该下载并安装适用于 Apache Cassandra 的 DataStax C++ 驱动程序。 git clone ...
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 ...
5. **版本控制**:通过DevCenter,用户可以跟踪和管理Cassandra模式的版本。这在团队协作中非常有用,可以确保在多个人同时修改模式时保持一致性。 6. **脚本和工作流**:DevCenter支持创建和运行CQL脚本,可以用于...