accumulo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ktur...@apache.org
Subject [accumulo] branch master updated: Add method to create client from connector (#1657)
Date Thu, 16 Jul 2020 00:55:30 GMT
This is an automated email from the ASF dual-hosted git repository.

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


The following commit(s) were added to refs/heads/master by this push:
     new 2196bc8  Add method to create client from connector (#1657)
2196bc8 is described below

commit 2196bc8418eb0eedf599e9a413fe356a41b742a8
Author: Keith Turner <kturner@apache.org>
AuthorDate: Wed Jul 15 20:55:20 2020 -0400

    Add method to create client from connector (#1657)
---
 .../org/apache/accumulo/core/client/Connector.java | 22 +++++++++++++++++++++
 .../accumulo/test/functional/AccumuloClientIT.java | 23 ++++++++++++++++++++++
 2 files changed, 45 insertions(+)

diff --git a/core/src/main/java/org/apache/accumulo/core/client/Connector.java b/core/src/main/java/org/apache/accumulo/core/client/Connector.java
index 4172fd4..fa30e62 100644
--- a/core/src/main/java/org/apache/accumulo/core/client/Connector.java
+++ b/core/src/main/java/org/apache/accumulo/core/client/Connector.java
@@ -284,4 +284,26 @@ public abstract class Connector {
       throws AccumuloSecurityException, AccumuloException {
     return new ConnectorImpl((ClientContext) client);
   }
+
+  /**
+   * Creates a new Accumulo Client from a Connector. The returned client should be closed
and
+   * closing it will not affect the Connector from which it was derived. This method is useful
for
+   * cases where code written using Connector must call code written using AccumuloClient.
Below is
+   * an example.
+   *
+   * <pre>
+   * <code>
+   *   Connector conn = getMyConnector();
+   *   try(AccumuloClient client = Connector.newClient(conn) {
+   *      doSomething(client);
+   *   }
+   * </code>
+   * </pre>
+   *
+   * @since 2.1.0
+   */
+  public static AccumuloClient newClient(Connector conn) {
+    return Accumulo.newClient().from(((ConnectorImpl) conn).getAccumuloClient().getProperties())
+        .build();
+  }
 }
diff --git a/test/src/main/java/org/apache/accumulo/test/functional/AccumuloClientIT.java
b/test/src/main/java/org/apache/accumulo/test/functional/AccumuloClientIT.java
index 4662166..bd0cbd8 100644
--- a/test/src/main/java/org/apache/accumulo/test/functional/AccumuloClientIT.java
+++ b/test/src/main/java/org/apache/accumulo/test/functional/AccumuloClientIT.java
@@ -32,6 +32,7 @@ import org.apache.accumulo.core.client.Accumulo;
 import org.apache.accumulo.core.client.AccumuloClient;
 import org.apache.accumulo.core.client.BatchWriter;
 import org.apache.accumulo.core.client.ConditionalWriterConfig;
+import org.apache.accumulo.core.client.Connector;
 import org.apache.accumulo.core.client.Scanner;
 import org.apache.accumulo.core.client.admin.TableOperations;
 import org.apache.accumulo.core.client.security.tokens.PasswordToken;
@@ -93,6 +94,28 @@ public class AccumuloClientIT extends AccumuloClusterHarness {
     expectClosed(c::tableOperations);
   }
 
+  @SuppressWarnings("deprecation")
+  @Test
+  public void testGetAccumuloClientFromConnector() throws Exception {
+    try (AccumuloClient client1 = Accumulo.newClient().from(getClientProps()).build()) {
+      org.apache.accumulo.core.client.Connector c =
+          org.apache.accumulo.core.client.Connector.from(client1);
+
+      String tableName = getUniqueNames(1)[0];
+
+      c.tableOperations().create(tableName);
+
+      try (AccumuloClient client2 = Connector.newClient(c)) {
+        assertTrue(client2.tableOperations().list().contains(tableName));
+      }
+
+      // closing client2 should not have had an impact on the connector or client1
+
+      assertTrue(client1.tableOperations().list().contains(tableName));
+      assertTrue(c.tableOperations().list().contains(tableName));
+    }
+  }
+
   @Test
   public void testAccumuloClientBuilder() throws Exception {
     AccumuloClient c = Accumulo.newClient().from(getClientProps()).build();


Mime
View raw message