Scala Spark Streaming 2.0.1-数据集和RDD
我是spark streaming的初学者,并尝试使用scala使用streaming线性回归示例。所以当我搜索时,我发现了很多使用RDD的流式机器学习算法的例子。但是,是否可以使用数据集(spark 2.0.1中引入)来代替RDD进行流式处理呢。有没有办法验证代码是否使用RDD或数据集?我已经在下面发布了我的代码。谢谢你的帮助Scala Spark Streaming 2.0.1-数据集和RDD,scala,apache-spark,machine-learning,spark-streaming,linear-regression,Scala,Apache Spark,Machine Learning,Spark Streaming,Linear Regression,我是spark streaming的初学者,并尝试使用scala使用streaming线性回归示例。所以当我搜索时,我发现了很多使用RDD的流式机器学习算法的例子。但是,是否可以使用数据集(spark 2.0.1中引入)来代替RDD进行流式处理呢。有没有办法验证代码是否使用RDD或数据集?我已经在下面发布了我的代码。谢谢你的帮助 import scala.language.reflectiveCalls import scopt.OptionParser import org.apache.sp
import scala.language.reflectiveCalls
import scopt.OptionParser
import org.apache.spark.ml.regression.LinearRegression
import org.apache.spark.sql.{ DataFrame, SparkSession }
import com.sun.xml.internal.ws.wsdl.writer.document.Import
import org.apache.spark.SparkConf
import org.apache.spark.storage.StorageLevel
import org.apache.spark.streaming.{Seconds, StreamingContext}
import java.lang.Boolean
object LinearRegressionExample {
case class Params(
input: String = null,
testInput: String = "",
dataFormat: String = "libsvm",
regParam: Double = 0.0,
elasticNetParam: Double = 0.0,
maxIter: Int = 100,
tol: Double = 1E-6,
fracTest: Double = 0.2) extends AbstractParams[Params]
def main(args: Array[String]) {
val defaultParams = Params()
val parser = new OptionParser[Params]("LinearRegressionExample") {
head("LinearRegressionExample: an example Linear Regression with Elastic-Net app.")
opt[Double]("regParam")
.text(s"regularization parameter, default: ${defaultParams.regParam}")
.action((x, c) => c.copy(regParam = x))
opt[Double]("elasticNetParam")
.text(s"ElasticNet mixing parameter. For alpha = 0, the penalty is an L2 penalty. " +
s"For alpha = 1, it is an L1 penalty. For 0 < alpha < 1, the penalty is a combination of " +
s"L1 and L2, default: ${defaultParams.elasticNetParam}")
.action((x, c) => c.copy(elasticNetParam = x))
opt[Int]("maxIter")
.text(s"maximum number of iterations, default: ${defaultParams.maxIter}")
.action((x, c) => c.copy(maxIter = x))
opt[Double]("tol")
.text(s"the convergence tolerance of iterations, Smaller value will lead " +
s"to higher accuracy with the cost of more iterations, default: ${defaultParams.tol}")
.action((x, c) => c.copy(tol = x))
opt[Double]("fracTest")
.text(s"fraction of data to hold out for testing. If given option testInput, " +
s"this option is ignored. default: ${defaultParams.fracTest}")
.action((x, c) => c.copy(fracTest = x))
opt[String]("testInput")
.text(s"input path to test dataset. If given, option fracTest is ignored." +
s" default: ${defaultParams.testInput}")
.action((x, c) => c.copy(testInput = x))
opt[String]("dataFormat")
.text("data format: libsvm (default), dense (deprecated in Spark v1.1)")
.action((x, c) => c.copy(dataFormat = x))
arg[String]("<input>")
.text("input path to labeled examples")
.required()
.action((x, c) => c.copy(input = x))
}
parser.parse(args, defaultParams) match {
case Some(params) => run(params)
case _ => sys.exit(1)
}
}
def run(params: Params): Unit = {
val conf = new SparkConf().setMaster("local[2]").setAppName("LinearRegressionExample with $params")
val ssc = new StreamingContext(conf, Seconds(1))
val spark = SparkSession
.builder
.appName(s"LinearRegressionExample with $params")
.getOrCreate()
println(s"LinearRegressionExample with parameters:\n$params")
// Load training and test data and cache it.
val (training: DataFrame, test: DataFrame) = DecisionTreeExample.loadDatasets(params.input,
params.dataFormat, params.testInput, "regression", params.fracTest)
val lir = new LinearRegression()
.setFeaturesCol("features")
.setLabelCol("label")
.setRegParam(params.regParam)
.setElasticNetParam(params.elasticNetParam)
.setMaxIter(params.maxIter)
.setTol(params.tol)
// Train the model
val startTime = System.nanoTime()
val lirModel = lir.fit(training)
val elapsedTime = (System.nanoTime() - startTime) / 1e9
println(s"Training time: $elapsedTime seconds")
// Print the weights and intercept for linear regression.
println(s"Weights: ${lirModel.coefficients} Intercept: ${lirModel.intercept}")
println("Training data results:")
DecisionTreeExample.evaluateRegressionModel(lirModel, training, "label")
println("Test data results:")
DecisionTreeExample.evaluateRegressionModel(lirModel, test, "label")
spark.stop()
}
}
导入scala.language.reflectCalls
导入scopt.OptionParser
导入org.apache.spark.ml.regression.LinearRegression
导入org.apache.spark.sql.{DataFrame,SparkSession}
导入com.sun.xml.internal.ws.wsdl.writer.document.import
导入org.apache.spark.SparkConf
导入org.apache.spark.storage.StorageLevel
导入org.apache.spark.streaming.{Seconds,StreamingContext}
导入java.lang.Boolean
对象线性回归示例{
case类参数(
输入:String=null,
testInput:String=“”,
dataFormat:String=“libsvm”,
regParam:Double=0.0,
elasticNetParam:Double=0.0,
最大值:Int=100,
tol:Double=1E-6,
fracttest:Double=0.2)扩展抽象参数[Params]
def main(参数:数组[字符串]){
val defaultParams=Params()
val parser=new OptionParser[Params](“LinearRegressionExample”){
head(“线性回归示例:使用弹性网络应用程序的线性回归示例”)
opt[Double](“regParam”)
.text(s“正则化参数,默认值:${defaultParams.regParam}”)
.action((x,c)=>c.copy(regParam=x))
opt[Double](“elasticNetParam”)
.text(s“ElasticNet混合参数。对于alpha=0,惩罚为L2惩罚。”+
s“对于alpha=1,它是L1惩罚。对于0c.copy(elasticNetParam=x))
选择[Int](“maxIter”)
.text(s“最大迭代次数,默认值:${defaultParams.maxIter}”)
.action((x,c)=>c.copy(maxIter=x))
选择[双重](“tol”)
.text“迭代的收敛公差,较小的值将导致”+
s“以更高的精度和更多的迭代为代价,默认值:${defaultParams.tol}”)
.action((x,c)=>c.copy(tol=x))
选择[双重](“分形测试”)
.text“保留用于测试的部分数据。如果提供选项testInput,”+
s“忽略此选项。默认值:${defaultParams.fracTest}”)
.action((x,c)=>c.copy(fracTest=x))
opt[String](“testInput”)
.text“测试数据集的输入路径。如果给定,将忽略fracTest选项。”+
s“默认值:${defaultParams.testInput}”)
.action((x,c)=>c.copy(testInput=x))
opt[String](“数据格式”)
.text(“数据格式:libsvm(默认)、密集(Spark v1.1中已弃用)”)
.action((x,c)=>c.copy(dataFormat=x))
arg[字符串](“”)
.text(“标记示例的输入路径”)
.required()
.action((x,c)=>c.copy(输入=x))
}
parser.parse(args,defaultParams)匹配{
案例部分(参数)=>运行(参数)
案例=>sys.exit(1)
}
}
def运行(参数:参数):单位={
val conf=new SparkConf().setMaster(“local[2]”).setAppName(“LinearRegressionExample with$params”)
val ssc=新的StreamingContext(形态,秒(1))
val spark=火花会话
建设者
.appName(s“带$params的线性回归示例”)
.getOrCreate()
println(s“参数为:\n$params的线性回归示例”)
//加载培训和测试数据并将其缓存。
val(训练:数据帧,测试:数据帧)=决策树示例加载数据集(参数输入,
params.dataFormat,params.testInput,“回归”,params.FractTest)
val lir=新的线性回归()
.setFeaturesCol(“功能”)
.setLabelCol(“标签”)
.setRegParam(params.regParam)
.setElasticNetParam(参数elasticNetParam)
.setMaxIter(参数maxIter)
.setTol(参数tol)
//训练模型
val startTime=System.nanoTime()
val lirModel=lir.fit(培训)
val elapsedTime=(System.nanoTime()-startTime)/1e9
println(s“训练时间:$elapsedTime秒”)
//打印线性回归的权重和截距。
println(s“权重:${lirModel.coverties}截距:${lirModel.Intercept}”)
println(“培训数据结果:”)
决策树示例评估回归模型(lirModel,培训,“标签”)
println(“测试数据结果:”)
决策树示例评估回归模型(lirModel,测试,“标签”)
spark.stop()
}
}
您在程序中实际使用的ssc
?当您使用Spark Streaming Context时,它将返回数据流(它只是多个RDD的组合)。如果您想使用数据集,可以将RDD转换为数据集。@Shankar刚刚发现所有当前的流媒体算法都使用RDD。(spark 2.0.1)但我需要使用数据集。实现这一点的最佳方法是什么。我认为这可能是一个很好的例子。还有一种方法可以将“流式数据集”与一起使用,但它是alpha版本。@SimonSchiff很抱歉打断您。非常感谢您的帮助。是否可以将此流式数据集也用于机器学习算法的结构化流式中?流式数据集与spark sql中的流式数据集相同。但目前它们的功能有限。在幻灯片26的数据记录中,您可以看到这是/将是可能的。您在程序中实际使用了ssc
?当您使用Spark Streaming Context时,它将返回数据流(它只是多个RDD的组合)。如果您想使用数据集,可以将RDD转换为数据集。@Shankar刚刚发现所有当前的流媒体算法都使用RDD。(spark 2.0.1)但我需要使用数据集。实现这一点的最佳方法是什么。我认为这可能是一个很好的例子。还有一种方法可以将“流式数据集”与一起使用,但它是alpha版本。@SimonSchiff抱歉打断您