@PooledConnection
  public void setColumn(
      String keyspace,
      String column_family,
      String key,
      JSONObject json,
      ConsistencyLevel consistency_level,
      boolean index,
      long timestamp)
      throws InvalidRequestException, UnavailableException, TimedOutException, TException,
          HttpException, IOException {
    List<Mutation> slice = new ArrayList<Mutation>();
    for (Object field : json.keySet()) {
      String name = (String) field;
      String value = (String) json.get(name);
      Column c = new Column();
      c.setName(ByteBufferUtil.bytes(name));
      c.setValue(ByteBufferUtil.bytes(value));
      c.setTimestamp(timestamp);

      Mutation m = new Mutation();
      ColumnOrSuperColumn cc = new ColumnOrSuperColumn();
      cc.setColumn(c);
      m.setColumn_or_supercolumn(cc);
      slice.add(m);
    }
    Map<ByteBuffer, Map<String, List<Mutation>>> mutationMap =
        new HashMap<ByteBuffer, Map<String, List<Mutation>>>();
    Map<String, List<Mutation>> cfMutations = new HashMap<String, List<Mutation>>();
    cfMutations.put(column_family, slice);
    mutationMap.put(ByteBufferUtil.bytes(key), cfMutations);
    getConnection(keyspace).batch_mutate(mutationMap, consistency_level);

    if (config.isIndexingEnabled() && index) indexer.index(column_family, key, json);
  }
  @Override
  public ColumnListMutation<C> incrementCounterColumn(C columnName, long amount) {
    // 1. Set up the column with all the data
    CounterColumn column = new CounterColumn();
    column.setName(columnSerializer.toByteBuffer(columnName));
    column.setValue(amount);

    // 2. Create a mutation and append to the mutation list.
    Mutation mutation = new Mutation();
    mutation.setColumn_or_supercolumn(new ColumnOrSuperColumn().setCounter_column(column));
    mutationList.add(mutation);
    return this;
  }
Exemple #3
0
  private static Mutation getMutation(String colName, String value) {

    Column c = new Column();

    c.setName(ByteBufferUtil.bytes(colName));

    c.setValue(ByteBufferUtil.bytes(value));
    c.setTimestamp(System.currentTimeMillis());

    Mutation m = new Mutation();
    m.setColumn_or_supercolumn(new ColumnOrSuperColumn());
    m.column_or_supercolumn.setColumn(c);
    return m;
  }
  @Override
  public ColumnListMutation<C> putEmptyColumn(C columnName, Integer ttl) {
    Column column = new Column();
    column.setName(columnSerializer.toByteBuffer(columnName));
    column.setValue(ThriftUtils.EMPTY_BYTE_BUFFER);
    column.setTimestamp(timestamp);
    if (ttl != null) column.setTtl(ttl);
    else if (defaultTtl != null) column.setTtl(defaultTtl);

    // 2. Create a mutation and append to the mutation list.
    Mutation mutation = new Mutation();
    mutation.setColumn_or_supercolumn(new ColumnOrSuperColumn().setColumn(column));
    mutationList.add(mutation);
    return this;
  }
  @Override
  public <V> ColumnListMutation<C> putColumn(
      C columnName, V value, Serializer<V> valueSerializer, Integer ttl) {
    // 1. Set up the column with all the data
    Column column = new Column();
    column.setName(columnSerializer.toByteBuffer(columnName));
    column.setValue(valueSerializer.toByteBuffer(value));
    column.setTimestamp(timestamp);
    if (ttl != null) column.setTtl(ttl);
    else if (defaultTtl != null) column.setTtl(defaultTtl);

    // 2. Create a mutation and append to the mutation list.
    Mutation mutation = new Mutation();
    mutation.setColumn_or_supercolumn(new ColumnOrSuperColumn().setColumn(column));
    mutationList.add(mutation);

    return this;
  }
Exemple #6
0
  @Test
  public void testBatchMutate() throws HectorException {
    Map<String, Map<String, List<Mutation>>> outerMutationMap =
        new HashMap<String, Map<String, List<Mutation>>>();
    for (int i = 0; i < 10; i++) {

      Map<String, List<Mutation>> mutationMap = new HashMap<String, List<Mutation>>();

      ArrayList<Mutation> mutations = new ArrayList<Mutation>(10);
      for (int j = 0; j < 10; j++) {
        Column col =
            new Column(
                StringSerializer.get().toByteBuffer("testBatchMutateColumn_" + j),
                StringSerializer.get().toByteBuffer("testBatchMutateColumn_value_" + j),
                connectionManager.createClock());
        // list.add(col);
        ColumnOrSuperColumn cosc = new ColumnOrSuperColumn();
        cosc.setColumn(col);
        Mutation mutation = new Mutation();
        mutation.setColumn_or_supercolumn(cosc);
        mutations.add(mutation);
      }
      mutationMap.put("Standard1", mutations);
      outerMutationMap.put("testBatchMutateColumn_" + i, mutationMap);
    }
    keyspace.batchMutate(se.toBytesMap(outerMutationMap));
    // re-use later
    outerMutationMap.clear();

    // get value
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 10; j++) {
        ColumnPath cp = new ColumnPath("Standard1");
        cp.setColumn(bytes("testBatchMutateColumn_" + j));

        Column col = keyspace.getColumn("testBatchMutateColumn_" + i, cp);
        assertNotNull(col);
        String value = string(col.getValue());
        assertEquals("testBatchMutateColumn_value_" + j, value);
      }
    }

    // batch_mutate delete by key
    for (int i = 0; i < 10; i++) {
      ArrayList<Mutation> mutations = new ArrayList<Mutation>(10);
      Map<String, List<Mutation>> mutationMap = new HashMap<String, List<Mutation>>();
      SlicePredicate slicePredicate = new SlicePredicate();
      for (int j = 0; j < 10; j++) {
        slicePredicate.addToColumn_names(
            StringSerializer.get().toByteBuffer("testBatchMutateColumn_" + j));
      }
      Mutation mutation = new Mutation();
      Deletion deletion = new Deletion(connectionManager.createClock());
      deletion.setPredicate(slicePredicate);
      mutation.setDeletion(deletion);
      mutations.add(mutation);

      mutationMap.put("Standard1", mutations);
      outerMutationMap.put("testBatchMutateColumn_" + i, mutationMap);
    }
    keyspace.batchMutate(se.toBytesMap(outerMutationMap));
    // make sure the values are gone
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 10; j++) {
        ColumnPath cp = new ColumnPath("Standard1");
        cp.setColumn(bytes("testBatchMutateColumn_" + j));
        try {
          keyspace.getColumn("testBatchMutateColumn_" + i, cp);
          fail();
        } catch (HNotFoundException e) {
        }
      }
    }
  }