Example #1
0
  @Override
  public void write(String keySpace, CassandraProxyClient client, JobConf jc) throws IOException {
    ConsistencyLevel flevel = getConsistencyLevel(jc);

    List<ByteBuffer> values = new ArrayList<ByteBuffer>();

    StringBuilder valuesBuilder = new StringBuilder(" VALUES (");
    StringBuilder queryBuilder = new StringBuilder("INSERT INTO ");
    queryBuilder.append(jc.get(AbstractCassandraSerDe.CASSANDRA_CF_NAME));
    queryBuilder.append("(");
    Iterator<CqlColumn> iter = columns.iterator();
    while (iter.hasNext()) {
      CqlColumn column = iter.next();
      String columnName = new String(column.getColumn());
      queryBuilder.append(columnName);
      valuesBuilder.append("?");
      values.add(ByteBuffer.wrap(column.getValue()));
      if (iter.hasNext()) {
        queryBuilder.append(",");
        valuesBuilder.append(",");
      }
    }
    queryBuilder.append(")");
    valuesBuilder.append(")");

    queryBuilder.append(valuesBuilder);

    try {
      // tODO check compression
      client.getProxyConnection().set_keyspace(keySpace);
      CqlPreparedResult result =
          client
              .getProxyConnection()
              .prepare_cql3_query(ByteBufferUtil.bytes(queryBuilder.toString()), Compression.NONE);
      client.getProxyConnection().execute_prepared_cql3_query(result.itemId, values, flevel);
    } catch (InvalidRequestException e) {
      throw new IOException(e);
    } catch (TException e) {
      throw new IOException(e);
    }
  }