InvalidTypeException: Invalid type for value 1 of CQL type text, expecting class java.lang.String but class [Ljava.lang.Object; provided

StackOverflow https://stackoverflow.com/questions/20446515

Question

I am trying to insert into Cassandra database using Datastax Java driver. But everytime I am getting below exception at prBatchInsert.bind line-

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

Below is my method which accepts userId as the input and attributes as the Map which contains key as my Column Name and value as the actual value of that column

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);
    }
}

What wrong I am doing here? Any thoughts?

Était-ce utile?

La solution

You need to combine the userId string to the object array, because the way you are passing it now is being seen as [string, object array] and java doesn't like that.

// 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());
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top