java 操作cassandra super column 插入问题
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;
}
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你这哪是superColumn 你这是批量插入Column集合
类型转换错误
你这问得太有水平了,你等后面的人来吧。
那如何解决 ?一天了 都没有解决了
哥哥,错误信息提示得很明白了
类型转换出错:ArrayList不能转换为Map!!!
(Map<String, List<Mutation>>) cslist
cslist明明是个List,你非要别人死命变成Map,结果,变死了吧。