BoundStatement query = prBatchInsert.bind(userId, attributes.values().toArray(new *String*[attributes.size()]))


On 12/07/2013 03:59 PM, Techy Teck wrote:
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?

Reply via email to