Skip to content
Permalink
Browse files
Update constraints example. Fixes #23 (#24)
  • Loading branch information
milleruntime committed Apr 20, 2018
1 parent cb54d58 commit 1b568025c2036ad7ddcd81bcb5ca91e1ee5f268f
Showing 5 changed files with 124 additions and 82 deletions.
@@ -18,37 +18,28 @@ limitations under the License.

This tutorial uses the following Java classes, which can be found in org.apache.accumulo.examples.constraints:

* AlphaNumKeyConstraint.java - a constraint that requires alphanumeric keys
* NumericValueConstraint.java - a constraint that requires numeric string values

This an example of how to create a table with constraints. Below a table is
created with two example constraints. One constraints does not allow non alpha
numeric keys. The other constraint does not allow non numeric values. Two
inserts that violate these constraints are attempted and denied. The scan at
the end shows the inserts were not allowed.

$ accumulo shell -u username -p password

Shell - Apache Accumulo Interactive Shell
-
- version: 1.5.0
- instance name: instance
- instance id: 00000000-0000-0000-0000-000000000000
-
- type 'help' for a list of available commands
-
username@instance> createtable testConstraints
username@instance testConstraints> constraint -a org.apache.accumulo.examples.constraints.NumericValueConstraint
username@instance testConstraints> constraint -a org.apache.accumulo.examples.constraints.AlphaNumKeyConstraint
username@instance testConstraints> insert r1 cf1 cq1 1111
username@instance testConstraints> insert r1 cf1 cq1 ABC
Constraint Failures:
ConstraintViolationSummary(constrainClass:org.apache.accumulo.examples.constraints.NumericValueConstraint, violationCode:1, violationDescription:Value is not numeric, numberOfViolatingMutations:1)
username@instance testConstraints> insert r1! cf1 cq1 ABC
Constraint Failures:
ConstraintViolationSummary(constrainClass:org.apache.accumulo.examples.constraints.NumericValueConstraint, violationCode:1, violationDescription:Value is not numeric, numberOfViolatingMutations:1)
ConstraintViolationSummary(constrainClass:org.apache.accumulo.examples.constraints.AlphaNumKeyConstraint, violationCode:1, violationDescription:Row was not alpha numeric, numberOfViolatingMutations:1)
username@instance testConstraints> scan
r1 cf1:cq1 [] 1111
username@instance testConstraints>
* [AlphaNumKeyConstraint.java] - a constraint that requires alphanumeric keys
* [NumericValueConstraint.java] - a constraint that requires numeric string values
* [MaxMutationSize.java] - a constraint that limits the size of mutations accepted into a table

Remember to copy the accumulo-examples-\*.jar to Accumulo's 'lib/ext' directory.

AlphaNumKeyConstraint prevents insertion of keys with characters not between aA and zZ or 0 to 9.
NumericValueConstraint prevents insertion of values with characters not between 0 and 9. The examples create mutations
that violate the constraint, throwing an exception.

$ ./bin/runex constraints.AlphaNumKeyConstraint
$ ./bin/runex constraints.NumericValueConstraint

The MaxMutationSize constraint will force the table to reject any mutation that is larger than 1/256th of the
working memory of the tablet server. The following example attempts to ingest a single row with a million columns,
which exceeds the memory limit. Depending on the amount of Java heap your tserver(s) are given, you may have to
increase the number of columns provided to see the failure.

$ ./bin/runex constraints.MaxMutationSize

[AlphaNumKeyConstraint.java]: ../src/main/java/org/apache/accumulo/examples/constraints/AlphaNumKeyConstraint.java
[NumericValueConstraint.java]: ../src/main/java/org/apache/accumulo/examples/constraints/NumericValueConstraint.java
[MaxMutationSize.java]: ../src/main/java/org/apache/accumulo/examples/constraints/MaxMutationSize.java


This file was deleted.

@@ -22,9 +22,17 @@
import java.util.List;
import java.util.Set;

import org.apache.accumulo.core.client.AccumuloException;
import org.apache.accumulo.core.client.AccumuloSecurityException;
import org.apache.accumulo.core.client.BatchWriter;
import org.apache.accumulo.core.client.Connector;
import org.apache.accumulo.core.client.MutationsRejectedException;
import org.apache.accumulo.core.client.TableExistsException;
import org.apache.accumulo.core.client.TableNotFoundException;
import org.apache.accumulo.core.constraints.Constraint;
import org.apache.accumulo.core.data.ColumnUpdate;
import org.apache.accumulo.core.data.Mutation;
import org.apache.accumulo.core.data.Value;

/**
* This class is an accumulo constraint that ensures all fields of a key are alpha numeric.
@@ -93,4 +101,28 @@ public String getViolationDescription(short violationCode) {
return null;
}

public static void main(String[] args) throws AccumuloException, AccumuloSecurityException, TableNotFoundException {
Connector connector = Connector.builder().usingProperties("conf/accumulo-client.properties").build();
try {
connector.tableOperations().create("testConstraints");
} catch (TableExistsException e) {
// ignore
}

/**
* Add the {@link AlphaNumKeyConstraint} to the table. Be sure to use the fully qualified class name.
*/
int num = connector.tableOperations().addConstraint("testConstraints", "org.apache.accumulo.examples.constraints.AlphaNumKeyConstraint");

