0

1 つのノード クラスターをローカルにセットアップしました。そして今、Cassandra からデータを読み込もうとしています。Astyanax(CassandraのNetflixクライアント)は初めてです。

現在、これまでに見たのは、行キーでデータベースを要求できることです。行キーの意味に基づいて、必要ではないすべての列を取得できます。

しかし、私が探しているのは、行キーといくつかの列名を持つことです。その行キーに基づいて、それらの列のみを取得する必要があります。このようなもの-

SELECT colA, colB from table1 where rowkey = "222";

以下は、行キーに基づいてすべての列名を取得する方法です。行キーを指定して選択した列のみを取得するにはどうすればよいですか?

public void read(final String userId, final Collection<String> columnNames) {

    OperationResult<ColumnList<String>> result;
    try {
        result = CassandraConnection.getInstance().getKeyspace().prepareQuery(CassandraConnection.getInstance().getEmp_cf())
                .getKey(userId)
                .execute();

        ColumnList<String> cols = result.getResult();

        for(Iterator<Column<String>> i = cols.iterator(); i.hasNext(); ) {
            Column<String> c = i.next();
            Object v = null;
            if(c.getName().endsWith("id")) // type induction hack
                v = c.getIntegerValue();
            else
                v = c.getStringValue();
            System.out.println("- col: '"+c.getName()+"': "+v);
        }


    } catch (ConnectionException e) {
        System.out.println("failed to read from C*" +e);
        throw new RuntimeException("failed to read from C*", e);
    }


}

上記のコードでCollection<String> columnNamesは、リクエストしたい列名がほとんどありません。

上記の方法でどのような変更を加える必要があるか教えてもらえますか?

4

1 に答える 1