java 操作cassandra super column 插入问题

jy34521 发布于 2012/04/26 09:17
阅读 1K+
收藏 0
package com.cassandra.demo;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.cassandra.thrift.Cassandra;
import org.apache.cassandra.thrift.Column;
import org.apache.cassandra.thrift.ColumnOrSuperColumn;
import org.apache.cassandra.thrift.ColumnPath;
import org.apache.cassandra.thrift.ConsistencyLevel;
import org.apache.cassandra.thrift.Mutation;
import org.apache.cassandra.thrift.SuperColumn;
import org.apache.cassandra.utils.ByteBufferUtil;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

public class SuperColumnDemo {
    public String keyspace = "keyspace2";
    public String columnFamily = "super2";
    public String keyUserID = "1";
    public long time = System.currentTimeMillis();

    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        SuperColumnDemo demo = new SuperColumnDemo();
        demo.insertSuperColumn();
        //demo.getSuper();
    }

    /**
     * 读取SuperColumn
     *
     * @throws Exception
     */
    public void getSuper() throws Exception {
        TTransport tt = new TSocket("192.168.2.33", 9160);
        TProtocol proto = new TBinaryProtocol(tt);
        Cassandra.Client client = new Cassandra.Client(proto);
        tt.open();
        ColumnPath path = new ColumnPath(columnFamily);
        path.setSuper_column("address".getBytes());
        ColumnOrSuperColumn s =client.get(Translater.toByteBuffer(keyUserID), path, ConsistencyLevel.ONE);
        System.out.println(new String(s.super_column.columns.get(0).getName(),"utf8"));
        System.out.println(new String(s.super_column.columns.get(0).getValue(), "utf8"));
        System.out.println(new String(s.super_column.columns.get(1).getName(), "utf8"));
        System.out.println(new String(s.super_column.columns.get(1).getValue(), "utf8"));
    }

    /**
     * create column family super2
        with column_type = 'Super'
     * 插入SuperColumn
     *
     * @throws Exception
     */
    public void insertSuperColumn() throws Exception {
        TTransport tt = new TSocket("192.168.2.33", 9160);
        TProtocol proto = new TBinaryProtocol(tt);
        Cassandra.Client client = new Cassandra.Client(proto);
        tt.open();
       
        Map<ByteBuffer, Map<String,List<Mutation>>> mutationMap = getMap(); 
        client.batch_mutate(mutationMap, ConsistencyLevel.ONE);
    }

    private Map<ByteBuffer, Map<String,List<Mutation>>> getMap() {
        Map<ByteBuffer, Map<String, List<Mutation>>> mutationMap ;
        mutationMap= new  HashMap<ByteBuffer,Map<String,List<Mutation>>>();
       
        List<ColumnOrSuperColumn> cslist = new ArrayList<ColumnOrSuperColumn>();
        long timestamp = System.currentTimeMillis();
        // Create the username column.
        ColumnOrSuperColumn c = new ColumnOrSuperColumn();
        c.setColumn(new Column().setName(ByteBufferUtil.bytes("username"))
                .setValue(ByteBufferUtil.bytes("mike"))
                .setTimestamp(System.currentTimeMillis()));
        cslist.add(c);

         

        // Create the password column.
        ColumnOrSuperColumn cpsw = new ColumnOrSuperColumn();
        cpsw.setColumn(new Column().setName(ByteBufferUtil.bytes("password"))
                .setValue(ByteBufferUtil.bytes("smj"))
                .setTimestamp(System.currentTimeMillis()));
        cslist.add(cpsw);


        // Create the email column.
        ColumnOrSuperColumn cemail = new ColumnOrSuperColumn();
        c.setColumn(new Column().setName(ByteBufferUtil.bytes("email"))
                .setValue(ByteBufferUtil.bytes("smj34521@163.com"))
                .setTimestamp(System.currentTimeMillis()));
        cslist.add(c);
        mutationMap.put(Translater.toByteBuffer(columnFamily),(Map<String, List<Mutation>>) cslist);
        return mutationMap;
    }

}

 

错误信息:Exception in thread "main" java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.util.Map
    at com.cassandra.demo.SuperColumnDemo.getMap(SuperColumnDemo.java:125)
    at com.cassandra.demo.SuperColumnDemo.insertSuperColumn(SuperColumnDemo.java:70)
    at com.cassandra.demo.SuperColumnDemo.main(SuperColumnDemo.java:34)


加载中
0
Andre.Z
Andre.Z

哥哥,错误信息提示得很明白了
类型转换出错:ArrayList不能转换为Map!!!

(Map<String, List<Mutation>>) cslist

cslist明明是个List,你非要别人死命变成Map,结果,变死了吧。

0
jy34521
jy34521
那如何解决 ?一天了 都没有解决了
Andre.Z
Andre.Z
你这问得太有水平了,你等后面的人来吧。
0
whaon
whaon
类型转换错误
0
blankHeart
blankHeart
你这哪是superColumn    你这是批量插入Column集合
返回顶部
顶部