From af740d324224ba9bb6f6b798ec2339d7d79d6cfe Mon Sep 17 00:00:00 2001 From: zhangduo Date: Sat, 15 Oct 2016 20:17:20 +0800 Subject: [PATCH] HBASE-16834 Implement AsyncConnectionFactory --- .../hbase/client/AsyncConnectionFactory.java | 70 ++++++++++++++++++++++ .../apache/hadoop/hbase/client/TestAsyncTable.java | 3 +- 2 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncConnectionFactory.java diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncConnectionFactory.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncConnectionFactory.java new file mode 100644 index 0000000..d78862d --- /dev/null +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncConnectionFactory.java @@ -0,0 +1,70 @@ +/** + * 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. + */ +package org.apache.hadoop.hbase.client; + +import java.io.IOException; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.HBaseConfiguration; +import org.apache.hadoop.hbase.classification.InterfaceAudience; +import org.apache.hadoop.hbase.classification.InterfaceStability; +import org.apache.hadoop.hbase.security.User; +import org.apache.hadoop.hbase.security.UserProvider; +import org.apache.hadoop.hbase.util.ReflectionUtils; + +/** + * A non-instantiable class that manages creation of {@link AsyncConnection}s. Managing the + * lifecycle of the {@link AsyncConnection}s to the cluster is the responsibility of the caller. + * From a {@link AsyncConnection}, {@link AsyncTable} implementations are retrieved with + * {@link AsyncConnection#getTable(org.apache.hadoop.hbase.TableName)}. + *

+ * Example: + * + *

+ * try (AsyncConnection conn = AsyncConnectionFactory.create()) {
+ *   AsyncTable table = connection.getTable(TableName.valueOf("table1"));
+ *   // Use the table as needed, for a single operation and a single thread
+ * }
+ * 
+ * + * @see AsyncConnection + */ +@InterfaceAudience.Public +@InterfaceStability.Unstable +public class AsyncConnectionFactory { + + public static final String HBASE_CLIENT_ASYNC_CONNECTION_IMPL = + "hbase.client.async.connection.impl"; + + private AsyncConnectionFactory() { + } + + public static AsyncConnection create() throws IOException { + return create(HBaseConfiguration.create()); + } + + public static AsyncConnection create(Configuration conf) throws IOException { + return create(conf, UserProvider.instantiate(conf).getCurrent()); + } + + public static AsyncConnection create(Configuration conf, User user) throws IOException { + Class clazz = conf.getClass(HBASE_CLIENT_ASYNC_CONNECTION_IMPL, + AsyncConnectionImpl.class, AsyncConnection.class); + return ReflectionUtils.newInstance(clazz, conf, user); + } +} diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAsyncTable.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAsyncTable.java index 0667de3..7869994 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAsyncTable.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAsyncTable.java @@ -28,7 +28,6 @@ import java.util.stream.IntStream; import org.apache.hadoop.hbase.HBaseTestingUtility; import org.apache.hadoop.hbase.TableName; -import org.apache.hadoop.hbase.security.User; import org.apache.hadoop.hbase.testclassification.ClientTests; import org.apache.hadoop.hbase.testclassification.MediumTests; import org.apache.hadoop.hbase.util.Bytes; @@ -60,7 +59,7 @@ public class TestAsyncTable { TEST_UTIL.startMiniCluster(1); TEST_UTIL.createTable(TABLE_NAME, FAMILY); TEST_UTIL.waitTableAvailable(TABLE_NAME); - ASYNC_CONN = new AsyncConnectionImpl(TEST_UTIL.getConfiguration(), User.getCurrent()); + ASYNC_CONN = AsyncConnectionFactory.create(TEST_UTIL.getConfiguration()); } @AfterClass -- 1.9.1