安装NoSQL Cassandra2.2 yum,并尝试使用CQL!

尝试使用yum在Cassandra2.2 系中进行安装和操作。

我不太明白,但我会尝试安装和使用Cassandra。

安装 Java

rpm -ivh jdk-8u65-linux-x64.rpm

Cassandra yum仓库文件的配置

cat << '_EOT_' > /etc/yum.repos.d/datastax.repo 
[datastax] 
name = DataStax Repo for Apache Cassandra
baseurl = http://rpm.datastax.com/community
enabled = 1
gpgcheck = 0
_EOT_

Cassandra的安装

我不知道Cassandra从哪里开始到哪里结束,但是感觉像这样。

yum install dsc22
yum install cassandra22-tools

卡珊德拉的开始

/etc/init.d/cassandra start
------------------------------------------------------------------------------

Starting Cassandra: OK
/etc/init.d/cassandra status                          
------------------------------------------------------------------------------
cassandra (pid  22545) を実行中...

启动cqlsh

这是一个可以像使用SQL那样操作Cassandra的工具。类似于Hiveql吧?

cqlsh                                                 
------------------------------------------------------------------------------
Connected to Test Cluster at 127.0.0.1:9042.
[cqlsh 5.0.1 | Cassandra 2.2.3 | CQL spec 3.3.1 | Native protocol v4]
Use HELP for help.
cqlsh> 

CQL 操作

创建模式(创建键空间)。

暫時先不考慮太深入的事情。如果你能把它比作在MySQL中建立一個資料庫的動作,我會很感激。

cqlsh> 
cqlsh> create keyspace logs with replication = {'class' : 'SimpleStrategy',
 'replication_factor' : 1};

确认已经创建了Keyspace。

如果以MySQL的说法,你可以将其理解为”show create database logs”。

cqlsh> 
describe keyspace logs ;
------------------------------------------------------------------------------
CREATE KEYSPACE logs WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'}  AND durable_writes = true;

创建表

    MySQL
CREATE TABLE `customer_action_logs` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `updated_at` datetime NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT 'UTC-DATE',
  `domain` varchar(128) DEFAULT NULL COMMENT 'ex: www.yourdomain.com',
  `uri` varchar(500) DEFAULT NULL,
  `member_id` int(11) DEFAULT '0' COMMENT 'ログイン前であれば、0',
  `user_agent` varchar(255) DEFAULT NULL,
  `referer` varchar(500) DEFAULT NULL,
  `session_id` varchar(200) DEFAULT NULL COMMENT 'セッションID',
  PRIMARY KEY (`id`),
  KEY `ix01_customer_action_logs` (`member_id`),
  KEY `ix02_customer_action_logs` (`updated_at`)
) ENGINE=InnoDB AUTO_INCREMENT=173735587 DEFAULT CHARSET=utf8 ;

    cql
Use logs ;
cqlsh:logs>

CREATE TABLE customer_action_logs (
  id uuid PRIMARY KEY,
  updated_at timestamp,
  domain varchar,
  uri varchar,
  member_id int,
  user_agent varchar,
  referer varchar,
  session_id varchar
) ; 
CREATE INDEX ix01_customer_action_logs ON customer_action_logs ( member_id );
CREATE INDEX ix02_customer_action_logs ON customer_action_logs ( updated_at );

确认表格是否创建成功

cqlsh:logs> describe table customer_action_logs ;

CREATE TABLE logs.customer_action_logs (
    id uuid PRIMARY KEY,
    domain text,
    member_id int,
    referer text,
    session_id text,
    updated_at timestamp,
    uri text,
    user_agent text
) WITH bloom_filter_fp_chance = 0.01
    AND caching = '{"keys":"ALL", "rows_per_partition":"NONE"}'
    AND comment = ''
    AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy'}
    AND compression = {'sstable_compression': 'org.apache.cassandra.io.compress.LZ4Compressor'}
    AND dclocal_read_repair_chance = 0.1
    AND default_time_to_live = 0
    AND gc_grace_seconds = 864000
    AND max_index_interval = 2048
    AND memtable_flush_period_in_ms = 0
    AND min_index_interval = 128
    AND read_repair_chance = 0.0
    AND speculative_retry = '99.0PERCENTILE';
CREATE INDEX ix01_customer_action_logs ON logs.customer_action_logs (member_id);
CREATE INDEX ix02_customer_action_logs ON logs.customer_action_logs (updated_at);

试着插入

我对日期和时间的设置有些犹豫,但其他方面应该和SQL的插入语句一样吧?另外,自动递增是通过UUID属性进行创建的,关键是如何确保唯一性的。

INSERT INTO customer_action_logs 
(
id,
domain,
member_id,
referer,
session_id,
updated_at,
uri,user_agent
) VALUES (
now(),
'qiita.com',
12345678,
'qiita.ref.com',
'session010101',
dateof(now()),
'/hit/items/3151208b7f01aaa1ea9d',
'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko'
) ;

INSERT INTO customer_action_logs 
(
id,
domain,
member_id,
referer,
session_id,
updated_at,
uri,user_agent
) VALUES (
now(),
'qiita.com',
121221122,
'qiita.ref.com',
'session010101',
dateof(now()),
'/hit/items/3151208b7f01aaa1ea9d',
'Mozilla/4.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko'
) ;

嗯,基本上就是可以像這樣進行操作。Cassandra在集群中發揮威力,所以單獨安裝沒有意義,但是能夠在NoSQL中進行類似SQL的操作真的很方便,你不認為嗎?所以下次如果心情好的話,我想寫一下與MariaDB的連接部分。

广告
将在 10 秒后关闭
bannerAds