cassandra 跑一个节点

本文详细介绍了如何在单台机器上部署Cassandra并使用其基本命令行界面CLI进行操作,包括安装、配置、创建键空间、列族、添加数据、查询等关键步骤。教程覆盖了从入门到实践的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

跑一个节点  下载  http://cassandra.apache.org/download/

 

Cassandra 往往以机器集群的方式部署,但是在单台机器上也能跑得很好。为避免一下就要对付巨大的复杂系统,玩单机是入门良径。

现在还没有安装程序,最简单的方法就是从解压的目录1 或者 SVN 检出(参见: 选择一个版本)目录直接运行 Canssandra。如果你下载的不是二进制版本,你需要先使用 ant 编译它。

发布版的样例设置 conf/storage-conf.xml 包含了一些比较推荐的默认值和注释,但是你要保证 CommitLogDirectoryDataFileDirectories 都存在。 另外,最好也花点时间检查一下 conf/log4j.properties,确保配置中指定的目录都存在。

Mac OS X 的一些用户可能会在运行 Java 6 的时候遇到困难。如果你持续获取 Apple 的最新更新,Java 6 就已经安装好了 (包含于 Mac OS X 10.5 Update 1 中)。 不幸的是,Apple 在默认情况下是不使用它的。 你需要做的是修改环境变量 JAVA_HOME/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home,并且把 /System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/bin 添加到环境变量 PATH 中。

万事俱备,只欠东风,现在你可以在命令行中运行 bin/cassandra -f2 来启动 Cassandra 了! 服务应该在前台启动(非后台方式),并且把日志打印到标准输出上。如果你没看到吓人的词汇(如 "error" 或者 "fatal"), 或者像 java stack trace 之类的东西, 那么你就成功了。请随意尝试 CassandraCliThriftInterface 的例子。遇到问题莫恐慌,冷静的看看 出岔子了!

 

 

 

 

 

 

 

 

Cassandra ships with a very basic interactive command line interface. Using the CLI you can connect to remote nodes in the cluster to create or update your schema and set and retrieve records.

The examples below have been tested with Cassandra 1.0.6. For instructions for previous versions of Cassandra see one of: CassandraCli07, CassandraCli06.

You may also be interested in the DataStax documentation which has excellent coverage on this topic.

 

Starting the CLI

 

You can start the CLI using the bin/cassandra-cli script in your Cassandra installation (bin\cassandra-cli.bat on windows). If you are evaluating a local cassandra node then be sure that it has been correctly configured and successfully started before starting the CLI.

If successful you will see output similar to this:

Welcome to cassandra CLI.

Type 'help;' or '?' for help. Type 'quit;' or 'exit;' to quit.

 

You must then specify a system to connect to:

connect localhost/9160;

 

 

Creating a Keyspace

 

We first create a keyspace to run our examples in.

create keyspace Twissandra;

 

 

Selecting the keyspace to user

 

We must then select our example keyspace as our new context before we can run any queries.

use Twissandra;

 

 

To Create A Column

 

We can then create a column to play with.

create column family User with comparator = UTF8Type;

 

For the later examples to work you must also update the schema using the following command. This will set the return type for the first and last name to make them human readable. It will also add and index for the age field so that you filter your gets using the Users name field.

update column family User with
        column_metadata =
        [
        {column_name: first, validation_class: UTF8Type},
        {column_name: last, validation_class: UTF8Type},
        {column_name: age, validation_class: UTF8Type, index_type: KEYS}
        ];

 

 

To Add Data

 

To add data we want to into our new column we must first specify our default key type otherwise we would have to specify it for each key using the format [utf8('keyname')] this is probably advisable if you have mixed key types but makes simple cases harder to read.

So we run the command below, which will last the length of you cli session. On quitting and restarting we must run it again.

assume User keys as utf8;

 

and then we add our data.

set User['jsmith']['first'] = 'John';
set User['jsmith']['last'] = 'Smith';
set User['jsmith']['age'] = '38';

 

If you get the error like this cannot parse 'John' as hex bytes, then it likely you either haven't set your default key type or you haven't updated your schema as in the create column example.

The set command uses API#insert

To Update Data

 

If we need to update a value we simply set it again.

set User['jsmith']['first'] = 'Jack';

 

 

To Get Data

 

Now let's read back the jsmith row to see what it contains:

get User['jsmith'];

 

The get command uses API#get_slice

 

To Query Data

 

 

get User where age = '12';

 

 

For help

 

 

help;

 

 

To Quit

 

 

quit;

 

 

To Execute Script

 

 

bin/cassandra-cli -host localhost -port 9160 -f script.txt
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值