You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by qi...@apache.org on 2022/12/13 01:58:46 UTC

[iotdb] branch rel/0.13 updated: [To rel/0.13][IOTDB-5133] Export Schema Tool with Script (#8422)

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

qiaojialin pushed a commit to branch rel/0.13
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/rel/0.13 by this push:
     new 8f5e5b23b0 [To rel/0.13][IOTDB-5133] Export Schema Tool with Script  (#8422)
8f5e5b23b0 is described below

commit 8f5e5b23b0cf29f0d06cafcf57116e3147fd2f51
Author: Chen YZ <43...@users.noreply.github.com>
AuthorDate: Tue Dec 13 09:58:39 2022 +0800

    [To rel/0.13][IOTDB-5133] Export Schema Tool with Script  (#8422)
---
 docs/UserGuide/Operate-Metadata/Export-Schema.md   |  21 +--
 .../zh/UserGuide/Operate-Metadata/Export-Schema.md |  25 ++--
 .../iotdb/db/integration/IoTDBExportSchemaIT.java  |  22 +++
 .../apache/iotdb/db/tools/mlog/ExportSchema.java   | 152 ++++++++++++---------
 4 files changed, 133 insertions(+), 87 deletions(-)

diff --git a/docs/UserGuide/Operate-Metadata/Export-Schema.md b/docs/UserGuide/Operate-Metadata/Export-Schema.md
index 785ca1922f..2716fc6805 100644
--- a/docs/UserGuide/Operate-Metadata/Export-Schema.md
+++ b/docs/UserGuide/Operate-Metadata/Export-Schema.md
@@ -42,12 +42,19 @@ Windows
 > ./exportSchema.bat-d /yourpath/data/system/schema -o /yourpath/targetDir
 
 
-Source directory and the export destination directory need to be specified when exporting metadata using scripting.
+Export destination directory on server need to be specified when exporting metadata using scripting.
 ```
-usage: ExportSchema -d <source directory path> -o <target directory path>
-       [-help]
- -d <source directory path>   Need to specify a source directory path
- -o <target directory path>   Need to specify a target directory path
+usage: ExportSchema -o <target directory path> [-h <host address>] [-p <port>] [-u <user>] [-pw <password>] [-help]
+ -o <target directory path>   Need to specify a target directory path on
+                              server(required)
+ -h <host address>            Could specify a specify the IoTDB host
+                              address, default is 127.0.0.1 (optional)
+ -p <port>                    Could specify a specify the IoTDB port,
+                              default is 6667 (optional)
+ -u <user>                    Could specify the IoTDB user name, default
+                              is root (optional)
+ -pw <password>               Could specify the IoTDB password, default is
+                              root (optional)
  -help,--help                 Display help information
 ```
 
@@ -57,10 +64,6 @@ usage: ExportSchema -d <source directory path> -o <target directory path>
     * It may be because the environment variable $IOTDB_HOME is not set, please set the environment variable and try again
 * Encounter an error, because: File ... already exist.
     * There is already a mlog.bin or tlog.txt file in the target directory, please check the target directory and try again
-* Encounter an error, because: ... does not exist or is not a directory.
-    * The source directory path does not exist or is not a directory, please check the source directory and try again
-* Encounter an error, because: ... is not a valid directory.
-    * The source directory is not the schema directory in IoTDB, please check the target directory and try again
 
 
 ### Load Schema
diff --git a/docs/zh/UserGuide/Operate-Metadata/Export-Schema.md b/docs/zh/UserGuide/Operate-Metadata/Export-Schema.md
index 6198338b67..749da80c5d 100644
--- a/docs/zh/UserGuide/Operate-Metadata/Export-Schema.md
+++ b/docs/zh/UserGuide/Operate-Metadata/Export-Schema.md
@@ -36,18 +36,25 @@ EXPORT SCHEMA '<path/dir>'
 
 Linux/MacOS
 
-> ./exportSchema.sh -d /yourpath/data/system/schema -o /yourpath/targetDir
+> ./exportSchema.sh -o /yourpath/targetDir -h 127.0.0.1 -p 6667 -u root -pw root
 
 Windows
 
-> ./exportSchema.bat-d /yourpath/data/system/schema -o /yourpath/targetDir
+> ./exportSchema.bat -o /yourpath/targetDir -h 127.0.0.1 -p 6667 -u root -pw root
 
-使用脚本方式导出元数据时候,需要指定 IoTDB 的元数据文件目录与导出目标目录:
+使用脚本方式导出元数据时候,需要指定 IoTDB 元数据文件的导出目标目录(位于 IoTDB 服务器上):
 ```
-usage: ExportSchema -d <source directory path> -o <target directory path>
-       [-help]
- -d <source directory path>   Need to specify a source directory path
- -o <target directory path>   Need to specify a target directory path
+usage: ExportSchema -o <target directory path> [-h <host address>] [-p <port>] [-u <user>] [-pw <password>] [-help]
+ -o <target directory path>   Need to specify a target directory path on
+                              server(required)
+ -h <host address>            Could specify a specify the IoTDB host
+                              address, default is 127.0.0.1 (optional)
+ -p <port>                    Could specify a specify the IoTDB port,
+                              default is 6667 (optional)
+ -u <user>                    Could specify the IoTDB user name, default
+                              is root (optional)
+ -pw <password>               Could specify the IoTDB password, default is
+                              root (optional)
  -help,--help                 Display help information
 ```
 
@@ -57,10 +64,6 @@ usage: ExportSchema -d <source directory path> -o <target directory path>
     * 可能是由于未设置环境变量 $IOTDB_HOME,请设置环境变量之后重试
 * Encounter an error, because: File ... already exist.
     * 目标目录下已有 mlog.bin 或者 tlog.txt 文件,请检查目标目录之后重试
-* Encounter an error, because: ... does not exist or is not a directory.
-    * 源目录路径不存在或不是目录,请检查源目录之后重试
-* Encounter an error, because: ... is not a valid directory.
-    * 源目录不是 IoTDB 中的元数据文件目录,请检查目标目录之后重试
 
 ## 元数据加载操作
 
diff --git a/integration/src/test/java/org/apache/iotdb/db/integration/IoTDBExportSchemaIT.java b/integration/src/test/java/org/apache/iotdb/db/integration/IoTDBExportSchemaIT.java
index b2deab90db..08b067b163 100644
--- a/integration/src/test/java/org/apache/iotdb/db/integration/IoTDBExportSchemaIT.java
+++ b/integration/src/test/java/org/apache/iotdb/db/integration/IoTDBExportSchemaIT.java
@@ -19,6 +19,7 @@
 package org.apache.iotdb.db.integration;
 
 import org.apache.iotdb.db.metadata.MetadataConstant;
+import org.apache.iotdb.db.tools.mlog.ExportSchema;
 import org.apache.iotdb.db.tools.mlog.MLogLoader;
 import org.apache.iotdb.db.utils.EnvironmentUtils;
 import org.apache.iotdb.itbase.category.LocalStandaloneTest;
@@ -288,4 +289,25 @@ public class IoTDBExportSchemaIT {
         });
     checkExpectedResult();
   }
+
+  @Test
+  public void testExportSchemaAndLoadWithScript() throws Exception {
+    saveExpectedResult();
+
+    String[] args = new String[] {"-o", targetDir.getAbsolutePath()};
+    ExportSchema.main(args);
+
+    File[] files = targetDir.listFiles();
+    Assert.assertNotEquals(0, files.length);
+    cleanAndRestart();
+    // load mlog
+    MLogLoader.main(
+        new String[] {
+          "-mlog",
+          targetDir.getAbsolutePath() + "/" + MetadataConstant.METADATA_LOG,
+          "-tlog",
+          targetDir.getAbsolutePath() + "/" + MetadataConstant.TAG_LOG
+        });
+    checkExpectedResult();
+  }
 }
