如何编程添加索引到Cassandra 0.7 [英] How to programatically add index to Cassandra 0.7

查看:211
本文介绍了如何编程添加索引到Cassandra 0.7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在 http:// www上运行演示.archano.com / blog / whats-new-cassandra-07-secondary-indexes ,但是结果与在CLI中运行不同。看起来Cassandra只能在添加索引后索引列。

I tried to run the demo on http://www.riptano.com/blog/whats-new-cassandra-07-secondary-indexes programatically, but the results are different from running it in CLI. It seems like Cassandra can only index columns after index is added. All previous data are left unindexed.

完整的源代码如下: -

Full source code are as below:-

public static void main(String[] args) {
    try {
        try {
            transport.open();
        } catch (TTransportException ex) {
            Logger.getLogger(IndexLaterTest.class.getName()).log(Level.SEVERE, null, ex);
            System.exit(1);
        }
        KsDef ksDef = new KsDef();
        ksDef.name = KEYSPACE_NAME;
        ksDef.replication_factor = 1;
        ksDef.strategy_class = "org.apache.cassandra.locator.SimpleStrategy";
        CfDef cfDef = new CfDef(KEYSPACE_NAME, COLUMN_FAMILY_NAME);
        cfDef.comparator_type = "UTF8Type";
        ColumnDef columnDef = new ColumnDef(ByteBuffer.wrap(FULL_NAME.getBytes()), "UTF8Type");

        cfDef.addToColumn_metadata(columnDef);
        ColumnDef columnDef1 = new ColumnDef(ByteBuffer.wrap(BIRTH_DATE.getBytes()), "LongType");
        columnDef1.index_type = IndexType.KEYS;
        cfDef.addToColumn_metadata(columnDef1);
        ksDef.cf_defs = Arrays.asList(cfDef);
        try {
            client.system_add_keyspace(ksDef);

            client.set_keyspace(KEYSPACE_NAME);

            ColumnParent columnParent = new ColumnParent();
            columnParent.column_family = COLUMN_FAMILY_NAME;
            Column column = new Column(ByteBuffer.wrap(FULL_NAME.getBytes()), ByteBuffer.wrap("Brandon Sanderson".getBytes()), System.currentTimeMillis());
            client.insert(ByteBuffer.wrap("bsanderson".getBytes()), columnParent, column, ConsistencyLevel.ONE);
            column.name = ByteBuffer.wrap(BIRTH_DATE.getBytes());
            column.value = ByteBuffer.allocate(8).putLong(1975);
            client.insert(ByteBuffer.wrap("bsanderson".getBytes()), columnParent, column, ConsistencyLevel.ONE);

            column.name = ByteBuffer.wrap(FULL_NAME.getBytes());
            column.value = ByteBuffer.wrap("Patrick Rothfuss".getBytes());
            client.insert(ByteBuffer.wrap("prothfuss".getBytes()), columnParent, column, ConsistencyLevel.ONE);
            column.name = ByteBuffer.wrap(BIRTH_DATE.getBytes());
            column.value = ByteBuffer.allocate(8).putLong(1973);
            client.insert(ByteBuffer.wrap("prothfuss".getBytes()), columnParent, column, ConsistencyLevel.ONE);

            column.name = ByteBuffer.wrap(FULL_NAME.getBytes());
            column.value = ByteBuffer.wrap("Howard Tayler".getBytes());
            client.insert(ByteBuffer.wrap("htayler".getBytes()), columnParent, column, ConsistencyLevel.ONE);
            column.name = ByteBuffer.wrap(BIRTH_DATE.getBytes());
            column.value = ByteBuffer.allocate(8).putLong(1968);
            client.insert(ByteBuffer.wrap("htayler".getBytes()), columnParent, column, ConsistencyLevel.ONE);

            column.name = ByteBuffer.wrap(STATE.getBytes());
            column.value = ByteBuffer.wrap("WI".getBytes());
            client.insert(ByteBuffer.wrap("prothfuss".getBytes()), columnParent, column, ConsistencyLevel.ONE);
            column.value = ByteBuffer.wrap("UT".getBytes());
            client.insert(ByteBuffer.wrap("htayler".getBytes()), columnParent, column, ConsistencyLevel.ONE);

            KsDef ks = client.describe_keyspace(KEYSPACE_NAME);
            cfDef = new CfDef(ks.cf_defs.get(0));
            ColumnDef columnDef2 = new ColumnDef(ByteBuffer.wrap(STATE.getBytes()), "UTF8Type");
            columnDef2.index_type = IndexType.KEYS;
            cfDef.setColumn_metadata(Arrays.asList(columnDef, columnDef1, columnDef2));

            client.system_update_column_family(cfDef);
            Thread.sleep(120000);//give cassandra enough time to build the index.
            client.insert(ByteBuffer.wrap("bsanderson".getBytes()), columnParent, column, ConsistencyLevel.ONE);

            IndexClause indexClause = new IndexClause();
            indexClause.start_key = ByteBuffer.allocate(0);
            IndexExpression indexExpression = new IndexExpression();
            indexExpression.column_name = ByteBuffer.wrap(STATE.getBytes());
            indexExpression.value = ByteBuffer.wrap("UT".getBytes());
            indexExpression.op = IndexOperator.EQ;
            indexClause.addToExpressions(indexExpression);
            SliceRange sliceRange = new SliceRange();
            sliceRange.count = 10;
            sliceRange.start = ByteBuffer.allocate(0);
            sliceRange.finish = ByteBuffer.allocate(0);
            sliceRange.reversed = false;
            SlicePredicate slicePredicate = new SlicePredicate();
            slicePredicate.slice_range = sliceRange;
            List<KeySlice> keys = client.get_indexed_slices(columnParent, indexClause, slicePredicate, ConsistencyLevel.ONE);
            if (!keys.isEmpty()) {
                System.out.println("expecting: bsanderson htayler");
                System.out.print("actual: ");
                for (KeySlice key : keys) {
                    System.out.print(new String(key.getKey()) + " ");
                }
            } else {
                System.out.println("failed to find indexed item");
            }
        } catch (Exception ex) {
            Logger.getLogger(IndexLaterTest.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                client.system_drop_keyspace(KEYSPACE_NAME);
            } catch (Exception ex) {
                Logger.getLogger(IndexLaterTest.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    } finally {
        transport.close();
    }
}

结果是: -

expecting: bsanderson htayler
actual: bsanderson 


推荐答案

用于记录的目的。

ColumnDef columnDef1 = new ColumnDef(ByteBuffer.wrap(BIRTH_DATE.getBytes()), "LongType");
columnDef1.index_type = IndexType.KEYS;

单独定义index_type是不够的。您还需要设置一个index_name以使其工作。

Defining the index_type alone is not enough. You need to set a index_name as well for it to work.

这篇关于如何编程添加索引到Cassandra 0.7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