You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ge...@apache.org on 2020/02/06 15:37:21 UTC
[incubator-iotdb] 03/05: update rest
This is an automated email from the ASF dual-hosted git repository.
geniuspig pushed a commit to branch http
in repository https://gitbox.apache.org/repos/asf/incubator-iotdb.git
commit b0f9b1ce3ae699f5461110e17ea6c2d7173f6891
Author: zhutianci <zh...@gmail.com>
AuthorDate: Thu Feb 6 22:35:11 2020 +0800
update rest
---
server/pom.xml | 15 +++
.../iotdb/db/rest/controller/RestController.java | 121 +++---------------
.../apache/iotdb/db/rest/service/RestService.java | 87 ++++++++++++-
.../java/org/apache/iotdb/db/rest/RestTest.java | 139 +++++++++++++++++++++
4 files changed, 255 insertions(+), 107 deletions(-)
diff --git a/server/pom.xml b/server/pom.xml
index 180c31b..c10fef2 100644
--- a/server/pom.xml
+++ b/server/pom.xml
@@ -126,6 +126,21 @@
<artifactId>jersey-container-servlet</artifactId>
<version>2.30</version>
</dependency>
+ <dependency>
+ <groupId>org.glassfish.jersey.inject</groupId>
+ <artifactId>jersey-hk2</artifactId>
+ <version>2.30</version>
+ </dependency>
+ <dependency>
+ <groupId>org.glassfish.jersey.core</groupId>
+ <artifactId>jersey-client</artifactId>
+ <version>2.30</version>
+ </dependency>
+ <dependency>
+ <groupId>org.glassfish.jersey.media</groupId>
+ <artifactId>jersey-media-json-jackson</artifactId>
+ <version>2.30</version>
+ </dependency>
</dependencies>
<build>
<plugins>
diff --git a/server/src/main/java/org/apache/iotdb/db/rest/controller/RestController.java b/server/src/main/java/org/apache/iotdb/db/rest/controller/RestController.java
index c0ed5f8..80b7577 100644
--- a/server/src/main/java/org/apache/iotdb/db/rest/controller/RestController.java
+++ b/server/src/main/java/org/apache/iotdb/db/rest/controller/RestController.java
@@ -18,34 +18,22 @@
*/
package org.apache.iotdb.db.rest.controller;
-import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
-import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.sql.SQLException;
-import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
-import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
+import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import org.apache.iotdb.db.auth.AuthException;
import org.apache.iotdb.db.auth.authorizer.IAuthorizer;
import org.apache.iotdb.db.auth.authorizer.LocalFileAuthorizer;
import org.apache.iotdb.db.conf.IoTDBConstant;
-import org.apache.iotdb.db.exception.StorageEngineException;
-import org.apache.iotdb.db.exception.metadata.MetadataException;
-import org.apache.iotdb.db.exception.query.QueryProcessException;
-import org.apache.iotdb.db.exception.storageGroup.StorageGroupException;
-import org.apache.iotdb.db.rest.model.TimeValues;
import org.apache.iotdb.db.rest.service.RestService;
-import org.apache.iotdb.tsfile.exception.filter.QueryFilterOptimizationException;
import org.apache.iotdb.tsfile.utils.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -62,14 +50,16 @@ public class RestController {
/**
* http request to login IoTDB
- * @param username username for login IoTDB
- * @param password password for login IoTDB
*/
- @Path("/login/{username}&&{password}")
+ @Path("/login")
@POST
- public void login(@PathParam("username") String username, @PathParam("password") String password)
+ @Consumes(MediaType.TEXT_PLAIN)
+ public void login(@Context HttpServletRequest request)
throws AuthException {
+ JSONObject jsonObject = restService.getRequestBodyJson(request);
+ String username = (String)jsonObject.get("username");
+ String password = (String)jsonObject.get("password");
logger.info("{}: receive http request from username {}", IoTDBConstant.GLOBAL_DB_NAME,
username);
IAuthorizer authorizer = LocalFileAuthorizer.getInstance();
@@ -85,18 +75,16 @@ public class RestController {
/**
*
* @param request this request will be in json format.
- * @param response this response will be in json format.
* @return json in String
*/
@Path("/query")
- @GET
- @Produces(MediaType.APPLICATION_JSON)
- @Consumes(MediaType.APPLICATION_JSON)
- public String query(HttpServletRequest request, HttpServletResponse response) {
+ @POST
+ @Produces(MediaType.TEXT_PLAIN)
+ @Consumes(MediaType.TEXT_PLAIN)
+ public String query(@Context HttpServletRequest request) {
String targetStr = "target";
- response.setStatus(200);
try {
- JSONObject jsonObject = getRequestBodyJson(request);
+ JSONObject jsonObject = restService.getRequestBodyJson(request);
assert jsonObject != null;
JSONObject range = (JSONObject) jsonObject.get("range");
Pair<String, String> timeRange = new Pair<>((String) range.get("from"), (String) range.get("to"));
@@ -108,13 +96,13 @@ public class RestController {
return "[]";
}
String target = (String) object.get(targetStr);
- String type = getJsonType(jsonObject);
+ String type = restService.getJsonType(jsonObject);
JSONObject obj = new JSONObject();
obj.put("target", target);
if (type.equals("table")) {
- setJsonTable(obj, target, timeRange);
+ restService.setJsonTable(obj, target, timeRange);
} else if (type.equals("timeserie")) {
- setJsonTimeseries(obj, target, timeRange);
+ restService.setJsonTimeseries(obj, target, timeRange);
}
result.add(i, obj);
}
@@ -125,83 +113,4 @@ public class RestController {
}
return null;
}
-
- /**
- * get request body JSON.
- *
- * @param request http request
- * @return request JSON
- * @throws JSONException JSONException
- */
- private JSONObject getRequestBodyJson(HttpServletRequest request) throws JSONException {
- try {
- BufferedReader br = request.getReader();
- StringBuilder sb = new StringBuilder();
- String line;
- while ((line = br.readLine()) != null) {
- sb.append(line);
- }
- return JSON.parseObject(sb.toString());
- } catch (IOException e) {
- logger.error("getRequestBodyJson failed", e);
- }
- return null;
- }
-
- /**
- * get JSON type of input JSON object.
- *
- * @param jsonObject JSON Object
- * @return type (string)
- * @throws JSONException JSONException
- */
- private String getJsonType(JSONObject jsonObject) throws JSONException {
- JSONArray array = (JSONArray) jsonObject.get("targets"); // []
- JSONObject object = (JSONObject) array.get(0); // {}
- return (String) object.get("type");
- }
-
- private void setJsonTable(JSONObject obj, String target,
- Pair<String, String> timeRange)
- throws JSONException, StorageEngineException, QueryFilterOptimizationException,
- MetadataException, IOException, StorageGroupException, SQLException, QueryProcessException, AuthException {
- List<TimeValues> timeValues = restService.querySeries(target, timeRange);
- JSONArray columns = new JSONArray();
- JSONObject column = new JSONObject();
- column.put("text", "Time");
- column.put("type", "time");
- columns.add(column);
- column = new JSONObject();
- column.put("text", "Number");
- column.put("type", "number");
- columns.add(column);
- obj.put("columns", columns);
- JSONArray values = new JSONArray();
- for (TimeValues tv : timeValues) {
- JSONArray value = new JSONArray();
- value.add(tv.getTime());
- value.add(tv.getValue());
- values.add(value);
- }
- obj.put("values", values);
- }
-
- private void setJsonTimeseries(JSONObject obj, String target,
- Pair<String, String> timeRange)
- throws JSONException, StorageEngineException, QueryFilterOptimizationException,
- MetadataException, IOException, StorageGroupException, SQLException, QueryProcessException, AuthException {
- List<TimeValues> timeValues = restService.querySeries(target, timeRange);
- logger.info("query size: {}", timeValues.size());
- JSONArray dataPoints = new JSONArray();
- for (TimeValues tv : timeValues) {
- long time = tv.getTime();
- String value = tv.getValue();
- JSONArray jsonArray = new JSONArray();
- jsonArray.add(value);
- jsonArray.add(time);
- dataPoints.add(jsonArray);
- }
- obj.put("datapoints", dataPoints);
- }
-
}
diff --git a/server/src/main/java/org/apache/iotdb/db/rest/service/RestService.java b/server/src/main/java/org/apache/iotdb/db/rest/service/RestService.java
index 3843555..7cec82c 100644
--- a/server/src/main/java/org/apache/iotdb/db/rest/service/RestService.java
+++ b/server/src/main/java/org/apache/iotdb/db/rest/service/RestService.java
@@ -18,10 +18,17 @@
*/
package org.apache.iotdb.db.rest.service;
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONException;
+import com.alibaba.fastjson.JSONObject;
+import java.io.BufferedReader;
import java.io.IOException;
+import java.io.InputStreamReader;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
+import javax.servlet.http.HttpServletRequest;
import org.apache.iotdb.db.auth.AuthException;
import org.apache.iotdb.db.auth.AuthorityChecker;
import org.apache.iotdb.db.conf.IoTDBConstant;
@@ -62,7 +69,7 @@ public class RestService {
private String username;
- public List<TimeValues> querySeries(String s, Pair<String, String> timeRange)
+ private List<TimeValues> querySeries(String s, Pair<String, String> timeRange)
throws QueryProcessException, StorageGroupException, AuthException, MetadataException, QueryFilterOptimizationException, SQLException, StorageEngineException, IOException {
String from = timeRange.left;
String to = timeRange.right;
@@ -174,4 +181,82 @@ public class RestService {
public void setUsername(String username) {
this.username = username;
}
+
+ /**
+ * get request body JSON.
+ *
+ * @param request http request
+ * @return request JSON
+ * @throws JSONException JSONException
+ */
+ public JSONObject getRequestBodyJson(HttpServletRequest request) throws JSONException {
+ try {
+ BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
+ StringBuilder sb = new StringBuilder();
+ String line;
+ while ((line = br.readLine()) != null) {
+ sb.append(line);
+ }
+ return JSON.parseObject(sb.toString());
+ } catch (IOException e) {
+ logger.error("getRequestBodyJson failed", e);
+ }
+ return null;
+ }
+
+ /**
+ * get JSON type of input JSON object.
+ *
+ * @param jsonObject JSON Object
+ * @return type (string)
+ * @throws JSONException JSONException
+ */
+ public String getJsonType(JSONObject jsonObject) throws JSONException {
+ JSONArray array = (JSONArray) jsonObject.get("targets"); // []
+ JSONObject object = (JSONObject) array.get(0); // {}
+ return (String) object.get("type");
+ }
+
+ public void setJsonTable(JSONObject obj, String target,
+ Pair<String, String> timeRange)
+ throws JSONException, StorageEngineException, QueryFilterOptimizationException,
+ MetadataException, IOException, StorageGroupException, SQLException, QueryProcessException, AuthException {
+ List<TimeValues> timeValues = querySeries(target, timeRange);
+ JSONArray columns = new JSONArray();
+ JSONObject column = new JSONObject();
+ column.put("text", "Time");
+ column.put("type", "time");
+ columns.add(column);
+ column = new JSONObject();
+ column.put("text", "Number");
+ column.put("type", "number");
+ columns.add(column);
+ obj.put("columns", columns);
+ JSONArray values = new JSONArray();
+ for (TimeValues tv : timeValues) {
+ JSONArray value = new JSONArray();
+ value.add(tv.getTime());
+ value.add(tv.getValue());
+ values.add(value);
+ }
+ obj.put("values", values);
+ }
+
+ public void setJsonTimeseries(JSONObject obj, String target,
+ Pair<String, String> timeRange)
+ throws JSONException, StorageEngineException, QueryFilterOptimizationException,
+ MetadataException, IOException, StorageGroupException, SQLException, QueryProcessException, AuthException {
+ List<TimeValues> timeValues = querySeries(target, timeRange);
+ logger.info("query size: {}", timeValues.size());
+ JSONArray dataPoints = new JSONArray();
+ for (TimeValues tv : timeValues) {
+ long time = tv.getTime();
+ String value = tv.getValue();
+ JSONArray jsonArray = new JSONArray();
+ jsonArray.add(value);
+ jsonArray.add(time);
+ dataPoints.add(jsonArray);
+ }
+ obj.put("datapoints", dataPoints);
+ }
}
diff --git a/server/src/test/java/org/apache/iotdb/db/rest/RestTest.java b/server/src/test/java/org/apache/iotdb/db/rest/RestTest.java
new file mode 100644
index 0000000..4976364
--- /dev/null
+++ b/server/src/test/java/org/apache/iotdb/db/rest/RestTest.java
@@ -0,0 +1,139 @@
+package org.apache.iotdb.db.rest;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.Statement;
+import java.util.Locale;
+import javax.ws.rs.client.Client;
+import javax.ws.rs.client.ClientBuilder;
+import javax.ws.rs.client.Entity;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.utils.EnvironmentUtils;
+import org.apache.iotdb.jdbc.Config;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class RestTest {
+ private static final String REST_URI
+ = "http://localhost:8181/rest/query";
+
+ private static String[] creationSqls = new String[]{
+ "SET STORAGE GROUP TO root.vehicle.d0",
+ "SET STORAGE GROUP TO root.vehicle.d1",
+
+ "CREATE TIMESERIES root.vehicle.d0.s0 WITH DATATYPE=INT32, ENCODING=RLE",
+ "CREATE TIMESERIES root.vehicle.d0.s1 WITH DATATYPE=INT64, ENCODING=RLE",
+ "CREATE TIMESERIES root.vehicle.d0.s2 WITH DATATYPE=FLOAT, ENCODING=RLE",
+ "CREATE TIMESERIES root.vehicle.d0.s3 WITH DATATYPE=TEXT, ENCODING=PLAIN",
+ "CREATE TIMESERIES root.vehicle.d0.s4 WITH DATATYPE=BOOLEAN, ENCODING=PLAIN"
+ };
+ private static String[] dataSet2 = new String[]{
+ "SET STORAGE GROUP TO root.ln.wf01.wt01",
+ "CREATE TIMESERIES root.ln.wf01.wt01.status WITH DATATYPE=BOOLEAN, ENCODING=PLAIN",
+ "CREATE TIMESERIES root.ln.wf01.wt01.temperature WITH DATATYPE=FLOAT, ENCODING=PLAIN",
+ "CREATE TIMESERIES root.ln.wf01.wt01.hardware WITH DATATYPE=INT32, ENCODING=PLAIN",
+ "INSERT INTO root.ln.wf01.wt01(timestamp,temperature,status, hardware) "
+ + "values(1, 1.1, false, 11)",
+ "INSERT INTO root.ln.wf01.wt01(timestamp,temperature,status, hardware) "
+ + "values(2, 2.2, true, 22)",
+ "INSERT INTO root.ln.wf01.wt01(timestamp,temperature,status, hardware) "
+ + "values(3, 3.3, false, 33 )",
+ "INSERT INTO root.ln.wf01.wt01(timestamp,temperature,status, hardware) "
+ + "values(4, 4.4, false, 44)",
+ "INSERT INTO root.ln.wf01.wt01(timestamp,temperature,status, hardware) "
+ + "values(5, 5.5, false, 55)"
+ };
+
+ @Before
+ public void setUp() throws Exception {
+ EnvironmentUtils.closeStatMonitor();
+ EnvironmentUtils.envSetUp();
+ IoTDBDescriptor.getInstance().getConfig().setPartitionInterval(1000);
+ Class.forName(Config.JDBC_DRIVER_NAME);
+ prepareData();
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ IoTDBDescriptor.getInstance().getConfig().setPartitionInterval(86400);
+ EnvironmentUtils.cleanEnv();
+ }
+
+ private void prepareData() {
+ try (Connection connection = DriverManager
+ .getConnection(Config.IOTDB_URL_PREFIX + "127.0.0.1:6667/", "root",
+ "root");
+ Statement statement = connection.createStatement()) {
+
+ for (String sql : creationSqls) {
+ statement.execute(sql);
+ }
+
+ for (String sql : dataSet2) {
+ statement.execute(sql);
+ }
+
+ // prepare BufferWrite file
+ String insertTemplate = "INSERT INTO root.vehicle.d0(timestamp,s0,s1,s2,s3,s4)"
+ + " VALUES(%d,%d,%d,%f,%s,%s)";
+ for (int i = 5000; i < 7000; i++) {
+ statement.execute(String
+ .format(Locale.ENGLISH, insertTemplate, i, i, i, (double) i, "\'" + i + "\'", "true"));
+ }
+ statement.execute("flush");
+ for (int i = 7500; i < 8500; i++) {
+ statement.execute(String
+ .format(Locale.ENGLISH, insertTemplate, i, i, i, (double) i, "\'" + i + "\'", "false"));
+ }
+ statement.execute("flush");
+ // prepare Unseq-File
+ for (int i = 500; i < 1500; i++) {
+ statement.execute(String
+ .format(Locale.ENGLISH, insertTemplate, i, i, i, (double) i, "\'" + i + "\'", "true"));
+ }
+ statement.execute("flush");
+ for (int i = 3000; i < 6500; i++) {
+ statement.execute(String
+ .format(Locale.ENGLISH, insertTemplate, i, i, i, (double) i, "\'" + i + "\'", "false"));
+ }
+ statement.execute("merge");
+
+ // prepare BufferWrite cache
+ for (int i = 9000; i < 10000; i++) {
+ statement.execute(String
+ .format(Locale.ENGLISH, insertTemplate, i, i, i, (double) i, "\'" + i + "\'", "true"));
+ }
+ // prepare Overflow cache
+ for (int i = 2000; i < 2500; i++) {
+ statement.execute(String
+ .format(Locale.ENGLISH, insertTemplate, i, i, i, (double) i, "\'" + i + "\'", "false"));
+ }
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ @Test
+ public void testQuery() {
+ Client client = ClientBuilder.newClient();
+ String json = "{\n"
+ + " \"range\": {\n"
+ + " \"from\": \"1\",\n"
+ + " \"to\": \"300\",\n"
+ + " },\n"
+ + " \n"
+ + " \"targets\": [\n"
+ + " { \"target\": \"root.ln.wf01.wt01\", \"type\": \"timeserie\" },\n"
+ + " ]\n"
+ + "}";
+ Response response = client.target(REST_URI)
+ .request(MediaType.TEXT_PLAIN)
+ .post(Entity.entity(json, MediaType.TEXT_PLAIN));
+ String result = response.readEntity(String.class);
+ System.out.println(result);
+ }
+}