This is an automated email from the ASF dual-hosted git repository.

haonan 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 e2bfd156bea add path check example (#11336)
e2bfd156bea is described below

commit e2bfd156bea4e8d2552a5272e3be62eca56abbfb
Author: YuFengLiu <[email protected]>
AuthorDate: Thu Oct 19 17:12:56 2023 +0800

    add path check example (#11336)
---
 example/pom.xml                                    |   1 +
 example/schema/pom.xml                             |  44 +++++++
 .../org/apache/iotdb/schema/PathCheckExample.java  | 138 +++++++++++++++++++++
 3 files changed, 183 insertions(+)

diff --git a/example/pom.xml b/example/pom.xml
index f6b3a5e29ba..2680c44805c 100644
--- a/example/pom.xml
+++ b/example/pom.xml
@@ -45,6 +45,7 @@
         <module>rabbitmq</module>
         <module>rest-java-example</module>
         <module>flink-sql</module>
+        <module>schema</module>
     </modules>
     <build>
         <pluginManagement>
diff --git a/example/schema/pom.xml b/example/schema/pom.xml
new file mode 100644
index 00000000000..d23268c79a1
--- /dev/null
+++ b/example/schema/pom.xml
@@ -0,0 +1,44 @@
+<?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";>
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <artifactId>iotdb-examples</artifactId>
+        <groupId>org.apache.iotdb</groupId>
+        <version>1.3.0-SNAPSHOT</version>
+    </parent>
+    <artifactId>schema-example</artifactId>
+    <name>IoTDB: Example: Schema</name>
+    <dependencies>
+        <dependency>
+            <groupId>ch.qos.logback</groupId>
+            <artifactId>logback-classic</artifactId>
+            <version>1.3.11</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.iotdb</groupId>
+            <artifactId>tsfile</artifactId>
+            <version>1.3.0-SNAPSHOT</version>
+            <scope>compile</scope>
+        </dependency>
+    </dependencies>
+</project>
diff --git 
a/example/schema/src/main/java/org/apache/iotdb/schema/PathCheckExample.java 
b/example/schema/src/main/java/org/apache/iotdb/schema/PathCheckExample.java
new file mode 100644
index 00000000000..8ce64b88f0e
--- /dev/null
+++ b/example/schema/src/main/java/org/apache/iotdb/schema/PathCheckExample.java
@@ -0,0 +1,138 @@
+/*
+ * 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.schema;
+
+import org.apache.iotdb.tsfile.exception.PathParseException;
+import org.apache.iotdb.tsfile.read.common.parser.PathNodesGenerator;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Objects;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+
+/**
+ * Before creating paths in IoTDB, it's essential to check whether the paths 
are correct to avoid
+ * errors.
+ *
+ * <p>This example checks the paths . You can add paths to the inputList or 
read paths from files
+ * generated by export-csv.sh/export-csv.bat.
+ *
+ * <p>The usage of export-csv.sh can be found in the <a
+ * 
href="https://iotdb.apache.org/zh/UserGuide/V1.2.x/Tools-System/Import-Export-Tool.html#csv%E5%AF%BC%E5%85%A5%E5%AF%BC%E5%87%BA%E5%B7%A5%E5%85%B7";>documentation</a>.
+ * For example: ./export-csv.sh -h 127.0.0.1 -p 6667 -u root -pw root -td . -q 
"show timeseries"
+ */
+public class PathCheckExample {
+  private static final List<String> inputList = new ArrayList<>();
+  private static final String DIR = "/Users/root/iotdb/tools";
+
+  private static final Logger logger = 
LoggerFactory.getLogger(PathCheckExample.class);
+
+  // concurrent thread of path check
+  private static final int CONCURRENCY = 5;
+
+  public static void main(String[] args) {
+    batchCheck();
+    dirCheck();
+  }
+
+  private static void batchCheck() {
+    inputList.add("root.test.d1.s1");
+    inputList.add("root.b+.d1.s2");
+    inputList.add("root.test.1.s3");
+    inputList.add("root.test.d-j.s4");
+    inputList.add("root.test.'8`7'.s5");
+    inputList.add("root.test.`1`.s6");
+    inputList.add("root.test.\"d+b\".s7");
+    for (String path : inputList) {
+      checkPath(path);
+    }
+  }
+
+  // This function wile check whether the paths are correct in current version.
+  private static void checkPath(String path) {
+    try {
+      PathNodesGenerator.checkPath(path);
+    } catch (PathParseException e) {
+      logger.error("{} is not a legal path.", path);
+    }
+  }
+
+  /**
+   * Using multiple threads to check the paths in the files of the directory. 
the files are created
+   * by export-csv.sh/export-csv.bat.
+   */
+  public static void dirCheck() {
+
+    List<Future<Void>> futureList = new ArrayList<>();
+    ExecutorService executorService = 
Executors.newFixedThreadPool(CONCURRENCY);
+    File dir = new File(DIR);
+    for (File file : Objects.requireNonNull(dir.listFiles())) {
+      if (file.getName().startsWith("dump") && 
file.getName().endsWith(".csv")) {
+        Future<Void> future = executorService.submit(new CheckThread(file));
+        futureList.add(future);
+      }
+    }
+    try {
+      for (Future<Void> future : futureList) {
+        future.get();
+      }
+    } catch (InterruptedException | ExecutionException e) {
+      logger.error("Error when checking paths.");
+      Thread.currentThread().interrupt();
+    }
+    executorService.shutdown();
+  }
+
+  static class CheckThread implements Callable<Void> {
+    File file;
+
+    public CheckThread(File file) {
+      this.file = file;
+    }
+
+    @Override
+    public Void call() {
+      try (BufferedReader br = new BufferedReader(new FileReader(file))) {
+
+        String line = br.readLine();
+        while ((line = br.readLine()) != null) {
+          String[] split = line.split(",");
+          String path = split[0];
+          checkPath(path);
+        }
+      } catch (IOException e) {
+        logger.error("Error reading file: {}", file.getName());
+        throw new RuntimeException(e);
+      }
+      return null;
+    }
+  }
+}

Reply via email to