You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kylin.apache.org by sh...@apache.org on 2017/02/16 03:36:22 UTC

kylin git commit: KYLIN-2447 Add more functions to rest client for future use

Repository: kylin
Updated Branches:
  refs/heads/master c880db0d7 -> a71193796


KYLIN-2447 Add more functions to rest client for future use

Signed-off-by: shaofengshi <sh...@apache.org>


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

Branch: refs/heads/master
Commit: a71193796f218ba1754240334d8cfce34ddde6ff
Parents: c880db0
Author: xiefan46 <95...@qq.com>
Authored: Fri Jan 13 16:40:05 2017 +0800
Committer: shaofengshi <sh...@apache.org>
Committed: Thu Feb 16 11:07:52 2017 +0800

----------------------------------------------------------------------
 .../kylin/common/restclient/RestClient.java     | 155 +++++++++++++++++++
 .../kylin/common/restclient/RestClientTest.java |   9 +-
 .../kylin/restclient/ITRestClientTest.java      | 149 ++++++++++++++++++
 3 files changed, 311 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/kylin/blob/a7119379/core-common/src/main/java/org/apache/kylin/common/restclient/RestClient.java
----------------------------------------------------------------------
diff --git a/core-common/src/main/java/org/apache/kylin/common/restclient/RestClient.java b/core-common/src/main/java/org/apache/kylin/common/restclient/RestClient.java
index 269e935..825c67f 100644
--- a/core-common/src/main/java/org/apache/kylin/common/restclient/RestClient.java
+++ b/core-common/src/main/java/org/apache/kylin/common/restclient/RestClient.java
@@ -18,22 +18,33 @@
 
 package org.apache.kylin.common.restclient;
 
+import java.io.BufferedReader;
 import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.URI;
+import java.util.HashMap;
 import java.util.Map;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.commons.io.IOUtils;
 import org.apache.http.HttpResponse;
 import org.apache.http.auth.AuthScope;
 import org.apache.http.auth.UsernamePasswordCredentials;
 import org.apache.http.client.CredentialsProvider;
 import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
 import org.apache.http.client.methods.HttpPut;
+import org.apache.http.client.methods.HttpRequestBase;
+import org.apache.http.entity.StringEntity;
 import org.apache.http.impl.client.BasicCredentialsProvider;
 import org.apache.http.impl.client.DefaultHttpClient;
 import org.apache.http.util.EntityUtils;
 import org.apache.kylin.common.util.JsonUtil;
 
+import javax.xml.bind.DatatypeConverter;
+
 /**
  * @author yangli9
  */
@@ -71,6 +82,10 @@ public class RestClient {
         init(host, port, user, pwd);
     }
 
+    public RestClient(String host, int port, String userName, String password) {
+        init(host, port, userName, password);
+    }
+
     private void init(String host, int port, String userName, String password) {
         this.host = host;
         this.port = port;
@@ -122,4 +137,144 @@ public class RestClient {
         }
     }
 
