zhipeng93 commented on a change in pull request #28: URL: https://github.com/apache/flink-ml/pull/28#discussion_r762403551
########## File path: flink-ml-lib/src/main/java/org/apache/flink/ml/classification/linear/LogisticRegressionModelData.java ########## @@ -0,0 +1,122 @@ +/* + * 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.linear; + +import org.apache.flink.api.common.serialization.Encoder; +import org.apache.flink.api.common.typeinfo.TypeInformation; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.connector.file.src.reader.SimpleStreamFormat; +import org.apache.flink.core.fs.FSDataInputStream; +import org.apache.flink.ml.linalg.DenseVector; +import org.apache.flink.streaming.api.datastream.DataStream; +import org.apache.flink.table.api.Table; +import org.apache.flink.table.api.bridge.java.StreamTableEnvironment; +import org.apache.flink.table.api.internal.TableImpl; + +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.Serializable; + +/** Model data of {@link LogisticRegressionModel}. */ +public class LogisticRegressionModelData implements Serializable { + + public final DenseVector coefficient; + + public LogisticRegressionModelData(DenseVector coefficient) { + this.coefficient = coefficient; + } + + /** + * Converts the table model to a data stream. + * + * @param modelData The table model data. + * @return The data stream model data. + */ + public static DataStream<LogisticRegressionModelData> getModelDataStream(Table modelData) { + StreamTableEnvironment tEnv = + (StreamTableEnvironment) ((TableImpl) modelData).getTableEnvironment(); + return tEnv.toDataStream(modelData).map(x -> (LogisticRegressionModelData) x.getField(0)); + } + + /** + * Gets the data encoder for {@link LogisticRegressionModelData}. + * + * @return The data encoder for {@link LogisticRegressionModelData}. + */ + public static ModelDataEncoder getModelDataEncoder() { + return new ModelDataEncoder(); + } + + /** + * Gets the data decoder for {@link LogisticRegressionModelData}. + * + * @return The data decoder for {@link LogisticRegressionModelData}. + */ + public static ModelDataDecoder getModelDataDecoder() { + return new ModelDataDecoder(); + } + + /** Data encoder for {@link LogisticRegressionModel}. */ + private static class ModelDataEncoder implements Encoder<LogisticRegressionModelData> { + + @Override + public void encode(LogisticRegressionModelData modelData, OutputStream stream) { Review comment: I tried to re-used `DenseVectorSerializer ` but encountered the following exception: ``` Caused by: java.io.EOFException at java.io.DataInputStream.readInt(DataInputStream.java:392) at org.apache.flink.ml.linalg.typeinfo.DenseVectorSerializer.deserialize(DenseVectorSerializer.java:85) at org.apache.flink.ml.classification.linear.LogisticRegressionModelData$ModelDataDecoder$1.read(LogisticRegressionModelData.java:100) at org.apache.flink.ml.classification.linear.LogisticRegressionModelData$ModelDataDecoder$1.read(LogisticRegressionModelData.java:83) ``` I guess it is because I did not check whether the input stream has reached the end. However, I did not find an API that allows me to check the end of of `InputStream` without reading one byte. Do you have a solution here? The code snippet is as follows: ``` new Reader<LogisticRegressionModelData>() { private final DenseVectorSerializer serializer = new DenseVectorSerializer(); @Override public LogisticRegressionModelData read() throws IOException{ DenseVector coefficient = serializer.deserialize(new DataInputViewStreamWrapper(stream)); return new LogisticRegressionModelData(coefficient); } }; ``` -- 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: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org