System.out.println("Attempting to write non alpha numeric data to testConstraints");
try (BatchWriter bw = connector.createBatchWriter("testConstraints")) {
Mutation m = new Mutation("r1--$$@@%%");
m.put("cf1", "cq1", new Value(("value1").getBytes()));
bw.addMutation(m);
} catch (MutationsRejectedException e) {
e.getConstraintViolationSummaries().forEach(violationSummary -> System.out.println("Constraint violated: " + violationSummary.constrainClass));
}

connector.tableOperations().removeConstraint("testConstraints", num);
}
}
@@ -19,8 +19,16 @@
import java.util.Collections;
import java.util.List;

import org.apache.accumulo.core.client.AccumuloException;
import org.apache.accumulo.core.client.AccumuloSecurityException;
import org.apache.accumulo.core.client.BatchWriter;
import org.apache.accumulo.core.client.Connector;
import org.apache.accumulo.core.client.MutationsRejectedException;
import org.apache.accumulo.core.client.TableExistsException;
import org.apache.accumulo.core.client.TableNotFoundException;
import org.apache.accumulo.core.constraints.Constraint;
import org.apache.accumulo.core.data.Mutation;
import org.apache.accumulo.core.data.Value;

/**
* Ensure that mutations are a reasonable size: we must be able to fit several in memory at a time.
@@ -41,4 +49,31 @@ public List<Short> check(Environment env, Mutation mutation) {
return empty;
return violations;
}

public static void main(String[] args) throws AccumuloException, AccumuloSecurityException, TableNotFoundException {
Connector connector = Connector.builder().usingProperties("conf/accumulo-client.properties").build();
try {
connector.tableOperations().create("testConstraints");
} catch (TableExistsException e) {
// ignore
}

/**
* Add the {@link MaxMutationSize} constraint to the table. Be sure to use the fully qualified class name
*/
int num = connector.tableOperations().addConstraint("testConstraints", "org.apache.accumulo.examples.constraints.MaxMutationSize");

System.out.println("Attempting to write a lot of mutations to testConstraints");
try (BatchWriter bw = connector.createBatchWriter("testConstraints")) {
Mutation m = new Mutation("r1");
for (int i = 0; i < 1_000_000; i++)
m.put("cf" + i % 5000, "cq" + i, new Value(("value" + i).getBytes()));
bw.addMutation(m);
} catch (MutationsRejectedException e) {
e.getConstraintViolationSummaries().forEach(m -> System.out.println("Constraint violated: " + m.constrainClass));
}

connector.tableOperations().removeConstraint("testConstraints", num);
}

}
@@ -21,9 +21,17 @@
import java.util.Collections;
import java.util.List;

import org.apache.accumulo.core.client.AccumuloException;
import org.apache.accumulo.core.client.AccumuloSecurityException;
import org.apache.accumulo.core.client.BatchWriter;
import org.apache.accumulo.core.client.Connector;
import org.apache.accumulo.core.client.MutationsRejectedException;
import org.apache.accumulo.core.client.TableExistsException;
import org.apache.accumulo.core.client.TableNotFoundException;
import org.apache.accumulo.core.constraints.Constraint;
import org.apache.accumulo.core.data.ColumnUpdate;
import org.apache.accumulo.core.data.Mutation;
import org.apache.accumulo.core.data.Value;

/**
* This class is an accumulo constraint that ensures values are numeric strings.
@@ -68,4 +76,29 @@ public String getViolationDescription(short violationCode) {
return null;
}

public static void main(String[] args) throws AccumuloException, AccumuloSecurityException, TableNotFoundException {
Connector connector = Connector.builder().usingProperties("conf/accumulo-client.properties").build();
try {
connector.tableOperations().create("testConstraints");
} catch (TableExistsException e) {
// ignore
}

/**
* Add the {@link NumericValueConstraint} constraint to the table. Be sure to use the fully qualified class name
*/
int num = connector.tableOperations().addConstraint("testConstraints", "org.apache.accumulo.examples.constraints.NumericValueConstraint");

System.out.println("Attempting to write non numeric data to testConstraints");
try (BatchWriter bw = connector.createBatchWriter("testConstraints")) {
Mutation m = new Mutation("r1");
m.put("cf1", "cq1", new Value(("value1--$$@@%%").getBytes()));
bw.addMutation(m);
} catch (MutationsRejectedException e) {
e.getConstraintViolationSummaries().forEach(m -> System.out.println("Constraint violated: " + m.constrainClass));
}

connector.tableOperations().removeConstraint("testConstraints", num);
}

}

0 comments on commit 1b56802

Please sign in to comment.