尝试在WSL Ubuntu上运行ScalarDB(环境设置篇)
ScalarDB 是什么?
使用Scalar提供的库可以将非ACID的分布式数据库/存储转换为符合ACID标准的数据库,通过使用像Cassandra这样的分布式数据库,实现了线性可扩展性和高可用性。
最大的特点是可以使用具有强大一致性的ACID事务功能。
请查看 ScalarDB 文档以获取详细信息。
ScalarDB的架构
ScalarDB主要由以下三层组成。
-
- 负责管理无主事务的管理层
-
- 用于数据存储的分布式数据库之间差异的吸收层,即存储抽象化层
- 特定于存储的适配器(目前支持Cassandra)

ScalarDB的数据模型是基于键值对形式的多维地图模型,记录由分区键、聚簇键和值集合组成。

每个值通过由分区键、聚簇键和值的名称构成的主键进行唯一映射。
关于Cassandra
Cassandra是一种NoSQL数据库,具有以下特点。
・无单一故障点的高度可扩展性与可用性
・类似SQL的查询语言,支持通过次要索引进行搜索
・灵活的模式。
不过,还有以下的限制。
・不支持事务和Join操作
・不支持外键,键是不可变的
・键必须是唯一的
・搜索复杂
数据模型由以下要素组成,牺牲了部分数据一致性来实现数据的分散。
-
- 键空间
-
- 顶级命名空间
-
- 列族(表)
-
- 对应关系数据库管理系统的表,是列集合的容器
-
- 分区键
-
- 将数据分散在节点上的键
-
- 值
- 分区键、聚簇键之外的列数据
环境建立
我們接下來要進入正題,試著在本地的Windows10上安裝Ubuntu16.04並運行這個ScalarDB。
以下是所需的组件:
• Oracle JDK 8(OpenJDK 8)或更高版本
• Cassandra 3.11.x(当前最新稳定版本)
• Golang Ver.1.10或更高版本
• Gradle Ver.4.10或更高版本
我们立即安装吧。
Oracle JDK 8(OpenJDK 8) 奥拉克尔JDK 8(开放JDK 8)
(请参考:http://cassandra.apache.org/)
据说Ubuntu的初始状态下具备Java 8的运行环境,但不具备开发环境。
更新软件包列表。
$ sudo apt update
安裝Java 8開發環境
$ sudo apt安裝openjdk-8-jdk
Cassandra 3.11.x

※如果在第3步骤中出现以下错误:
GPG错误:http://www.apache.org 311x InRelease:由于公钥不可用,无法验证以下签名:NO_PUBKEY A278B781FE4B2BDA。
添加公钥
$ sudo apt-key adv –keyserver pool.sks-keyservers.net –recv-key A278B781FE4B2BDA
请再次更新存储库
$ sudo apt-get update
Go语言

Gradle – 构建工具
为了安装Gradle,需要先安装SDKMan。而要安装SDKMan,则需要分别安装zip和unzip。
-
- 压缩和解压缩程序的安装
-
- $ sudo apt install zip unzip
-
- SDKMan的安装
-
- $ curl -s “https://get.sdkman.io” | bash
-
- 详情请参阅
-
- SDKMan的初始设置
-
- $ source “/home/(自己的用户名)/.sdkman/bin/sdkman-init.sh”
-
- 确认SDKMan的安装
-
- $ sdk version
-
- 应显示sdkman 5.0.0+51才为正常
-
- Gradle的安装
-
- $ sdk install gradle 4.10.2
-
- 详情请参阅
-
- 确认Gradle的版本
-
- $ gradle –version
- 应显示Gradle 4.10.2才为正常
ScalarDB的配置
1. 创建设置文件(database.properties)
使用命令行创建一个名为/etc/scalar/的目录,然后使用vi编辑器打开/etc/scalar/database.properties文件。
# Comma separated contact points
scalar.database.contact_points=localhost
# Port number for all the contact points. Default port number for each database is used if empty.
# scalar.database.contact_port=
# Credential information to access the database
scalar.database.username=cassandra
scalar.database.password=cassandra
只需要一种选项:
在「localhost」处写入要连接的终端的IP地址。如果要进行多个连接,则用逗号隔开写入。
为Cassandra创建数据目录。
请用中文将以下内容重新表达,只需要一种版本:
$ sudo mkdir -p /data/cassandra/data
$ sudo mkdir -p /data/cassandra/commitlog
$ sudo mkdir -p /data/cassandra/hints
$ sudo mkdir -p /data/cassandra/saved_caches
请使用管理员权限创建以下目录:
$ sudo mkdir -p /data/cassandra/data
$ sudo mkdir -p /data/cassandra/commitlog
$ sudo mkdir -p /data/cassandra/hints
$ sudo mkdir -p /data/cassandra/saved_caches
更改/data/cassandra文件夹的拥有者为cassandra:cassandra。
使用sudo chown -R cassandra:cassandra /data/cassandra命令实现。
由于这是示例,所以在同一实例上创建,但在生产环境中,预计将在AWS或NFS上创建。
编辑Cassandra的配置文件。
先创建Cassandra配置文件的备份然后进行编辑。
将/etc/cassandra/cassandra.yaml复制到/etc/cassandra/cassandra.yaml.copy:
$ sudo cp /etc/cassandra/cassandra.yaml /etc/cassandra/cassandra.yaml.copy
使用sudo vi命令编辑/etc/cassandra/cassandra.yaml文件:
$ sudo vi /etc/cassandra/cassandra.yaml
commitlog_directoryの設定を変更(196行目)
変更前
# commit log. when running on magnetic HDD, this should be a
# separate spindle than the data directories.
# If not set, the default directory is $CASSANDRA_HOME/data/commitlog.
commitlog_directory: /var/lib/cassandra/commitlog
変更後
# commit log. when running on magnetic HDD, this should be a
# separate spindle than the data directories.
# If not set, the default directory is $CASSANDRA_HOME/data/commitlog.
commitlog_directory: /data/cassandra/commitlog
data_file_directoriesの設定を変更(191行目)
変更前
# Directories where Cassandra should store data on disk. Cassandra
# will spread data evenly across them, subject to the granularity of
# the configured compaction strategy.
# If not set, the default directory is $CASSANDRA_HOME/data/data.
data_file_directories: /var/lib/cassandra/data
変更後
# Directories where Cassandra should store data on disk. Cassandra
# will spread data evenly across them, subject to the granularity of
# the configured compaction strategy.
# If not set, the default directory is $CASSANDRA_HOME/data/data.
data_file_directories: /data/cassandra/data
hints_directoryの設定を変更(71行目)
変更前
# Directory where Cassandra should store hints.
# If not set, the default directory is $CASSANDRA_HOME/data/hints.
hints_directory: /var/lib/cassandra/hints
変更後
# Directory where Cassandra should store hints.
# If not set, the default directory is $CASSANDRA_HOME/data/hints.
hints_directory: /data/cassandra/hints
saved_caches_directoryの設定を変更(368行目)
変更前
# saved caches
# If not set, the default directory is $CASSANDRA_HOME/data/saved_caches.
saved_caches_directory: /var/lib/cassandra/saved_caches
変更後
# saved caches
# If not set, the default directory is $CASSANDRA_HOME/data/saved_caches.
saved_caches_directory: /data/cassandra/saved_caches
設定のコメントアウトを切り替える(379,380,385,386行目)
変更前
# commitlog_sync_batch_window_in_ms milliseconds between fsyncs.
# This window should be kept short because the writer threads will
# be unable to do extra work while waiting. (You may need to increase
# concurrent_writes for the same reason.)
#
# commitlog_sync: batch
# commitlog_sync_batch_window_in_ms: 2
#
# the other option is "periodic" where writes may be acked immediately
# and the CommitLog is simply synced every commitlog_sync_period_in_ms
# milliseconds.
commitlog_sync: periodic
commitlog_sync_period_in_ms: 10000
変更後
# commitlog_sync_batch_window_in_ms milliseconds between fsyncs.
# This window should be kept short because the writer threads will
# be unable to do extra work while waiting. (You may need to increase
# concurrent_writes for the same reason.)
#
commitlog_sync: batch
commitlog_sync_batch_window_in_ms: 2
#
# the other option is "periodic" where writes may be acked immediately
# and the CommitLog is simply synced every commitlog_sync_period_in_ms
# milliseconds.
# commitlog_sync: periodic
# commitlog_sync_period_in_ms: 10000
重新启动Cassandra并进行登录确认
若能成功登录到Cassandra控制台,则表示已成功启动。
如果显示*Cassandra正在运行,则表示成功。
ScalarDB模式工具
这个工具是为ScalarDB数据库生成和加载模式的工具。
它有两个功能:生成器(generator)和加载器(loader)。生成器可以创建存储实现(例如:Cassandra)特定的模式定义文件和元数据定义,加载器可以使用生成器获取模式文件,并创建模式定义到存储中。
这样一来,在对应用程序的数据进行建模时,我们就不再需要考虑存储特定的模式了。
1. 工具下载
将ScalarDB的GitHub仓库进行克隆。
$ cd~/
$ git clone https://github.com/scalar-labs/scalardb.git
2. 使用Gradle配置ScalarDB环境。
使用变量设置路径
$ SCALARDB_HOME=/home/(用户的环境用户名)/scalardb
$ cd $SCALARDB_HOME
执行构建
$ sudo ./gradlew installDist
如果显示“构建成功”,则表示OK
用Go语言make方法
请转到Schema Tools目录并运行make命令。
$ cd tools/schema
$ sudo make
创建样本文件
$ sudo vi emoney-storage.sdbql
REPLICATION FACTOR 1;
CREATE NAMESPACE emoney;
CREATE TABLE emoney.account (
id TEXT PARTITIONKEY,
balance INT,
);
5. 执行测试
尝试启动generator。
$ sudo ./generator emoney-storage.sdbql emoney-storage.cql
确认 emoney-storage.cql 文件已被创建。
进入样品应用程序创建部分。
环境搭建部分已经完成。在示例应用创建部分,我希望实际创建一个示例应用程序,并确认ScalarDB应用程序的运行情况。
在WSL Ubuntu上尝试运行ScalarDB(创建示例应用程序的部分)。