Skip to content

Commit 7c05de3

Browse files
committed
Feat: EXPORT MODEL ~ TO FILE
1 parent 9f7a6a6 commit 7c05de3

File tree

5 files changed

+27
-7
lines changed

5 files changed

+27
-7
lines changed

traindb-core/src/main/antlr4/traindb/sql/TrainDBSql.g4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ optionValue
146146
;
147147

148148
exportModel
149-
: K_EXPORT K_MODEL modelName
149+
: K_EXPORT K_MODEL modelName exportToClause?
150150
;
151151

152152
importModel

traindb-core/src/main/java/traindb/engine/TrainDBQueryEngine.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,7 +1102,8 @@ public void deleteTasks(Integer cnt) throws Exception {
11021102
}
11031103

11041104
@Override
1105-
public TrainDBListResultSet exportModel(String modelName) throws Exception {
1105+
public TrainDBListResultSet exportModel(String modelName, String exportFilename)
1106+
throws Exception {
11061107
T_tracer.startTaskTracer("export model " + modelName);
11071108

11081109
T_tracer.openTaskTime("find : model");
@@ -1131,13 +1132,22 @@ public TrainDBListResultSet exportModel(String modelName) throws Exception {
11311132
catalogContext.updateTrainingStatus(modelName, "FINISHED");
11321133
}
11331134

1134-
Path outputPath = Paths.get(runner.getModelPath().getParent().toString(), modelName + ".zip");
1135+
Path outputPath;
1136+
if (exportFilename != null) {
1137+
outputPath = Paths.get(exportFilename).toAbsolutePath();
1138+
} else {
1139+
outputPath = Paths.get(runner.getModelPath().getParent().toString(), modelName + ".zip");
1140+
}
11351141
runner.exportModel(outputPath.toString());
11361142

11371143
ObjectMapper mapper = new ObjectMapper();
11381144
ZipUtils.addNewFileFromStringToZip("export_metadata.json",
11391145
mapper.writeValueAsString(mModel), outputPath);
11401146

1147+
if (exportFilename != null) {
1148+
return TrainDBListResultSet.empty();
1149+
}
1150+
11411151
List<String> header = Arrays.asList("export_model");
11421152
List<List<Object>> exportModelInfo = new ArrayList<>();
11431153
ByteArray byteArray = convertFileToByteArray(new File(outputPath.toString()));

traindb-core/src/main/java/traindb/sql/TrainDBSql.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public static TrainDBListResultSet run(TrainDBSqlCommand command, TrainDBSqlRunn
144144
break;
145145
case EXPORT_MODEL:
146146
TrainDBSqlExportModel exportModel = (TrainDBSqlExportModel) command;
147-
return runner.exportModel(exportModel.getModelName());
147+
return runner.exportModel(exportModel.getModelName(), exportModel.getExportFilename());
148148
case IMPORT_MODEL:
149149
TrainDBSqlImportModel importModel = (TrainDBSqlImportModel) command;
150150
return runner.importModel(importModel.getModelName(), importModel.getModelBinaryString());
@@ -375,8 +375,12 @@ public void exitDescribeTable(TrainDBSqlParser.DescribeTableContext ctx) {
375375
@Override
376376
public void exitExportModel(TrainDBSqlParser.ExportModelContext ctx) {
377377
String modelName = ctx.modelName().getText();
378+
String exportFilename = null;
379+
if (ctx.exportToClause() != null) {
380+
exportFilename = ctx.exportToClause().filenameString().getText();
381+
}
378382
LOG.debug("EXPORT MODEL: name=" + modelName);
379-
commands.add(new TrainDBSqlExportModel(modelName));
383+
commands.add(new TrainDBSqlExportModel(modelName, exportFilename));
380384
}
381385

382386
@Override

traindb-core/src/main/java/traindb/sql/TrainDBSqlExportModel.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,21 @@
1616

1717
class TrainDBSqlExportModel extends TrainDBSqlCommand {
1818
private final String modelName;
19+
private final String exportFilename;
1920

20-
TrainDBSqlExportModel(String modelName) {
21+
TrainDBSqlExportModel(String modelName, String exportFilename) {
2122
this.modelName = modelName;
23+
this.exportFilename = exportFilename;
2224
}
2325

2426
String getModelName() {
2527
return modelName;
2628
}
2729

30+
String getExportFilename() {
31+
return exportFilename;
32+
}
33+
2834
@Override
2935
public Type getType() {
3036
return Type.EXPORT_MODEL;

traindb-core/src/main/java/traindb/sql/TrainDBSqlRunner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ void createSynopsis(String synopsisName, TrainDBSqlCommand.SynopsisType synopsis
7070

7171
void deleteTasks(Integer cnt) throws Exception;
7272

73-
TrainDBListResultSet exportModel(String modelName) throws Exception;
73+
TrainDBListResultSet exportModel(String modelName, String exportFilename) throws Exception;
7474

7575
TrainDBListResultSet importModel(String modelName, String modelBinaryString) throws Exception;
7676

0 commit comments

Comments
 (0)