milleruntime closed pull request #24: Update constraints example. Fixes #23
URL: https://github.com/apache/accumulo-examples/pull/24
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/docs/constraints.md b/docs/constraints.md
index 331e81e..71803bc 100644
--- a/docs/constraints.md
+++ b/docs/constraints.md
@@ -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
+
 
diff --git a/docs/maxmutation.md b/docs/maxmutation.md
deleted file mode 100644
index d091adb..0000000
--- a/docs/maxmutation.md
+++ /dev/null
@@ -1,49 +0,0 @@
-<!--
-Licensed to the Apache Software Foundation (ASF) under one or more
-contributor license agreements.  See the NOTICE file distributed with
-this work for additional information regarding copyright ownership.
-The ASF licenses this file to You under the Apache License, Version 2.0
-(the "License"); you may not use this file except in compliance with
-the License.  You may obtain a copy of the License at
-
-    http://www.apache.org/licenses/LICENSE-2.0
-
-Unless required by applicable law or agreed to in writing, software
-distributed under the License is distributed on an "AS IS" BASIS,
-WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-See the License for the specific language governing permissions and
-limitations under the License.
--->
-# Apache Accumulo MaxMutation Constraints Example
-
-This an example of how to limit the size of mutations that will be accepted 
into
-a table. Under the default configuration, accumulo does not provide a 
limitation
-on the size of mutations that can be ingested. Poorly behaved writers might
-inadvertently create mutations so large, that they cause the tablet servers to
-run out of memory. A simple contraint can be added to a table to reject very
-large mutations.
-
-    $ 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 test_ingest
-    username@instance test_ingest> config -t test_ingest -s 
table.constraint.1=org.apache.accumulo.examples.constraints.MaxMutationSize
-    username@instance test_ingest>
-
-
-Now the table will reject any mutation that is larger than 1/256th of the 
-working memory of the tablet server.  The following command attempts to ingest 
-a single row with 10000 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.
-
-    $ accumulo org.apache.accumulo.test.TestIngest -i instance -z zookeepers 
-u username -p password --rows 1 --cols 10000 
-    ERROR : Constraint violates : 
ConstraintViolationSummary(constrainClass:org.apache.accumulo.examples.constraints.MaxMutationSize,
 violationCode:0, violationDescription:mutation exceeded maximum size of 
188160, numberOfViolatingMutations:1)
-
diff --git 
a/src/main/java/org/apache/accumulo/examples/constraints/AlphaNumKeyConstraint.java
 
b/src/main/java/org/apache/accumulo/examples/constraints/AlphaNumKeyConstraint.java
index 7077722..0ae0d5f 100644
--- 
a/src/main/java/org/apache/accumulo/examples/constraints/AlphaNumKeyConstraint.java
+++ 
b/src/main/java/org/apache/accumulo/examples/constraints/AlphaNumKeyConstraint.java
@@ -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);
+  }
 }
diff --git 
a/src/main/java/org/apache/accumulo/examples/constraints/MaxMutationSize.java 
b/src/main/java/org/apache/accumulo/examples/constraints/MaxMutationSize.java
index c7094c6..52d1d7c 100644
--- 
a/src/main/java/org/apache/accumulo/examples/constraints/MaxMutationSize.java
+++ 
b/src/main/java/org/apache/accumulo/examples/constraints/MaxMutationSize.java
@@ -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 String getViolationDescription(short violationCode) {
       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);
+  }
+
 }
diff --git 
a/src/main/java/org/apache/accumulo/examples/constraints/NumericValueConstraint.java
 
b/src/main/java/org/apache/accumulo/examples/constraints/NumericValueConstraint.java
index 0a8842d..998787f 100644
--- 
a/src/main/java/org/apache/accumulo/examples/constraints/NumericValueConstraint.java
+++ 
b/src/main/java/org/apache/accumulo/examples/constraints/NumericValueConstraint.java
@@ -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);
+  }
+
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to