zhipeng93 commented on a change in pull request #60:
URL: https://github.com/apache/flink-ml/pull/60#discussion_r810726491



##########
File path: 
flink-ml-core/src/main/java/org/apache/flink/ml/util/ReadWriteUtils.java
##########
@@ -401,6 +383,19 @@ public static void updateExistingParams(Stage<?> stage, 
Map<Param<?>, Object> pa
         }
     }
 
+    /** Returns a subdirectory of the given path for saving/loading model 
data. */
+    private static String getDataPath(String path) {
+        return Paths.get(path, "data").toString();
+    }
+
+    /** Assigns model version for every model data when sinking to files. */
+    public static class ModelVersionAssigner<T> extends 
BasePathBucketAssigner<T> {

Review comment:
       nits: public --> private

##########
File path: 
flink-ml-core/src/main/java/org/apache/flink/ml/util/ReadWriteUtils.java
##########
@@ -401,6 +383,19 @@ public static void updateExistingParams(Stage<?> stage, 
Map<Param<?>, Object> pa
         }
     }
 
+    /** Returns a subdirectory of the given path for saving/loading model 
data. */
+    private static String getDataPath(String path) {
+        return Paths.get(path, "data").toString();
+    }
+
+    /** Assigns model version for every model data when sinking to files. */
+    public static class ModelVersionAssigner<T> extends 
BasePathBucketAssigner<T> {
+        @Override
+        public String getBucketId(T element, Context context) {
+            return String.valueOf(System.nanoTime());

Review comment:
       Can we make `modelVersionAssiginer` independent of execution time? 
   If model data contains multiple streams and we use the current version 
assigner(with timestamp as the version), we may not be able to associate the 
model data from different streams. 

##########
File path: 
flink-ml-lib/src/test/java/org/apache/flink/ml/classification/OnlineModelSaveLoadTest.java
##########
@@ -0,0 +1,128 @@
+/*
+ * 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.flink.ml.classification;
+
+import org.apache.flink.api.common.restartstrategy.RestartStrategies;
+import org.apache.flink.configuration.Configuration;
+import 
org.apache.flink.ml.classification.logisticregression.LogisticRegressionModel;
+import 
org.apache.flink.ml.classification.logisticregression.LogisticRegressionModelData;
+import org.apache.flink.ml.linalg.DenseVector;
+import org.apache.flink.ml.linalg.Vectors;
+import 
org.apache.flink.streaming.api.environment.ExecutionCheckpointingOptions;
+import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.Schema;
+import org.apache.flink.table.api.Table;
+import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
+import org.apache.flink.types.Row;
+
+import org.apache.commons.collections.IteratorUtils;
+import org.junit.Assert;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.apache.flink.ml.util.ReadWriteUtils.loadModelData;
+
+/** Tests online LogisticRegression model save and load. */
+public class OnlineModelSaveLoadTest {
+    @Rule public final TemporaryFolder tempFolder = new TemporaryFolder();
+
+    StreamTableEnvironment tEnv;
+    Schema modelSchema = Schema.newBuilder().column("f0", 
DataTypes.of(DenseVector.class)).build();
+    Schema dataSchema =
+            Schema.newBuilder()
+                    .column("f0", DataTypes.of(Double.class))
+                    .column("f1", DataTypes.of(DenseVector.class))
+                    .build();
+
+    private static final List<Row> modelData =
+            new ArrayList<>(
+                    Arrays.asList(
+                            Row.of(Vectors.dense(2.0, 4.5, 3.0)),
+                            Row.of(Vectors.dense(2.1, 4.6, 3.1)),
+                            Row.of(Vectors.dense(20.1, 5.6, 3.1)),
+                            Row.of(Vectors.dense(2.1, 4.7, 3.1))));
+
+    private static final List<Row> validData =
+            new ArrayList<>(
+                    Arrays.asList(
+                            Row.of(1.0, Vectors.dense(1.0, 3.5, -4.0)),
+                            Row.of(0.0, Vectors.dense(1.1, -8.6, 3.3))));
+
+    @Test
+    public void saveAndLoadOnlineModel() throws Exception {
+        Configuration config = new Configuration();
+        
config.set(ExecutionCheckpointingOptions.ENABLE_CHECKPOINTS_AFTER_TASKS_FINISH, 
true);
+        StreamExecutionEnvironment env = 
StreamExecutionEnvironment.getExecutionEnvironment(config);
+        env.enableCheckpointing(100);
+        env.setRestartStrategy(RestartStrategies.noRestart());
+        tEnv = StreamTableEnvironment.create(env);
+        env.setParallelism(1);
+
+        String tmpPath = tempFolder.newFolder().getAbsolutePath();
+
+        /* Constructs online LogisticRegression model. */
+        Table lrModelStream =
+                tEnv.fromDataStream(env.fromCollection(modelData), 
modelSchema).as("coefficient");
+        LogisticRegressionModel lr = new LogisticRegressionModel();
+        /* Saves online model to given path (tmpPath). */
+        lr.setModelData(lrModelStream);
+        lr.save(tmpPath);
+        env.execute();
+
+        /* Constructs validated data table. */
+        Table validDataTable =
+                tEnv.fromDataStream(env.fromCollection(validData), 
dataSchema).as("label, vec");
+
+        Process proc = Runtime.getRuntime().exec("ls " + tmpPath + "/data");
+        BufferedReader bufferedReader =
+                new BufferedReader(new 
InputStreamReader(proc.getInputStream()));
+
+        /* Loads every LogisticRegression model in model path and validates 
it. */
+        String modelVersion;
+        while ((modelVersion = bufferedReader.readLine()) != null) {
+            if (!"metadata".equals(modelVersion)) {
+                LogisticRegressionModel lrModel =

Review comment:
       nits: `LogisticRegressionModel lrModel =..` could be outside of the loop.

##########
File path: 
flink-ml-core/src/main/java/org/apache/flink/ml/util/ReadWriteUtils.java
##########
@@ -431,8 +426,37 @@ public static void updateExistingParams(Stage<?> stage, 
Map<Param<?>, Object> pa
      */
     public static <T> DataStream<T> loadModelData(
             StreamExecutionEnvironment env, String path, SimpleStreamFormat<T> 
modelDecoder) {
+        String[] fileNames = new File(path).list();
+        org.apache.flink.core.fs.Path modelPath = null;
+        for (String fileName : fileNames) {
+            if (new File(path + "/" + fileName).isDirectory()) {
+                modelPath = new org.apache.flink.core.fs.Path(path + "/" + 
fileName);
+            }
+        }
+        Source<T, ?, ?> source = 
FileSource.forRecordStreamFormat(modelDecoder, modelPath).build();

Review comment:
       Should we still use ".../data/" as the default model data path?
   
   If there is a directory that is not ".../data/", can the test case still 
work?

##########
File path: 
flink-ml-core/src/main/java/org/apache/flink/ml/util/ReadWriteUtils.java
##########
@@ -431,8 +426,37 @@ public static void updateExistingParams(Stage<?> stage, 
Map<Param<?>, Object> pa
      */
     public static <T> DataStream<T> loadModelData(
             StreamExecutionEnvironment env, String path, SimpleStreamFormat<T> 
modelDecoder) {
+        String[] fileNames = new File(path).list();
+        org.apache.flink.core.fs.Path modelPath = null;
+        for (String fileName : fileNames) {
+            if (new File(path + "/" + fileName).isDirectory()) {
+                modelPath = new org.apache.flink.core.fs.Path(path + "/" + 
fileName);
+            }
+        }
+        Source<T, ?, ?> source = 
FileSource.forRecordStreamFormat(modelDecoder, modelPath).build();
+        return env.fromSource(source, WatermarkStrategy.noWatermarks(), 
"modelData");
+    }
+
+    /**
+     * Loads the model data from the given path which has more than one model.

Review comment:
       Can you update the java doc and explain why do we need this function 
here?

##########
File path: 
flink-ml-lib/src/test/java/org/apache/flink/ml/classification/OnlineModelSaveLoadTest.java
##########
@@ -0,0 +1,128 @@
+/*
+ * 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.flink.ml.classification;
+
+import org.apache.flink.api.common.restartstrategy.RestartStrategies;
+import org.apache.flink.configuration.Configuration;
+import 
org.apache.flink.ml.classification.logisticregression.LogisticRegressionModel;
+import 
org.apache.flink.ml.classification.logisticregression.LogisticRegressionModelData;
+import org.apache.flink.ml.linalg.DenseVector;
+import org.apache.flink.ml.linalg.Vectors;
+import 
org.apache.flink.streaming.api.environment.ExecutionCheckpointingOptions;
+import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.Schema;
+import org.apache.flink.table.api.Table;
+import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
+import org.apache.flink.types.Row;
+
+import org.apache.commons.collections.IteratorUtils;
+import org.junit.Assert;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.apache.flink.ml.util.ReadWriteUtils.loadModelData;
+
+/** Tests online LogisticRegression model save and load. */
+public class OnlineModelSaveLoadTest {
+    @Rule public final TemporaryFolder tempFolder = new TemporaryFolder();
+
+    StreamTableEnvironment tEnv;
+    Schema modelSchema = Schema.newBuilder().column("f0", 
DataTypes.of(DenseVector.class)).build();
+    Schema dataSchema =
+            Schema.newBuilder()
+                    .column("f0", DataTypes.of(Double.class))
+                    .column("f1", DataTypes.of(DenseVector.class))
+                    .build();
+
+    private static final List<Row> modelData =
+            new ArrayList<>(
+                    Arrays.asList(
+                            Row.of(Vectors.dense(2.0, 4.5, 3.0)),
+                            Row.of(Vectors.dense(2.1, 4.6, 3.1)),
+                            Row.of(Vectors.dense(20.1, 5.6, 3.1)),
+                            Row.of(Vectors.dense(2.1, 4.7, 3.1))));
+
+    private static final List<Row> validData =
+            new ArrayList<>(
+                    Arrays.asList(
+                            Row.of(1.0, Vectors.dense(1.0, 3.5, -4.0)),
+                            Row.of(0.0, Vectors.dense(1.1, -8.6, 3.3))));
+
+    @Test
+    public void saveAndLoadOnlineModel() throws Exception {
+        Configuration config = new Configuration();
+        
config.set(ExecutionCheckpointingOptions.ENABLE_CHECKPOINTS_AFTER_TASKS_FINISH, 
true);
+        StreamExecutionEnvironment env = 
StreamExecutionEnvironment.getExecutionEnvironment(config);
+        env.enableCheckpointing(100);
+        env.setRestartStrategy(RestartStrategies.noRestart());
+        tEnv = StreamTableEnvironment.create(env);
+        env.setParallelism(1);
+
+        String tmpPath = tempFolder.newFolder().getAbsolutePath();
+
+        /* Constructs online LogisticRegression model. */
+        Table lrModelStream =
+                tEnv.fromDataStream(env.fromCollection(modelData), 
modelSchema).as("coefficient");
+        LogisticRegressionModel lr = new LogisticRegressionModel();
+        /* Saves online model to given path (tmpPath). */
+        lr.setModelData(lrModelStream);
+        lr.save(tmpPath);
+        env.execute();
+
+        /* Constructs validated data table. */
+        Table validDataTable =
+                tEnv.fromDataStream(env.fromCollection(validData), 
dataSchema).as("label, vec");
+
+        Process proc = Runtime.getRuntime().exec("ls " + tmpPath + "/data");
+        BufferedReader bufferedReader =
+                new BufferedReader(new 
InputStreamReader(proc.getInputStream()));
+
+        /* Loads every LogisticRegression model in model path and validates 
it. */
+        String modelVersion;
+        while ((modelVersion = bufferedReader.readLine()) != null) {

Review comment:
       Can we also hava a test case that loads all of the model data in a 
single data stream?

##########
File path: 
flink-ml-lib/src/test/java/org/apache/flink/ml/classification/OnlineModelSaveLoadTest.java
##########
@@ -0,0 +1,128 @@
+/*
+ * 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.flink.ml.classification;
+
+import org.apache.flink.api.common.restartstrategy.RestartStrategies;
+import org.apache.flink.configuration.Configuration;
+import 
org.apache.flink.ml.classification.logisticregression.LogisticRegressionModel;
+import 
org.apache.flink.ml.classification.logisticregression.LogisticRegressionModelData;
+import org.apache.flink.ml.linalg.DenseVector;
+import org.apache.flink.ml.linalg.Vectors;
+import 
org.apache.flink.streaming.api.environment.ExecutionCheckpointingOptions;
+import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.Schema;
+import org.apache.flink.table.api.Table;
+import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
+import org.apache.flink.types.Row;
+
+import org.apache.commons.collections.IteratorUtils;
+import org.junit.Assert;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.apache.flink.ml.util.ReadWriteUtils.loadModelData;
+
+/** Tests online LogisticRegression model save and load. */
+public class OnlineModelSaveLoadTest {
+    @Rule public final TemporaryFolder tempFolder = new TemporaryFolder();
+
+    StreamTableEnvironment tEnv;
+    Schema modelSchema = Schema.newBuilder().column("f0", 
DataTypes.of(DenseVector.class)).build();
+    Schema dataSchema =
+            Schema.newBuilder()
+                    .column("f0", DataTypes.of(Double.class))
+                    .column("f1", DataTypes.of(DenseVector.class))
+                    .build();
+
+    private static final List<Row> modelData =
+            new ArrayList<>(
+                    Arrays.asList(
+                            Row.of(Vectors.dense(2.0, 4.5, 3.0)),
+                            Row.of(Vectors.dense(2.1, 4.6, 3.1)),
+                            Row.of(Vectors.dense(20.1, 5.6, 3.1)),
+                            Row.of(Vectors.dense(2.1, 4.7, 3.1))));
+
+    private static final List<Row> validData =
+            new ArrayList<>(
+                    Arrays.asList(
+                            Row.of(1.0, Vectors.dense(1.0, 3.5, -4.0)),
+                            Row.of(0.0, Vectors.dense(1.1, -8.6, 3.3))));
+
+    @Test
+    public void saveAndLoadOnlineModel() throws Exception {
+        Configuration config = new Configuration();

Review comment:
       It would be better to extract the common logic here in `Before` 
function, same as we did in other test cases.

##########
File path: 
flink-ml-lib/src/test/java/org/apache/flink/ml/classification/OnlineModelSaveLoadTest.java
##########
@@ -0,0 +1,128 @@
+/*
+ * 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.flink.ml.classification;
+
+import org.apache.flink.api.common.restartstrategy.RestartStrategies;
+import org.apache.flink.configuration.Configuration;
+import 
org.apache.flink.ml.classification.logisticregression.LogisticRegressionModel;
+import 
org.apache.flink.ml.classification.logisticregression.LogisticRegressionModelData;
+import org.apache.flink.ml.linalg.DenseVector;
+import org.apache.flink.ml.linalg.Vectors;
+import 
org.apache.flink.streaming.api.environment.ExecutionCheckpointingOptions;
+import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
+import org.apache.flink.table.api.DataTypes;
+import org.apache.flink.table.api.Schema;
+import org.apache.flink.table.api.Table;
+import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
+import org.apache.flink.types.Row;
+
+import org.apache.commons.collections.IteratorUtils;
+import org.junit.Assert;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.apache.flink.ml.util.ReadWriteUtils.loadModelData;
+
+/** Tests online LogisticRegression model save and load. */
+public class OnlineModelSaveLoadTest {
+    @Rule public final TemporaryFolder tempFolder = new TemporaryFolder();
+
+    StreamTableEnvironment tEnv;
+    Schema modelSchema = Schema.newBuilder().column("f0", 
DataTypes.of(DenseVector.class)).build();
+    Schema dataSchema =
+            Schema.newBuilder()
+                    .column("f0", DataTypes.of(Double.class))
+                    .column("f1", DataTypes.of(DenseVector.class))
+                    .build();
+
+    private static final List<Row> modelData =
+            new ArrayList<>(
+                    Arrays.asList(
+                            Row.of(Vectors.dense(2.0, 4.5, 3.0)),
+                            Row.of(Vectors.dense(2.1, 4.6, 3.1)),
+                            Row.of(Vectors.dense(20.1, 5.6, 3.1)),
+                            Row.of(Vectors.dense(2.1, 4.7, 3.1))));
+
+    private static final List<Row> validData =
+            new ArrayList<>(
+                    Arrays.asList(
+                            Row.of(1.0, Vectors.dense(1.0, 3.5, -4.0)),
+                            Row.of(0.0, Vectors.dense(1.1, -8.6, 3.3))));
+
+    @Test
+    public void saveAndLoadOnlineModel() throws Exception {
+        Configuration config = new Configuration();
+        
config.set(ExecutionCheckpointingOptions.ENABLE_CHECKPOINTS_AFTER_TASKS_FINISH, 
true);
+        StreamExecutionEnvironment env = 
StreamExecutionEnvironment.getExecutionEnvironment(config);
+        env.enableCheckpointing(100);
+        env.setRestartStrategy(RestartStrategies.noRestart());
+        tEnv = StreamTableEnvironment.create(env);
+        env.setParallelism(1);
+
+        String tmpPath = tempFolder.newFolder().getAbsolutePath();
+
+        /* Constructs online LogisticRegression model. */
+        Table lrModelStream =
+                tEnv.fromDataStream(env.fromCollection(modelData), 
modelSchema).as("coefficient");
+        LogisticRegressionModel lr = new LogisticRegressionModel();
+        /* Saves online model to given path (tmpPath). */
+        lr.setModelData(lrModelStream);
+        lr.save(tmpPath);
+        env.execute();
+
+        /* Constructs validated data table. */
+        Table validDataTable =
+                tEnv.fromDataStream(env.fromCollection(validData), 
dataSchema).as("label, vec");
+
+        Process proc = Runtime.getRuntime().exec("ls " + tmpPath + "/data");
+        BufferedReader bufferedReader =
+                new BufferedReader(new 
InputStreamReader(proc.getInputStream()));
+
+        /* Loads every LogisticRegression model in model path and validates 
it. */
+        String modelVersion;
+        while ((modelVersion = bufferedReader.readLine()) != null) {
+            if (!"metadata".equals(modelVersion)) {
+                LogisticRegressionModel lrModel =
+                        new LogisticRegressionModel().setFeaturesCol("vec");
+                Table modelData =
+                        tEnv.fromDataStream(
+                                        loadModelData(
+                                                env,
+                                                tmpPath,
+                                                new 
LogisticRegressionModelData.ModelDataDecoder(),
+                                                modelVersion))
+                                .as("label, vec");

Review comment:
       Why do you convert model data as `label, vec`?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to