|
|
@@ -1,7 +1,6 @@
|
|
|
package com.example.data
|
|
|
|
|
|
import java.util
|
|
|
-
|
|
|
import com.example.SparkConnect
|
|
|
import com.example.dao._
|
|
|
import com.example.entity.{Config, Model}
|
|
|
@@ -10,6 +9,7 @@ import org.apache.spark.ml.classification.LogisticRegressionModel
|
|
|
import org.apache.spark.ml.clustering.KMeansModel
|
|
|
import org.apache.spark.ml.feature.{StringIndexer, StringIndexerModel}
|
|
|
import org.apache.spark.ml.{Pipeline, PipelineModel}
|
|
|
+import org.apache.spark.sql.functions.lit
|
|
|
import org.apache.spark.sql.types.StringType
|
|
|
import org.apache.spark.sql.{DataFrame, SaveMode}
|
|
|
import org.springframework.beans.factory.annotation.Autowired
|
|
|
@@ -24,7 +24,7 @@ import scala.collection.JavaConverters._
|
|
|
class FitTestData extends SparkConnect{
|
|
|
|
|
|
@Autowired
|
|
|
- val fileLocDao: FileInfoDao = null
|
|
|
+ val fileInfoDao: FileInfoDao = null
|
|
|
|
|
|
@Autowired
|
|
|
val hdfsDao: HdfsDao = null
|
|
|
@@ -41,10 +41,10 @@ class FitTestData extends SparkConnect{
|
|
|
@throws(classOf[Exception])
|
|
|
def transformData(fileId: Int, modelId: Int): util.List[util.Map[String, String]] = {
|
|
|
|
|
|
- val fileLocs = fileLocDao.findById(fileId)
|
|
|
- println(fileLocs)
|
|
|
- val filepath = fileLocs.getLocation
|
|
|
- val filename = fileLocs.getFilename
|
|
|
+ val fileInfo = fileInfoDao.findById(fileId)
|
|
|
+ log.info("fileId=" + fileId + ", read: "+fileInfo.toString)
|
|
|
+ val filepath = fileInfo.getLocation
|
|
|
+ val filename = fileInfo.getFilename
|
|
|
|
|
|
log.info("file loc: " + filepath)
|
|
|
|
|
|
@@ -61,7 +61,8 @@ class FitTestData extends SparkConnect{
|
|
|
//val keyFeature = fieldsId.remove(fieldsId.size() - 1)
|
|
|
val fieldNames: Array[String] = new Array[String](fieldsId.size()-1)
|
|
|
|
|
|
- val keyFeature = headerInfoDao.findById(Integer.parseInt(fieldsId.get(fieldsId.size()-1))).getFieldName
|
|
|
+ val labelHeaderInfoId = fieldsId.get(fieldsId.size()-1)
|
|
|
+ val keyFeature = headerInfoDao.findById(Integer.parseInt(labelHeaderInfoId)).getFieldName
|
|
|
var i: Int = 0
|
|
|
var flag = false
|
|
|
while(iter.hasNext && !flag){
|
|
|
@@ -75,12 +76,34 @@ class FitTestData extends SparkConnect{
|
|
|
val spark = sparkSession
|
|
|
|
|
|
import spark.implicits._
|
|
|
- val parquetFile = sparkSession.read.parquet(filepath)
|
|
|
+ val dataFrameFromParquet = sparkSession.read.parquet(filepath)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ //此处需要做一个适配,若文件此时没有label列,就先加上
|
|
|
+ var dataFrameForPredict:DataFrame = dataFrameFromParquet
|
|
|
+ if(!dataFrameFromParquet.columns.toList.contains(keyFeature)){
|
|
|
+
|
|
|
+ val labelHeaderInfo = headerInfoDao.findById(Integer.parseInt(labelHeaderInfoId))
|
|
|
+ val valueInfo = labelHeaderInfo.getValueInfo
|
|
|
+ val fieldType = labelHeaderInfo.getFieldType
|
|
|
+ val valueArray = json2Object.Json2List(valueInfo)
|
|
|
+ val labelDefaultValueStr = valueArray.get(0)
|
|
|
+
|
|
|
+ val labelDefaultValue = fieldType match {
|
|
|
+ case "string" => labelDefaultValueStr
|
|
|
+ case "int" => Integer.parseInt(labelDefaultValueStr)
|
|
|
+ case "double" => labelDefaultValueStr.toDouble
|
|
|
+ case _ => log.error("类型出错")
|
|
|
+ }
|
|
|
+
|
|
|
+ dataFrameForPredict = dataFrameFromParquet.withColumn(keyFeature,lit(labelDefaultValue))
|
|
|
+ }
|
|
|
+ //添加部分到此结束
|
|
|
+
|
|
|
|
|
|
|
|
|
- //这一段是直接复制的Lib,获得数据时,不需要处理label数据,全部删除
|
|
|
- parquetFile.select()
|
|
|
- val featuresDF = parquetFile.select(keyFeature,fieldNames:_*).persist()
|
|
|
+ val featuresDF = dataFrameForPredict.select(keyFeature,fieldNames:_*).persist()
|
|
|
featuresDF.schema.foreach(println)
|
|
|
|
|
|
val strFeatures = featuresDF.schema.filter(_.dataType.equals(StringType)).map(_.name)
|