Last active
August 29, 2015 14:01
-
-
Save ryanleary/5b8b8de78fb84b5bc788 to your computer and use it in GitHub Desktop.
A brief example of how Conditional Mutations work in Accumulo 1.6.0.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void conditionalWrite(String docId, ColumnVisibility cv, Connector conn) throws Exception { | |
// setup writer | |
String tablename = "table"; | |
ConditionalWriterConfig cwConfig = new ConditionalWriterConfig(); | |
cwConfig.setAuthorizations(new Authorizations("a","b","c")); | |
ConditionalWriter cw = conn.createConditionalWriter(tablename, cwConfig); | |
// set up condition on this mutation | |
IteratorSetting is = new IteratorSetting(1, SuppressCVIterator.class); | |
Condition condition = new Condition("docId", new Text(docId)); | |
condition.setIterators(is); | |
// document mutations | |
ConditionalMutation m = new ConditionalMutation(type.getId(), condition); | |
m.put("docId", docId, cv, new Value("".getBytes())); | |
m.put("meta", "meta1", cv, new Value("v1")); | |
m.put("meta", "meta2", cv, new Value("v2")); | |
m.put("meta", "meta3", cv, new Value("v3")); | |
// send mutation and check status | |
Status status = cw.write(docMutation).getStatus(); | |
switch (status) { | |
case ACCEPTED: | |
break; | |
case REJECTED: | |
throw new RuntimeException("Document already exists (rejected)."); | |
case UNKNOWN: | |
throw new RuntimeException("Document may already exist or be written. Resent mutation."); | |
default: | |
throw new RuntimeException("Unexpected status " + status); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment