在 cassandra-cli 中如何获取表中的所有列名以及如何在 Java 中使用 hector 获取它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18374285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
in cassandra-cli how to get all column names in a table and how to get it using hector in java?
提问by N D Thokare
I'm trying to get column names but could not get way to get only column names.
我正在尝试获取列名,但无法仅获取列名。
In cli I executed command describe table nodes
, it returned the result:
在 cli 中我执行了 command describe table nodes
,它返回了结果:
CREATE TABLE nodes (
key text PRIMARY KEY,
id text,
scores text,
topic1 text,
topic2 text,
topic3 text,
topic4 text,
topics text
) WITH COMPACT STORAGE AND
bloom_filter_fp_chance=0.010000 AND
caching='KEYS_ONLY' AND
comment='' AND
dclocal_read_repair_chance=0.000000 AND
gc_grace_seconds=864000 AND
read_repair_chance=0.100000 AND
replicate_on_write='true' AND
populate_io_cache_on_flush='false' AND
compaction={'class': 'SizeTieredCompactionStrategy'} AND
compression={'sstable_compression': 'SnappyCompressor'};
CREATE INDEX idx_nodes_id ON nodes (id);
As given in this question, I tries using following command in cli:
正如在这个问题中给出的,我尝试在 cli 中使用以下命令:
SELECT column_name FROM system.schema_columnfamilies WHERE keyspace_name = 'ianew' AND columnfamily_name = 'nodes';
but it gave the error:
但它给出了错误:
Bad Request: Undefined name column_name in selection clause
Perhaps you meant to use CQL 2? Try using the -2 option when starting cqlsh.
Then I tried with:
然后我尝试:
SELECT * FROM system.schema_columnfamilies WHERE keyspace_name = 'ianew' AND columnfamily_name = 'nodes';
It returned all following things:
它返回了以下所有内容:
keyspace_name | columnfamily_name | bloom_filter_fp_chance | caching | column_aliases | comment | compaction_strategy_class | compaction_strategy_options | comparator | compression_parameters | default_read_consistency | default_validator | default_write_consistency | gc_grace_seconds | id | key_alias | key_aliases | key_validator | local_read_repair_chance | max_compaction_threshold | min_compaction_threshold | populate_io_cache_on_flush | read_repair_chance | replicate_on_write | subcomparator | type | value_alias
---------------+-------------------+------------------------+-----------+----------------+---------+-----------------------------------------------------------------+-----------------------------+------------------------------------------+-----------------------------------------------------------------------------+--------------------------+------------------------------------------+---------------------------+------------------+------+-----------+-------------+------------------------------------------+--------------------------+--------------------------+--------------------------+----------------------------+--------------------+--------------------+---------------+----------+-------------
ianew | nodes | null | KEYS_ONLY | [] | | org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy | {} | org.apache.cassandra.db.marshal.UTF8Type | {"sstable_compression":"org.apache.cassandra.io.compress.SnappyCompressor"} | null | org.apache.cassandra.db.marshal.UTF8Type | null | 864000 | null | null | [] | org.apache.cassandra.db.marshal.UTF8Type | 0 | 32 | 4 | False | 0.1 | True | null | Standard | null
As given in this post, I tried using hector in java:
正如这篇文章中给出的,我尝试在 Java 中使用 hector:
SliceQuery<String, String, String> query = HFactory.createSliceQuery(keyspace, StringSerializer.get(), StringSerializer.get(), StringSerializer.get());
query.setColumnFamily(columnFamilyName);
query.setKey("key");
query.setRange(null, null, false, Integer.MAX_VALUE);
ColumnSliceIterator<String, String, String> iterator = new ColumnSliceIterator<String, String, String>(query, null, "\uFFFF", false);
while (iterator.hasNext()) {
HColumnImpl<String, String> column = (HColumnImpl<String, String>) iterator.next();
System.out.println("Column name = " + column.getName() + "; Column value = " + column.getValue());
colNames.add(column.getName());
}
but it returned with no results.
但它返回没有结果。
I want output to be something like:
我希望输出是这样的:
TABLE nodes:
Columns: key text PRIMARY KEY, id text, scores text, topic1 text, topic2 text, topic3 text, topic4 text, topics text
and similar result through Hector.
和通过赫克托获得的类似结果。
Versions I'm using:
我正在使用的版本:
[cqlsh 2.3.0 | Cassandra 1.2.4 | CQL spec 3.0.0 | Thrift protocol 19.35.0]
回答by Lyuben Todorov
In cassandra 1.1 that would indeed work, however the schema_columnfamilies
column family has been modified since then.
在 cassandra 1.1 中确实可以工作,但是schema_columnfamilies
从那时起列族已被修改。
Bad Request: Undefined name column_name in selection clause
错误请求:选择子句中未定义名称 column_name
In Cassandra 1.2.x information about the columns lives in a separate keyspace called schema_columnswith the following schema:
在 Cassandra 1.2.x 中,有关列的信息位于名为schema_columns的单独键空间中,其架构如下:
CREATE TABLE schema_columns (
keyspace_name text,
columnfamily_name text,
column_name text,
component_index int,
index_name text,
index_options text,
index_type text,
validator text,
PRIMARY KEY (keyspace_name, columnfamily_name, column_name)
);
Try something along these lines:
尝试以下方法:
SELECT * FROM system.schema_columns
WHERE keyspace_name = 'ianew' AND columnfamily_name = 'nodes';
Documentationon what the contents of the system keyspace.
关于系统密钥空间内容的文档。