【发布时间】:2019-10-23 12:09:54
【问题描述】:
我正在使用 Apache Flink 来预测来自 Twitter 的流。
代码在 Scala 中实现
我的问题是,我从 DataSet API 训练的 SVM-Model 需要一个 DataSet 作为 predict()-Method 的输入。
我已经在这里看到了一个问题,用户说,你需要编写一个自己的 MapFunction,它在工作开始时读取模型(参考:Real-Time streaming prediction in Flink using scala)
但我无法编写/理解此代码。
即使我在 StreamingMapFunction 中获取模型。我仍然需要一个 DataSet 作为参数来预测结果。
我真的希望有人可以向我展示/解释这是如何完成的。
Flink 版本:1.9 Scala 版本:2.11 Flink-ML:2.11
val strEnv = StreamExecutionEnvironment.getExecutionEnvironment
val env = ExecutionEnvironment.getExecutionEnvironment
//this is my Model including all the terms to calculate the tfidf-values and to create a libsvm
val featureVectorService = new FeatureVectorService
featureVectorService.learnTrainingData(labeledData, false)
//reads the created libsvm
val trainingData: DataSet[LabeledVector] = MLUtils.readLibSVM(env, "...")
val svm = SVM()
.setBlocks(env.getParallelism)
.setIterations(100)
.setRegularization(0.001)
.setStepsize(0.1)
.setSeed(42)
//learning
svm.fit(trainingData)
//this is my twitter stream - text should be predicted later
val streamSource: DataStream[String] = strEnv.addSource(new TwitterSource(params.getProperties))
//the texts i want to transform to tfidf using the service upon and give it the svm to predict
val tweets: DataStream[(String, String)] = streamSource
.flatMap(new SelectEnglishTweetWithCreatedAtFlatMapper)
【问题讨论】:
-
你想用 java 或 scala 做吗?
-
@AntonioMiranda 嘿,感谢您的回答。我在斯卡拉做。更新了上面的代码以便更好地理解。希望你能帮忙
标签: dataset apache-flink data-stream flinkml