You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@accumulo.apache.org by mm...@apache.org on 2018/04/20 15:22:49 UTC

[accumulo-examples] branch master updated: Update constraints example. Fixes #23 (#24)

This is an automated email from the ASF dual-hosted git repository.

mmiller pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/accumulo-examples.git


The following commit(s) were added to refs/heads/master by this push:
     new 1b56802  Update constraints example. Fixes #23 (#24)
1b56802 is described below

commit 1b568025c2036ad7ddcd81bcb5ca91e1ee5f268f
Author: Mike Miller <mm...@apache.org>
AuthorDate: Fri Apr 20 11:22:47 2018 -0400

    Update constraints example. Fixes #23 (#24)
---
 docs/constraints.md                                | 57 +++++++++-------------
 docs/maxmutation.md                                | 49 -------------------
 .../constraints/AlphaNumKeyConstraint.java         | 32 ++++++++++++
 .../examples/constraints/MaxMutationSize.java      | 35 +++++++++++++
 .../constraints/NumericValueConstraint.java        | 33 +++++++++++++
 5 files changed, 124 insertions(+), 82 deletions(-)

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.LinkedHashSet;
 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 class AlphaNumKeyConstraint implements Constraint {
     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 @@ package org.apache.accumulo.examples.constraints;
 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 class MaxMutationSize implements Constraint {
       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.Collection;
 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 class NumericValueConstraint implements Constraint {
     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);
+  }
+
 }

-- 
To stop receiving notification emails like this one, please contact
mmiller@apache.org.