diff --git a/server/src/main/java/org/apache/iotdb/db/tools/mlog/ExportSchema.java b/server/src/main/java/org/apache/iotdb/db/tools/mlog/ExportSchema.java
index 4ab6701bad..0dd972d23d 100644
--- a/server/src/main/java/org/apache/iotdb/db/tools/mlog/ExportSchema.java
+++ b/server/src/main/java/org/apache/iotdb/db/tools/mlog/ExportSchema.java
@@ -18,9 +18,10 @@
  */
 package org.apache.iotdb.db.tools.mlog;
 
-import org.apache.iotdb.db.conf.IoTDBDescriptor;
-import org.apache.iotdb.db.metadata.MManager;
-import org.apache.iotdb.db.metadata.MetadataConstant;
+import org.apache.iotdb.rpc.IoTDBConnectionException;
+import org.apache.iotdb.rpc.StatementExecutionException;
+import org.apache.iotdb.session.Session;
+import org.apache.iotdb.session.util.Version;
 
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.CommandLineParser;
@@ -32,29 +33,28 @@ import org.apache.commons.cli.ParseException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.io.File;
-
 public class ExportSchema {
   private static final Logger logger = LoggerFactory.getLogger(ExportSchema.class);
 
   private static final String EXPORT_PREFIX = "ExportSchema";
 
-  private static final String SOURCE_DIR_ARGS = "d";
-  private static final String SOURCE_DIR_NAME = "source directory path";
+  private static final String HOST_ARGS = "h";
+  private static final String HOST_NAME = "host address";
+
+  private static final String PORT_ARGS = "p";
+  private static final String PORT_NAME = "port";
+
+  private static final String USER_ARGS = "u";
+  private static final String USER_NAME = "user";
+
+  private static final String PASSWORD_ARGS = "pw";
+  private static final String PASSWORD_NAME = "password";
 
   private static final String TARGET_DIR_ARGS = "o";
   private static final String TARGET_DIR_NAME = "target directory path";
 
   private static final String HELP_ARGS = "help";
 
-  private final String sourceDir;
-  private final String targetDir;
-
-  private ExportSchema(String sourceDir, String targetDir) {
-    this.sourceDir = sourceDir;
-    this.targetDir = targetDir;
-  }
-
   /**
    * create the commandline options.
    *
@@ -63,24 +63,51 @@ public class ExportSchema {
   public static Options createOptions() {
     Options options = new Options();
 
-    Option sourceDir =
-        Option.builder(SOURCE_DIR_ARGS)
-            .required()
-            .argName(SOURCE_DIR_NAME)
-            .hasArg()
-            .desc("Need to specify a source directory path")
-            .build();
-    options.addOption(sourceDir);
-
     Option targetDir =
         Option.builder(TARGET_DIR_ARGS)
             .required()
             .argName(TARGET_DIR_NAME)
             .hasArg()
-            .desc("Need to specify a target directory path")
+            .desc("Need to specify a target directory path on server(required)")
             .build();
     options.addOption(targetDir);
 
+    Option opHost =
+        Option.builder(HOST_ARGS)
+            .required(false)
+            .argName(HOST_NAME)
+            .hasArg()
+            .desc("Could specify a specify the IoTDB host address, default is 127.0.0.1 (optional)")
+            .build();
+    options.addOption(opHost);
+
+    Option opPort =
+        Option.builder(PORT_ARGS)
+            .required(false)
+            .argName(PORT_NAME)
+            .hasArg()
+            .desc("Could specify a specify the IoTDB port, default is 6667 (optional)")
+            .build();
+    options.addOption(opPort);
+
+    Option opUser =
+        Option.builder(USER_ARGS)
+            .required(false)
+            .argName(USER_NAME)
+            .hasArg()
+            .desc("Could specify the IoTDB user name, default is root (optional)")
+            .build();
+    options.addOption(opUser);
+
+    Option opPw =
+        Option.builder(PASSWORD_ARGS)
+            .required(false)
+            .argName(PASSWORD_NAME)
+            .hasArg()
+            .desc("Could specify the IoTDB password, default is root (optional)")
+            .build();
+    options.addOption(opPw);
+
     Option opHelp =
         Option.builder(HELP_ARGS)
             .longOpt(HELP_ARGS)
@@ -92,14 +119,7 @@ public class ExportSchema {
     return options;
   }
 
-  public static void main(String[] args) {
-    //    args =
-    //        new String[] {
-    //          "-d",
-    //          "/Users/chenyanze/Desktop/exportSchema",
-    //          "-o",
-    //          "/Users/chenyanze/projects/JavaProjects/iotdb/iotdb/data/system/schema"
-    //        };
+  public static void main(String[] args) throws IoTDBConnectionException {
     Options options = createOptions();
     HelpFormatter hf = new HelpFormatter();
     hf.setOptionComparator(null);
@@ -122,41 +142,39 @@ public class ExportSchema {
       return;
     }
 
-    String sourceDir = commandLine.getOptionValue(SOURCE_DIR_ARGS);
-    String targetDir = commandLine.getOptionValue(TARGET_DIR_ARGS);
-    File srcDir = new File(sourceDir);
-    if (!srcDir.exists() || !srcDir.isDirectory()) {
-      logger.error(
-          "Encounter an error, because: {} does not exist or is not a directory.", sourceDir);
-    } else {
-      File[] files =
-          srcDir.listFiles(
-              (dir, name) ->
-                  MetadataConstant.METADATA_LOG.equals(name)
-                      || MetadataConstant.TAG_LOG.equals(name));
-      if (files == null || files.length != 2) {
-        logger.error("Encounter an error, because: {} is not a valid directory.", sourceDir);
-      } else {
-        ExportSchema exportSchema = new ExportSchema(sourceDir, targetDir);
-        try {
-          exportSchema.export();
-        } catch (Exception e) {
-          logger.error("Encounter an error, because: {} ", e.getMessage());
-        } finally {
-          exportSchema.clear();
-        }
-      }
+    String host = commandLine.getOptionValue(HOST_ARGS);
+    if (host == null) {
+      host = "127.0.0.1";
     }
-  }
-
-  private void export() throws Exception {
-
-    IoTDBDescriptor.getInstance().getConfig().setSchemaDir(sourceDir);
-    MManager.getInstance().init();
-    MManager.getInstance().exportSchema(new File(targetDir));
-  }
+    int port =
+        commandLine.getOptionValue(PORT_ARGS) == null
+            ? 6667
+            : Integer.parseInt(commandLine.getOptionValue(PORT_ARGS));
+    String user = commandLine.getOptionValue(USER_ARGS);
+    if (user == null) {
+      user = "root";
+    }
+    String password = commandLine.getOptionValue(PASSWORD_ARGS);
+    if (password == null) {
+      password = "root";
+    }
+    String targetDir = commandLine.getOptionValue(TARGET_DIR_ARGS);
 
-  public void clear() {
-    MManager.getInstance().clear();
+    Session session =
+        new Session.Builder()
+            .host(host)
+            .port(port)
+            .username(user)
+            .password(password)
+            .version(Version.V_0_13)
+            .build();
+    try {
+      session.open(false);
+      session.executeNonQueryStatement(String.format("EXPORT SCHEMA '%s'", targetDir));
+    } catch (IoTDBConnectionException | StatementExecutionException e) {
+      logger.error(e.getMessage(), e);
+    } finally {
+      session.close();
+    }
   }
 }