Conditional Writer Code

Accumulo Tour: Conditional Writer Code

Tour page 11 of 11

Below is a solution to the exercise.

  static boolean setAddress(Connector conn, String id, String expectedAddr, String newAddr) {
    try (ConditionalWriter writer = conn.createConditionalWriter("GothamPD", new ConditionalWriterConfig())) {
      Condition condition = new Condition("location", "home");
      if(expectedAddr != null) {
        condition.setValue(expectedAddr);
      }
      ConditionalMutation mutation = new ConditionalMutation(id, condition);
      mutation.put("location", "home", newAddr);
      return writer.write(mutation).getStatus() == Status.ACCEPTED;
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

The following output shows running the example with a conditional writer. Threads retry when conditional mutations are rejected. The final address has all three modifications.

Thread  37 attempting change '  1007 Mountain Drive, Gotham, New York  ' -> '  1007 Mountain Dr, Gotham, New York  '
Thread  38 attempting change '  1007 Mountain Drive, Gotham, New York  ' -> '1007 Mountain Drive, Gotham, New York'
Thread  39 attempting change '  1007 Mountain Drive, Gotham, New York  ' -> '  1007 Mountain Drive, Gotham, NY  '
Thread  38 attempting change '  1007 Mountain Dr, Gotham, New York  ' -> '1007 Mountain Dr, Gotham, New York'
Thread  39 attempting change '  1007 Mountain Dr, Gotham, New York  ' -> '  1007 Mountain Dr, Gotham, NY  '
Thread  39 attempting change '1007 Mountain Dr, Gotham, New York' -> '1007 Mountain Dr, Gotham, NY'
Final address : '1007 Mountain Dr, Gotham, NY'

< 11 / 11