简体   繁体   English

Spark Scala-将具有一条记录和一列的Dataframe转换为Double

[英]Spark Scala - converting Dataframe with one record and one column into Double

The scala code that i wrote gives me data type errors. 我写的scala代码给了我数据类型错误。 The main method which is testpredict_02 takes Double. testpredict_02的主要方法采用Double。

val featuresMD = hiveContext.read.parquet("hdfs://machine01:9000/models/nb/metadata/features")

def testpredict_02(VData: Vector) = { MyModel.predict(VData) }

def outerpredict_02(argincome: String,argage: String,arggender: String) = { 
featuresMD.registerTempTable("features_md")

val income = hiveContext.sql("select distinct income_index from features_md where income = argincome")
val age     = hiveContext.sql("select distinct age_index from features_md where age = argage") 
val gender  = hiveContext.sql("select distinct gender_index from features_md where gender = arggender") 

testpredict_02(Vectors.dense(income.select("income_index"), age.select("age_index"), gender.select("gender_index")))

Error :
<console>:43: error: type mismatch;
 found   : org.apache.spark.sql.DataFrame
 required: Double
              testpredict_02(Vectors.dense(income.select("income_index"), age.select("age_index")))

Please help.. 请帮忙..

If you're sure each of the 3 Dataframes contains exactly one column and one record, you can get the first column of the first record for each of them: 如果确定3个数据框中的每一个都只包含一个列和一个记录,则可以为每个数据框获取第一条记录的第一列:

def getFirstCell(df: DataFrame): Double = df.first().getAs[Double](0)

val vector: Vector = Vectors.dense(
  getFirstCell(income.select("income_index")),
  getFirstCell(age.select("age_index")),
  getFirstCell(gender.select("gender_index"))
)

testpredict_02(vector)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spark Scala将列从一个数据帧复制到另一个数据帧 - Spark Scala copy column from one dataframe to another 如何将spark / scala中数据框的一列值相加 - How to sum the values of one column of a dataframe in spark/scala 从火花的多列创建一个 dataframe - Scala eqv 的 Python - Create one from multiple column of a spark dataframe - Scala eqv of Python Spark SQL 将 Scala DataFrame 转换为列列表 - Spark SQL converting a scala DataFrame into a column list 将Spark Scala数据框列转换为字节数组 - Converting Spark Scala Dataframe Column to Byte Array 使用Spark Dataframe Scala将Array [Double]列转换为字符串或两个不同的列 - Converting an Array[Double] Column into a string or two different columns with Spark Dataframe Scala 比较一个 Dataframe 中多列中的值与目标 dataframe 中同一记录的单列中的多行 Scala? - Compare the values in multiple columns in one Dataframe with multiple rows in one single column for target dataframe for the same record in Scala? 在Spark DataFrame中添加一个新列,其中包含一个列的所有值的总和-Scala / Spark - Add a new Column in Spark DataFrame which contains the sum of all values of one column-Scala/Spark 在Scala Spark中从向量列转换为Double [Array]列 - Converting from vector column to Double[Array] column in scala Spark 使用Spark Scala检查一个数据框列中的值是否在另一数据框列中存在 - Check if value from one dataframe column exists in another dataframe column using Spark Scala
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM