InvalidTypeException:CQL类型文本的值1的类型无效,应为类java.lang.String,但应为类[Ljava.lang.Object;已提供

InvalidTypeException:CQL类型文本的值1的类型无效,应为类java.lang.String,但应为类[Ljava.lang.Object;已提供,java,cassandra,cql,datastax-java-driver,Java,Cassandra,Cql,Datastax Java Driver,我试图使用Datastax Java驱动程序插入Cassandra数据库。但每次我在prBatchInsert.bind行中遇到以下异常- com.datastax.driver.core.exceptions.InvalidTypeException: Invalid type for value 1 of CQL type text, expecting class java.lang.String but class [Ljava.lang.Object; provided 下面是我的方

我试图使用Datastax Java驱动程序插入Cassandra数据库。但每次我在
prBatchInsert.bind
行中遇到以下异常-

com.datastax.driver.core.exceptions.InvalidTypeException: Invalid type for value 1 of CQL type text, expecting class java.lang.String but class [Ljava.lang.Object; provided
下面是我的方法,它接受
userId
作为输入,
attributes
作为
Map
,其中包含
作为我的
列名
,值作为该列的实际值

public void upsertAttributes(final String userId, final Map<String, String> attributes, final String columnFamily) {

    try {
        Set<String> keys = attributes.keySet();
        StringBuilder sqlPart1 = new StringBuilder(); //StringBuilder.append() is faster than concatenating Strings in a loop
        StringBuilder sqlPart2 = new StringBuilder();

        sqlPart1.append("INSERT INTO " + columnFamily + "(USER_ID ");
        sqlPart2.append(") VALUES ( ?");

        for (String k : keys) {
            sqlPart1.append(", "+k); //append each key
            sqlPart2.append(", ?");  //append an unknown value for each key
        }
        sqlPart2.append(") "); //Last parenthesis (and space?)
        String sql = sqlPart1.toString()+sqlPart2.toString();

        CassandraDatastaxConnection.getInstance();
        PreparedStatement prBatchInsert = CassandraDatastaxConnection.getSession().prepare(sql);
        prBatchInsert.setConsistencyLevel(ConsistencyLevel.ONE);        

        // this line is giving me an exception
        BoundStatement query = prBatchInsert.bind(userId, attributes.values().toArray(new Object[attributes.size()])); //Vararg methods can take an array (might need to cast it to String[]?).

        CassandraDatastaxConnection.getSession().executeAsync(query);

    } catch (InvalidQueryException e) {
        LOG.error("Invalid Query Exception in CassandraDatastaxClient::upsertAttributes "+e);
    } catch (Exception e) {
        LOG.error("Exception in CassandraDatastaxClient::upsertAttributes "+e);
    }
}
public void upserttributes(最终字符串userId、最终映射属性、最终字符串columnFamily){
试一试{
Set keys=attributes.keySet();
StringBuilder sqlPart1=new StringBuilder();//StringBuilder.append()比在循环中串联字符串更快
StringBuilder sqlPart2=新的StringBuilder();
sqlPart1.append(“插入到”+columnFamily+“(用户ID”);
sqlPart2.附加(“)值(?”;
用于(字符串k:键){
sqlPart1.append(“,”+k);//追加每个键
sqlPart2.append(“,?”;//为每个键追加一个未知值
}
sqlPart2.append(“)”;//最后一个括号(和空格?)
字符串sql=sqlPart1.toString()+sqlPart2.toString();
CassandRadataSaxConnection.getInstance();
PreparedStatement prBatchInsert=CassandRadataTaxaxConnection.getSession().prepare(sql);
prBatchInsert.SetConsistenceLevel(ConsistenceLevel.ONE);
//这一行给了我一个例外
BoundStatement query=prBatchInsert.bind(userId,attributes.values().toArray(新对象[attributes.size()]);//Vararg方法可以接受数组(可能需要将其转换为字符串[]?)。
CassandRadataSaxConnection.getSession().executeAsync(查询);
}捕获(InvalidQueryException e){
LOG.error(“CassandraDatastaxClient::upsertAttributes中的查询异常无效”+e);
}捕获(例外e){
LOG.error(“CassandraDatastaxClient::upsertAttributes中的异常”+e);
}
}

我在这里做错了什么?有什么想法吗?

您需要将userId字符串组合到对象数组中,因为您现在传递它的方式被视为
[string,object array]
,java不喜欢这样

// instead of 
// BoundStatement query = prBatchInsert.bind(userId, attributes.values().toArray(new Object[attributes.size()])); 

// try, first combining the uid to the map of values 
Collection<Object> params = new ArrayList<Object>(attributes.size() + 1);
params.add(userId);
for(Object o : attributes.values().toArray())
    params.add(o);

// then passing in the new collection as an array
BoundStatement query = prBatchInsert.bind(params.toArray());
//而不是
//BoundStatement query=prBatchInsert.bind(userId,attributes.values().toArray(新对象[attributes.size()]);
//请尝试,首先将uid组合到值映射
集合参数=新的ArrayList(attributes.size()+1);
参数add(userId);
对于(对象o:attributes.values().toArray())
参数添加(o);
//然后将新集合作为数组传入
BoundStatement query=prBatchInsert.bind(params.toArray());

您需要将userId字符串组合到对象数组中,因为您现在传递它的方式被视为
[string,object array]
,java不喜欢这样

// instead of 
// BoundStatement query = prBatchInsert.bind(userId, attributes.values().toArray(new Object[attributes.size()])); 

// try, first combining the uid to the map of values 
Collection<Object> params = new ArrayList<Object>(attributes.size() + 1);
params.add(userId);
for(Object o : attributes.values().toArray())
    params.add(o);

// then passing in the new collection as an array
BoundStatement query = prBatchInsert.bind(params.toArray());
//而不是
//BoundStatement query=prBatchInsert.bind(userId,attributes.values().toArray(新对象[attributes.size()]);
//请尝试,首先将uid组合到值映射
集合参数=新的ArrayList(attributes.size()+1);
参数add(userId);
对于(对象o:attributes.values().toArray())
参数添加(o);
//然后将新集合作为数组传入
BoundStatement query=prBatchInsert.bind(params.toArray());