+    public boolean enableCache() throws IOException {
+        return setCache(true);
+    }
+
+    public boolean disableCache() throws IOException {
+        return setCache(false);
+    }
+
+    public boolean buildCube(String cubeName, long startTime, long endTime, String buildType) throws Exception {
+        String url = baseUrl + "/cubes/" + cubeName + "/build";
+        HttpPut put = newPut(url);
+        HashMap<String, String> paraMap = new HashMap<String, String>();
+        paraMap.put("startTime", startTime + "");
+        paraMap.put("endTime", endTime + "");
+        paraMap.put("buildType", buildType);
+        String jsonMsg = new ObjectMapper().writeValueAsString(paraMap);
+        put.setEntity(new StringEntity(jsonMsg, "UTF-8"));
+        HttpResponse response = client.execute(put);
+        String result = getContent(response);
+        if (response.getStatusLine().getStatusCode() != 200) {
+            throw new IOException("Invalid response " + response.getStatusLine().getStatusCode() + " with build cube url " + url + "\n" + jsonMsg);
+        } else {
+            return true;
+        }
+    }
+
+    public boolean disableCube(String cubeName) throws Exception {
+        return changeCubeStatus(baseUrl + "/cubes/" + cubeName + "/disable");
+    }
+
+    public boolean enableCube(String cubeName) throws Exception {
+        return changeCubeStatus(baseUrl + "/cubes/" + cubeName + "/enable");
+    }
+
+    public boolean purgeCube(String cubeName) throws Exception {
+        return changeCubeStatus(baseUrl + "/cubes/" + cubeName + "/purge");
+    }
+
+    public HashMap getCube(String cubeName) throws Exception {
+        String url = baseUrl + "/cubes/" + cubeName;
+        HttpGet get = newGet(url);
+        get.setURI(new URI(url));
+        HttpResponse response = client.execute(get);
+        return dealResponse(response);
+    }
+
+    private boolean changeCubeStatus(String url) throws Exception {
+        HttpPut put = newPut(url);
+        HashMap<String, String> paraMap = new HashMap<String, String>();
+        String jsonMsg = new ObjectMapper().writeValueAsString(paraMap);
+        put.setEntity(new StringEntity(jsonMsg, "UTF-8"));
+        HttpResponse response = client.execute(put);
+        String result = getContent(response);
+        if (response.getStatusLine().getStatusCode() != 200) {
+            throw new IOException("Invalid response " + response.getStatusLine().getStatusCode() + " with url " + url + "\n" + jsonMsg);
+        } else {
+            return true;
+        }
+    }
+
+    public HttpResponse query(String sql, String project) throws IOException {
+        String url = baseUrl + "/query";
+        HttpPost post = newPost(url);
+        HashMap<String, String> paraMap = new HashMap<String, String>();
+        paraMap.put("sql", sql);
+        paraMap.put("project", project);
+        String jsonMsg = new ObjectMapper().writeValueAsString(paraMap);
+        post.setEntity(new StringEntity(jsonMsg, "UTF-8"));
+        HttpResponse response = client.execute(post);
+        return response;
+    }
+
+    private HashMap dealResponse(HttpResponse response) throws IOException {
+        if (response.getStatusLine().getStatusCode() != 200) {
+            throw new IOException("Invalid response " + response.getStatusLine().getStatusCode());
+        }
+        String result = getContent(response);
+        HashMap resultMap = new ObjectMapper().readValue(result, HashMap.class);
+        return resultMap;
+    }
+
+    private void addHttpHeaders(HttpRequestBase method) {
+        method.addHeader("Accept", "application/json, text/plain, */*");
+        method.addHeader("Content-Type", "application/json");
+        String basicAuth = DatatypeConverter.printBase64Binary((this.userName + ":" + this.password).getBytes());
+        method.addHeader("Authorization", "Basic " + basicAuth);
+    }
+
+    private HttpPost newPost(String url) {
+        HttpPost post = new HttpPost(url);
+        addHttpHeaders(post);
+        return post;
+    }
+
+    private HttpPut newPut(String url) {
+        HttpPut put = new HttpPut(url);
+        addHttpHeaders(put);
+        return put;
+    }
+
+    private HttpGet newGet(String url) {
+        HttpGet get = new HttpGet();
+        addHttpHeaders(get);
+        return get;
+    }
+
+    private boolean setCache(boolean flag) throws IOException {
+        String url = baseUrl + "/admin/config";
+        HttpPut put = newPut(url);
+        HashMap<String, String> paraMap = new HashMap<String, String>();
+        paraMap.put("key", "kylin.query.cache-enabled");
+        paraMap.put("value", flag + "");
+        put.setEntity(new StringEntity(new ObjectMapper().writeValueAsString(paraMap), "UTF-8"));
+        HttpResponse response = client.execute(put);
+        EntityUtils.consume(response.getEntity());
+        if (response.getStatusLine().getStatusCode() != 200) {
+            return false;
+        } else {
+            return true;
+        }
+    }
+
+    private String getContent(HttpResponse response) throws IOException {
+        InputStreamReader reader = null;
+        BufferedReader rd = null;
+        StringBuffer result = new StringBuffer();
+        try{
+            reader = new InputStreamReader(response.getEntity().getContent());
+            rd = new BufferedReader(reader);
+            String line = null;
+            while ((line = rd.readLine()) != null) {
+                result.append(line);
+            }
+        }finally {
+            IOUtils.closeQuietly(reader);
+            IOUtils.closeQuietly(rd);
+        }
+        return result.toString();
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/kylin/blob/a7119379/core-common/src/test/java/org/apache/kylin/common/restclient/RestClientTest.java
----------------------------------------------------------------------
diff --git a/core-common/src/test/java/org/apache/kylin/common/restclient/RestClientTest.java b/core-common/src/test/java/org/apache/kylin/common/restclient/RestClientTest.java
index 82435b7..af05e5e 100644
--- a/core-common/src/test/java/org/apache/kylin/common/restclient/RestClientTest.java
+++ b/core-common/src/test/java/org/apache/kylin/common/restclient/RestClientTest.java
@@ -19,11 +19,17 @@
 package org.apache.kylin.common.restclient;
 
 import java.io.IOException;
-
 import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 
 public class RestClientTest {
 
+
+
+    private static final Logger logger = LoggerFactory.getLogger(RestClientTest.class);
+
     @SuppressWarnings("unused")
     @Test
     public void basicTests() throws IOException {
@@ -37,5 +43,4 @@ public class RestClientTest {
         //System.out.println(bb);
 
     }
-
 }

http://git-wip-us.apache.org/repos/asf/kylin/blob/a7119379/kylin-it/src/test/java/org/apache/kylin/restclient/ITRestClientTest.java
----------------------------------------------------------------------
diff --git a/kylin-it/src/test/java/org/apache/kylin/restclient/ITRestClientTest.java b/kylin-it/src/test/java/org/apache/kylin/restclient/ITRestClientTest.java
new file mode 100644
index 0000000..2afd5c9
--- /dev/null
+++ b/kylin-it/src/test/java/org/apache/kylin/restclient/ITRestClientTest.java
@@ -0,0 +1,149 @@
+/*
+ * 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.kylin.restclient;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.http.HttpResponse;
+import org.apache.kylin.common.restclient.RestClient;
+import org.apache.kylin.common.util.HBaseMetadataTestCase;
+import org.eclipse.jetty.server.Server;
+import org.eclipse.jetty.webapp.WebAppContext;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.util.HashMap;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+/**
+ */
+public class ITRestClientTest extends HBaseMetadataTestCase {
+
+    private static Server server = null;
+
+    private static SystemPropertiesOverride sysPropsOverride = new SystemPropertiesOverride();
+
+    private static final String HOST = "localhost";
+
+    private static final int PORT = 7070;
+
+    private static final String USERNAME = "ADMIN";
+
+    private static final String PASSWD = "KYLIN";
+
+    private static final String PROJECT_NAME = "default";
+
+    private static final String CUBE_NAME = "ci_left_join_cube";
+
+    private static final Logger logger = LoggerFactory.getLogger(ITRestClientTest.class);
+
+    @BeforeClass
+    public static void beforeClass() throws Exception {
+        sysPropsOverride.override("spring.profiles.active", "testing");
+        sysPropsOverride.override("catalina.home", "."); // resources/log4j.properties ref ${catalina.home}
+        staticCreateTestMetadata();
+        startJetty();
+    }
+
+    @AfterClass
+    public static void afterClass() throws Exception {
+        stopJetty();
+        staticCleanupTestMetadata();
+        sysPropsOverride.restore();
+    }
+
+    @Test
+    public void testGetCube() throws Exception {
+        RestClient client = new RestClient(HOST, PORT, USERNAME, PASSWD);
+        HashMap result = client.getCube(CUBE_NAME);
+        assertEquals("READY", result.get("status"));
+    }
+
+    @Test
+    public void testChangeCubeStatus() throws Exception {
+        RestClient client = new RestClient(HOST, PORT, USERNAME, PASSWD);
+        assertTrue(client.disableCube(CUBE_NAME));
+        assertTrue(client.enableCube(CUBE_NAME));
+    }
+
+    @Test
+    public void testChangeCache() throws Exception {
+        RestClient client = new RestClient(HOST, PORT, USERNAME, PASSWD);
+        assertTrue(client.disableCache());
+        assertTrue(client.enableCache());
+    }
+
+    @Test
+    public void testQuery() throws Exception {
+        RestClient client = new RestClient(HOST, PORT, USERNAME, PASSWD);
+        String sql = "select count(*) from TEST_KYLIN_FACT; ";
+        HttpResponse result = client.query(sql, PROJECT_NAME);
+    }
+
+    protected static void stopJetty() throws Exception {
+        if (server != null)
+            server.stop();
+
+        File workFolder = new File("work");
+        if (workFolder.isDirectory() && workFolder.exists()) {
+            FileUtils.deleteDirectory(workFolder);
+        }
+    }
+
+    protected static void startJetty() throws Exception {
+
+        server = new Server(7070);
+
+        WebAppContext context = new WebAppContext();
+        context.setDescriptor("../server/src/main/webapp/WEB-INF/web.xml");
+        context.setResourceBase("../server/src/main/webapp");
+        context.setContextPath("/kylin");
+        context.setParentLoaderPriority(true);
+
+        server.setHandler(context);
+
+        server.start();
+
+    }
+
+    private static class SystemPropertiesOverride {
+        HashMap<String, String> backup = new HashMap<String, String>();
+
+        public void override(String key, String value) {
+            backup.put(key, System.getProperty(key));
+            System.setProperty(key, value);
+        }
+
+        public void restore() {
+            for (String key : backup.keySet()) {
+                String value = backup.get(key);
+                if (value == null)
+                    System.clearProperty(key);
+                else
+                    System.setProperty(key, value);
+            }
+            backup.clear();
+        }
+    }
+}