CsvAdapter.scala 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package com.example.data
  2. import java.util
  3. import com.example.SparkConnect
  4. import com.example.dao.{FileInfoDao, HdfsDao, HeaderInfoDao}
  5. import com.example.entity.{FileInfo, HeaderInfo}
  6. import com.example.services.FileService
  7. import org.apache.spark.sql.{DataFrame, SaveMode}
  8. import org.springframework.beans.factory.annotation.Autowired
  9. import org.springframework.stereotype.Component
  10. import scala.collection.JavaConverters._
  11. /**
  12. * Convert CSV to Parquet
  13. * Created by lucas on 2016/10/10.
  14. * Trait configuration:
  15. * 1. SparkConf as sparkConf
  16. * 2. SQLContext as sQLContext
  17. * 3. HDFS Server address as hdfsServer
  18. * 4. Logger as log
  19. */
  20. @Component
  21. class CsvAdapter extends SparkConnect{
  22. @Autowired val hdfsDao: HdfsDao = null
  23. @Autowired val fileInfoDao: FileInfoDao = null
  24. @Autowired val headerInfoDao: HeaderInfoDao = null
  25. @Autowired val fileService: FileService = null
  26. /**
  27. *
  28. * @param fileLoc
  29. * @return
  30. */
  31. @throws(classOf[Exception])
  32. def parquetFileReader(fileLoc: String): util.List[util.Map[String, String]] = {
  33. val inFile = sparkSession.read.parquet(fileLoc).persist()
  34. val columns = inFile.columns
  35. val res = inFile.collect().map(row => {
  36. row.toSeq.zipWithIndex.map(pair => {
  37. (columns(pair._2), pair._1.toString)
  38. }).toMap.asJava
  39. }).toList
  40. println("haha: " + res)
  41. res.asJava
  42. }
  43. /**
  44. *
  45. * @param inFile {String}
  46. * @param outFile {String}
  47. * @return DocId {String}
  48. * @since 1.0.0
  49. */
  50. @throws(classOf[Exception])
  51. def Csv2Parquet(inFile: String, outFile: String, userId: Int): Int = {
  52. //1. Load local csv file to HDFS
  53. val csvHdfsLoc = hdfsDao.putFile(inFile)
  54. log.info("Initial spark config done.")
  55. //2. Read csv file to Dataframe, the schema property as String
  56. val df = sparkSession.read
  57. .option("header", "true")
  58. .option("inferSchema", "true")
  59. .csv(csvHdfsLoc)
  60. .persist()
  61. /*//-> 2. Preprocessing file(String to Int & nulls fill)
  62. val strFeatures = df.schema.filter(_.dataType.equals(StringType)).map(_.name)
  63. val notNullFeatureDF = df.na.fill(-1).na.fill("NA", strFeatures)
  64. val indexers = strFeatures.map(field => {
  65. new StringIndexer()
  66. .setInputCol(field)
  67. .setOutputCol("new_" + field)
  68. .fit(notNullFeatureDF)
  69. })
  70. var res:DataFrame = null
  71. if (indexers.nonEmpty) {
  72. val pipeline = new Pipeline().setStages(indexers.toArray[StringIndexerModel])
  73. res = pipeline.fit(notNullFeatureDF).transform(notNullFeatureDF).drop(strFeatures: _*)
  74. } else {
  75. res = notNullFeatureDF
  76. }
  77. res.show()*/
  78. //3. now simply write to a parquet file
  79. df.coalesce(1).write.mode(SaveMode.Overwrite).parquet(hdfsUri + outFile)
  80. //4. delete the csv file
  81. hdfsDao.deleteFileInHdfs(csvHdfsLoc, true)
  82. //5. Store filename to db
  83. val fileId = fileInfoDao.save(new FileInfo(hdfsUri + outFile, userId, outFile)).getId
  84. //6. 将header存入数据库中
  85. df.schema.fields.map(field => {
  86. headerInfoDao.save(new HeaderInfo(fileId, field.name, field.dataType.catalogString))
  87. })
  88. //7. 分析文件
  89. fileService.analysis(fileId);
  90. fileId
  91. }
  92. }