This is an automated email from the ASF dual-hosted git repository.
qiaojialin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git
The following commit(s) were added to refs/heads/master by this push:
new ed439a4c4a rest java example (#6274)
ed439a4c4a is described below
commit ed439a4c4afc88b226d775f7ad775ab9d6300245
Author: CloudWise-Lukemiao
<[email protected]>
AuthorDate: Thu Jun 16 20:47:16 2022 +0800
rest java example (#6274)
---
example/rest-java-example/pom.xml | 49 +++++++
.../main/java/org/apache/iotdb/HttpExample.java | 141 ++++++++++++++++++++
.../main/java/org/apache/iotdb/HttpsExample.java | 144 +++++++++++++++++++++
.../src/main/java/org/apache/iotdb/SSLClient.java | 83 ++++++++++++
4 files changed, 417 insertions(+)
diff --git a/example/rest-java-example/pom.xml
b/example/rest-java-example/pom.xml
new file mode 100644
index 0000000000..499ca85a9e
--- /dev/null
+++ b/example/rest-java-example/pom.xml
@@ -0,0 +1,49 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+ 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.
+
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <parent>
+ <artifactId>iotdb-examples</artifactId>
+ <groupId>org.apache.iotdb</groupId>
+ <version>0.14.0-SNAPSHOT</version>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+ <artifactId>rest-java-example</artifactId>
+ <properties>
+ <maven.compiler.source>1.8</maven.compiler.source>
+ <maven.compiler.target>1.8</maven.compiler.target>
+ </properties>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.httpcomponents</groupId>
+ <artifactId>httpclient</artifactId>
+ <scope>compile</scope>
+ </dependency>
+ <dependency>
+ <groupId>com.google.code.gson</groupId>
+ <artifactId>gson</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>com.fasterxml.jackson.core</groupId>
+ <artifactId>jackson-databind</artifactId>
+ </dependency>
+ </dependencies>
+</project>
diff --git
a/example/rest-java-example/src/main/java/org/apache/iotdb/HttpExample.java
b/example/rest-java-example/src/main/java/org/apache/iotdb/HttpExample.java
new file mode 100644
index 0000000000..532a266594
--- /dev/null
+++ b/example/rest-java-example/src/main/java/org/apache/iotdb/HttpExample.java
@@ -0,0 +1,141 @@
+/*
+ * 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.iotdb;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
+import org.apache.http.HttpEntity;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.util.EntityUtils;
+
+import java.io.IOException;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.util.Base64;
+import java.util.Map;
+
+public class HttpExample {
+ private String getAuthorization(String username, String password) {
+ return Base64.getEncoder()
+ .encodeToString((username + ":" +
password).getBytes(StandardCharsets.UTF_8));
+ }
+
+ public static void main(String[] args) {
+ HttpExample httpExample = new HttpExample();
+ httpExample.ping();
+ httpExample.insertTablet();
+ httpExample.query();
+ }
+
+ public void ping() {
+ CloseableHttpClient httpClient = SSLClient.getInstance().getHttpClient();
+ HttpGet httpGet = new HttpGet("http://127.0.0.1:18080/ping");
+ CloseableHttpResponse response = null;
+ try {
+ response = httpClient.execute(httpGet);
+ HttpEntity responseEntity = response.getEntity();
+ String message = EntityUtils.toString(responseEntity, "utf-8");
+ JsonObject result = JsonParser.parseString(message).getAsJsonObject();
+ System.out.println(result);
+ } catch (IOException e) {
+ e.printStackTrace();
+
+ } finally {
+ try {
+ if (httpClient != null) {
+ httpClient.close();
+ }
+ if (response != null) {
+ response.close();
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+
+ private HttpPost getHttpPost(String url) {
+ HttpPost httpPost = new HttpPost(url);
+ httpPost.addHeader("Content-type", "application/json; charset=utf-8");
+ httpPost.setHeader("Accept", "application/json");
+ String authorization = getAuthorization("root", "root");
+ httpPost.setHeader("Authorization", authorization);
+ return httpPost;
+ }
+
+ public void insertTablet() {
+ CloseableHttpClient httpClient = SSLClient.getInstance().getHttpClient();
+ CloseableHttpResponse response = null;
+ try {
+ HttpPost httpPost =
getHttpPost("http://127.0.0.1:18080/rest/v1/insertTablet");
+ String json =
+
"{\"timestamps\":[1635232143960,1635232153960],\"measurements\":[\"s3\",\"s4\",\"s5\",\"s6\",\"s7\",\"s8\"],\"dataTypes\":[\"TEXT\",\"INT32\",\"INT64\",\"FLOAT\",\"BOOLEAN\",\"DOUBLE\"],\"values\":[[\"2aa\",\"\"],[11,2],[1635000012345555,1635000012345556],[1.41,null],[null,false],[null,3.5555]],\"isAligned\":false,\"deviceId\":\"root.sg25\"}";
+ httpPost.setEntity(new StringEntity(json, Charset.defaultCharset()));
+ response = httpClient.execute(httpPost);
+ HttpEntity responseEntity = response.getEntity();
+ String message = EntityUtils.toString(responseEntity, "utf-8");
+ JsonObject result = JsonParser.parseString(message).getAsJsonObject();
+ System.out.println(result);
+ } catch (IOException e) {
+ e.printStackTrace();
+
+ } finally {
+ try {
+ if (response != null) {
+ response.close();
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ public void query() {
+ CloseableHttpClient httpClient = SSLClient.getInstance().getHttpClient();
+ CloseableHttpResponse response = null;
+ try {
+ HttpPost httpPost = getHttpPost("http://127.0.0.1:18080/rest/v1/query");
+ String sql = "{\"sql\":\"select *,s4+1,s4+1 from root.sg25\"}";
+ httpPost.setEntity(new StringEntity(sql, Charset.defaultCharset()));
+ response = httpClient.execute(httpPost);
+ HttpEntity responseEntity = response.getEntity();
+ String message = EntityUtils.toString(responseEntity, "utf-8");
+ ObjectMapper mapper = new ObjectMapper();
+ Map map = mapper.readValue(message, Map.class);
+ System.out.println(map);
+ } catch (IOException e) {
+ e.printStackTrace();
+ } finally {
+ try {
+ if (response != null) {
+ response.close();
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+}
diff --git
a/example/rest-java-example/src/main/java/org/apache/iotdb/HttpsExample.java
b/example/rest-java-example/src/main/java/org/apache/iotdb/HttpsExample.java
new file mode 100644
index 0000000000..d9932dc9e4
--- /dev/null
+++ b/example/rest-java-example/src/main/java/org/apache/iotdb/HttpsExample.java
@@ -0,0 +1,144 @@
+/*
+ * 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.iotdb;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
+import java.io.IOException;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.util.Base64;
+import java.util.Map;
+import org.apache.http.HttpEntity;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.util.EntityUtils;
+
+public class HttpsExample {
+
+ private String getAuthorization(String username, String password) {
+ return Base64.getEncoder()
+ .encodeToString((username + ":" +
password).getBytes(StandardCharsets.UTF_8));
+ }
+
+ public static void main(String[] args) throws Exception {
+ HttpsExample httpsExample=new HttpsExample();
+ httpsExample.pingHttps();
+ httpsExample.insertTablet();
+ httpsExample.query();
+
+ }
+
+ public void pingHttps() throws Exception {
+ CloseableHttpClient httpClient = SSLClient.getInstance().getHttpClient();
+ HttpGet httpGet = new HttpGet("https://127.0.0.1:18080/ping");
+ CloseableHttpResponse response = null;
+ try {
+ response = httpClient.execute(httpGet);
+ HttpEntity responseEntity = response.getEntity();
+ String message = EntityUtils.toString(responseEntity, "utf-8");
+ JsonObject result = JsonParser.parseString(message).getAsJsonObject();
+ System.out.println(result);
+
+ } catch (IOException e) {
+ e.printStackTrace();
+
+ } finally {
+ try {
+ if (httpClient != null) {
+ httpClient.close();
+ }
+ if (response != null) {
+ response.close();
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+
+ }
+ }
+ }
+
+ private HttpPost getHttpPost(String url) {
+ HttpPost httpPost = new HttpPost(url);
+ httpPost.addHeader("Content-type", "application/json; charset=utf-8");
+ httpPost.setHeader("Accept", "application/json");
+ String authorization = getAuthorization("root", "root");
+ httpPost.setHeader("Authorization", authorization);
+ return httpPost;
+ }
+
+ public void insertTablet() {
+ CloseableHttpClient httpClient = SSLClient.getInstance().getHttpClient();
+ CloseableHttpResponse response = null;
+ try {
+ HttpPost httpPost =
getHttpPost("https://127.0.0.1:18080/rest/v1/insertTablet");
+ String json =
+
"{\"timestamps\":[1635232143960,1635232153960],\"measurements\":[\"s3\",\"s4\",\"s5\",\"s6\",\"s7\",\"s8\"],\"dataTypes\":[\"TEXT\",\"INT32\",\"INT64\",\"FLOAT\",\"BOOLEAN\",\"DOUBLE\"],\"values\":[[\"2aa\",\"\"],[11,2],[1635000012345555,1635000012345556],[1.41,null],[null,false],[null,3.5555]],\"isAligned\":false,\"deviceId\":\"root.sg25\"}";
+ httpPost.setEntity(new StringEntity(json, Charset.defaultCharset()));
+ response = httpClient.execute(httpPost);
+ HttpEntity responseEntity = response.getEntity();
+ String message = EntityUtils.toString(responseEntity, "utf-8");
+ JsonObject result = JsonParser.parseString(message).getAsJsonObject();
+ System.out.println(result);
+ } catch (IOException e) {
+ e.printStackTrace();
+
+ } finally {
+ try {
+ if (response != null) {
+ response.close();
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ public void query() {
+ CloseableHttpClient httpClient = SSLClient.getInstance().getHttpClient();
+ CloseableHttpResponse response = null;
+ try {
+ HttpPost httpPost = getHttpPost("https://127.0.0.1:18080/rest/v1/query");
+ String sql = "{\"sql\":\"select *,s4+1,s4+1 from root.sg25\"}";
+ httpPost.setEntity(new StringEntity(sql, Charset.defaultCharset()));
+ response = httpClient.execute(httpPost);
+ HttpEntity responseEntity = response.getEntity();
+ String message = EntityUtils.toString(responseEntity, "utf-8");
+ ObjectMapper mapper = new ObjectMapper();
+ Map map = mapper.readValue(message, Map.class);
+ System.out.println(map);
+ } catch (IOException e) {
+ e.printStackTrace();
+ } finally {
+ try {
+ if (response != null) {
+ response.close();
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+}
diff --git
a/example/rest-java-example/src/main/java/org/apache/iotdb/SSLClient.java
b/example/rest-java-example/src/main/java/org/apache/iotdb/SSLClient.java
new file mode 100644
index 0000000000..faad2866cc
--- /dev/null
+++ b/example/rest-java-example/src/main/java/org/apache/iotdb/SSLClient.java
@@ -0,0 +1,83 @@
+/*
+ * 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.iotdb;
+
+import java.security.KeyManagementException;
+import java.security.KeyStoreException;
+import java.security.NoSuchAlgorithmException;
+import java.security.cert.CertificateException;
+import java.security.cert.X509Certificate;
+import org.apache.http.config.Registry;
+import org.apache.http.config.RegistryBuilder;
+import org.apache.http.conn.socket.ConnectionSocketFactory;
+import org.apache.http.conn.socket.PlainConnectionSocketFactory;
+import org.apache.http.conn.ssl.NoopHostnameVerifier;
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
+import org.apache.http.conn.ssl.TrustStrategy;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
+import org.apache.http.ssl.SSLContextBuilder;
+
+public class SSLClient {
+
+ private static SSLConnectionSocketFactory sslConnectionSocketFactory = null;
+ private static PoolingHttpClientConnectionManager
poolingHttpClientConnectionManager = null;
+ private static SSLContextBuilder sslContextBuilder = null;
+ private static ConnectionSocketFactory plainsf=null;
+
+ private static class SSLClientInstance {
+ private static final SSLClient instance = new SSLClient();
+ }
+
+ public static SSLClient getInstance() {
+ return SSLClientInstance.instance;
+ }
+ private SSLClient() {
+ try {
+ sslContextBuilder = new SSLContextBuilder().loadTrustMaterial(null, new
TrustStrategy() {
+ @Override
+ public boolean isTrusted(X509Certificate[] x509Certificates, String s)
throws CertificateException {
+ return true;
+ }
+ });
+ plainsf = PlainConnectionSocketFactory
+ .getSocketFactory();
+ sslConnectionSocketFactory = new
SSLConnectionSocketFactory(sslContextBuilder.build(), new String[]{"TLSv1.3"},
null, NoopHostnameVerifier.INSTANCE);
+ Registry<ConnectionSocketFactory> registryBuilder =
RegistryBuilder.<ConnectionSocketFactory>create()
+ .register("http", plainsf)
+ .register("https", sslConnectionSocketFactory)
+ .build();
+ poolingHttpClientConnectionManager = new
PoolingHttpClientConnectionManager(registryBuilder);
+ poolingHttpClientConnectionManager.setMaxTotal(10);
+ } catch (NoSuchAlgorithmException|KeyStoreException|KeyManagementException
e) {
+ e.printStackTrace();
+ }
+ }
+
+
+ public CloseableHttpClient getHttpClient() {
+ CloseableHttpClient httpClient =
HttpClients.custom().setSSLSocketFactory(sslConnectionSocketFactory)
+ .setConnectionManager(poolingHttpClientConnectionManager)
+ .setConnectionManagerShared(true)
+ .build();
+ return httpClient;
+ }
+}