You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@knox.apache.org by su...@apache.org on 2017/01/23 21:33:58 UTC

knox git commit: KNOX-828 Adding truncate for HBase table in KnoxShell (Vincent Devillers via Sumit Gupta)

Repository: knox
Updated Branches:
  refs/heads/master 919588273 -> 039915f6e


KNOX-828 Adding truncate for HBase table in KnoxShell (Vincent Devillers via Sumit Gupta)


Project: http://git-wip-us.apache.org/repos/asf/knox/repo
Commit: http://git-wip-us.apache.org/repos/asf/knox/commit/039915f6
Tree: http://git-wip-us.apache.org/repos/asf/knox/tree/039915f6
Diff: http://git-wip-us.apache.org/repos/asf/knox/diff/039915f6

Branch: refs/heads/master
Commit: 039915f6e642c97d43501c922f451c7cc3e679d9
Parents: 9195882
Author: Sumit Gupta <su...@apache.org>
Authored: Mon Jan 23 16:32:53 2017 -0500
Committer: Sumit Gupta <su...@apache.org>
Committed: Mon Jan 23 16:32:53 2017 -0500

----------------------------------------------------------------------
 .../hadoop/gateway/shell/AbstractRequest.java   |  4 +-
 .../org/apache/hadoop/gateway/shell/Hadoop.java |  8 +-
 .../hadoop/gateway/shell/hbase/table/Table.java |  4 +
 .../shell/hbase/table/TruncateTable.java        | 79 ++++++++++++++++++++
 4 files changed, 90 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/knox/blob/039915f6/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/AbstractRequest.java
----------------------------------------------------------------------
diff --git a/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/AbstractRequest.java b/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/AbstractRequest.java
index b639ab3..67ee7ad 100644
--- a/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/AbstractRequest.java
+++ b/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/AbstractRequest.java
@@ -20,8 +20,8 @@ package org.apache.hadoop.gateway.shell;
 import groovy.lang.Closure;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.http.HttpRequest;
-import org.apache.http.HttpResponse;
 import org.apache.http.NameValuePair;
+import org.apache.http.client.methods.CloseableHttpResponse;
 import org.apache.http.client.utils.URIBuilder;
 import org.apache.http.message.BasicNameValuePair;
 
@@ -43,7 +43,7 @@ public abstract class AbstractRequest<T> {
     return session;
   }
 
-  protected HttpResponse execute( HttpRequest request ) throws IOException {
+  protected CloseableHttpResponse execute(HttpRequest request ) throws IOException {
     return session.executeNow( request );
   }
 

http://git-wip-us.apache.org/repos/asf/knox/blob/039915f6/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/Hadoop.java
----------------------------------------------------------------------
diff --git a/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/Hadoop.java b/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/Hadoop.java
index 2dd91f5..1fe28b1 100644
--- a/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/Hadoop.java
+++ b/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/Hadoop.java
@@ -20,11 +20,11 @@ package org.apache.hadoop.gateway.shell;
 import org.apache.commons.io.IOUtils;
 import org.apache.http.HttpHost;
 import org.apache.http.HttpRequest;
-import org.apache.http.HttpResponse;
 import org.apache.http.auth.AuthScope;
 import org.apache.http.auth.UsernamePasswordCredentials;
 import org.apache.http.client.AuthCache;
 import org.apache.http.client.CredentialsProvider;
+import org.apache.http.client.methods.CloseableHttpResponse;
 import org.apache.http.config.ConnectionConfig;
 import org.apache.http.config.Registry;
 import org.apache.http.config.RegistryBuilder;
@@ -231,8 +231,8 @@ public class Hadoop implements Closeable {
     return base;
   }
 
-  public HttpResponse executeNow( HttpRequest request ) throws IOException {
-    HttpResponse response = client.execute( host, request, context );
+  public CloseableHttpResponse executeNow(HttpRequest request ) throws IOException {
+    CloseableHttpResponse response = client.execute( host, request, context );
     if( response.getStatusLine().getStatusCode() < 400 ) {
       return response;
     } else {
@@ -279,6 +279,8 @@ public class Hadoop implements Closeable {
       shutdown();
     } catch (InterruptedException e) {
       throw new HadoopException("Can not shutdown underlying resources", e);
+    } finally {
+      client.close();
     }
   }
 }

http://git-wip-us.apache.org/repos/asf/knox/blob/039915f6/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/hbase/table/Table.java
----------------------------------------------------------------------
diff --git a/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/hbase/table/Table.java b/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/hbase/table/Table.java
index d727f4f..4a17ffc 100644
--- a/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/hbase/table/Table.java
+++ b/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/hbase/table/Table.java
@@ -66,6 +66,10 @@ public class Table {
     return new DeleteTable.Request( session, name );
   }
 
+  public TruncateTable.Request truncate() {
+    return new TruncateTable.Request( session, name );
+  }
+
   public Row row( String id ) {
     return new Row( id ).table( this );
   }

http://git-wip-us.apache.org/repos/asf/knox/blob/039915f6/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/hbase/table/TruncateTable.java
----------------------------------------------------------------------
diff --git a/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/hbase/table/TruncateTable.java b/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/hbase/table/TruncateTable.java
new file mode 100644
index 0000000..5f4f131
--- /dev/null
+++ b/gateway-shell/src/main/java/org/apache/hadoop/gateway/shell/hbase/table/TruncateTable.java
@@ -0,0 +1,79 @@
+/**
+ * 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.gateway.shell.hbase.table;
+
+import org.apache.hadoop.gateway.shell.AbstractRequest;
+import org.apache.hadoop.gateway.shell.EmptyResponse;
+import org.apache.hadoop.gateway.shell.Hadoop;
+import org.apache.hadoop.gateway.shell.hbase.HBase;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpDelete;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPut;
+import org.apache.http.entity.ContentType;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.util.EntityUtils;
+
+import java.net.URI;
+import java.util.concurrent.Callable;
+
+public class TruncateTable {
+  public static class Request extends AbstractRequest<TruncateTable.Response> {
+
+    private String tableName;
+
+    public Request(Hadoop session, String tableName) {
+      super(session);
+      this.tableName = tableName;
+    }
+
+    protected Callable<TruncateTable.Response> callable() {
+      return new Callable<TruncateTable.Response>() {
+        @Override
+        public TruncateTable.Response call() throws Exception {
+
+          URI uri = uri(HBase.SERVICE_PATH, "/", tableName, "/schema").build();
+
+          String schema;
+          HttpGet get = new HttpGet(uri);
+          get.setHeader("Accept", "text/xml");
+          try (CloseableHttpResponse getResponse = execute(get)) {
+            schema = EntityUtils.toString(getResponse.getEntity());
+          }
+
+          HttpDelete delete = new HttpDelete(uri);
+          try (CloseableHttpResponse deleteResponse = execute(delete)) {
+            EntityUtils.consumeQuietly(deleteResponse.getEntity());
+          }
+
+          HttpPut put = new HttpPut(uri);
+          HttpEntity entity = new StringEntity(schema, ContentType.create("text/xml", "UTF-8"));
+          put.setEntity(entity);
+          return new TruncateTable.Response(execute(put));
+        }
+      };
+    }
+  }
+
+  public static class Response extends EmptyResponse {
+    Response(HttpResponse response) {
+      super(response);
+    }
+  }
+}