scala - 返回 Map(String,java.sql.Date) 时类型不匹配
问题描述
我最近开始使用 Spark-Scala,现在我面临一个可能过于琐碎和基本但我无法理解的问题。
目标 -
我有一个函数应该接受dateTime:String
并且应该返回一个Map(String,java.sql.Date)
.
代码 -
def newgetWeekStartDate(dateTime:String,mode:String):Map[String,java.sql.Date] = {
mode match {
case ExecMode.incremental =>
logInfo(s" ****************** ACCESSING TIMEUTILS IN INCREMENTAL MODE ******************")
val currentWeek = weekStartDate.getWeekStartDate(dateTime)
val weekRelatedInformationMap = Map("currentWeek" -> currentWeek)
weekRelatedInformationMap
// getWeekStartDate(dateTime)
case ExecMode.full =>
logInfo(s" ****************** ACCESSING TIMEUTILS in FULL MODE i.e 90 Days ******************")
val sdfOutput = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")
val cal = Calendar.getInstance()
cal.add(Calendar.DATE, -90)
val quarterDate = sdfOutput.format(cal.getTime())
val rangeWeek = weekStartDate.getWeekStartDate(quarterDate)
val currentWeek = weekStartDate.getWeekStartDate(dateTime)
val formattedCurrentWeek = Date.valueOf(currentWeek)
val formattedRangeWeek = Date.valueOf(rangeWeek)
val weekRelatedInformationMap1 = Map("currentWeek" -> formattedCurrentWeek,
"rangeWeek" -> formattedRangeWeek).withDefaultValue("NONE")
weekRelatedInformationMap1
}
}
现在,当我返回地图时,我收到了这个错误 -
[ERROR] .............. \src\main\scala\com\eg\modulename\utils\utils.scala:50: error: type mismatch;
[INFO] found : scala.collection.immutable.Map[String,Comparable[_ >: java.util.Date with String <: Comparable[_ >: java.util.Date with String <: java.io.Serializable] with java.io.Serializable] with java.io.
Serializable]
更新 -
这就是我计划调用此函数的方式-
val crrntWeek = utils.newgetWeekStartDate(utils.currentDate,"incremental").getOrElse("currentWeek","currentWeek Not Found")
然后将像这样使用 -
val existingDF = reader.readFrmHive(ConfigUtils.existingPath).filter(to_date(col("datetime"),"yyyy-MM-dd") >= crrntWeek
完整的堆栈跟踪 -
C:\Users\patrag\Documents\Git_REpo\new-winnew-update>mvn clean install
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.eg.modulename:winnew-update:jar:0.1.0
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-surefire-plugin is missing. @ line 112, column 15
[WARNING] 'build.plugins.plugin.version' for org.jacoco:jacoco-maven-plugin is missing. @ line 138, column 15
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building winnew-update 0.1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ winnew-update ---
[INFO] Deleting C:\Users\patrag\Documents\Git_REpo\new-winnew-update\target
[INFO]
[INFO] --- jacoco-maven-plugin:0.8.5:prepare-agent (default) @ winnew-update ---
[INFO] argLine set to -javaagent:C:\\Users\\patrag\\.m2\\repository\\org\\jacoco\\org.jacoco.agent\\0.8.5\\org.jacoco.agent-0.8.5-runtime.jar=destfile=C:\\Users\\patrag\\Documents\\Git_REpo\\new-windows-softwar
e-update\\target\\jacoco.exec
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ winnew-update ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 5 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ winnew-update ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-scala-plugin:2.15.2:compile (default) @ winnew-update ---
[INFO] Checking for multiple versions of scala
[WARNING] Expected all dependencies to require Scala version: 2.11.0
[WARNING] com.twitter:chill_2.11:0.9.3 requires scala version: 2.11.12
[WARNING] Multiple versions of scala libraries detected!
[INFO] includes = [**/*.java,**/*.scala,]
[INFO] excludes = []
[INFO] C:\Users\patrag\Documents\Git_REpo\new-winnew-update\src\main\scala:-1: info: compiling
[INFO] Compiling 22 source files to C:\Users\patrag\Documents\Git_REpo\new-winnew-update\target\classes at 1592903601259
[ERROR] C:\Users\patrag\Documents\Git_REpo\new-winnew-update\src\main\scala\com\eg\modulename\utils\utils.scala:50: error: type mismatch;
[INFO] found : scala.collection.immutable.Map[String,Comparable[_ >: java.util.Date with String <: Comparable[_ >: java.util.Date with String <: java.io.Serializable] with java.io.Serializable] with java.io.
Serializable]
[INFO] required: Map[String,String]
[INFO] weekRelatedInformationMap1
[INFO] ^
[WARNING] C:\Users\patrag\Documents\Git_REpo\new-winnew-update\src\main\scala\com\eg\modulename\writer\BaseWriter.scala:3: warning: imported `Writer' is permanently hidden by definition of trait Write
r in package writer
[INFO] import com.eg.modulename.writer.Writer
[INFO] ^
[WARNING] one warning found
[ERROR] one error found
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 15.915 s
[INFO] Finished at: 2020-06-23T14:43:26+05:30
[INFO] Final Memory: 26M/620M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.scala-tools:maven-scala-plugin:2.15.2:compile (default) on project winnew-update: wrap: org.apache.commons.exec.ExecuteException: Process exited with an error: 1(Exi
t value: 1) -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
解决方案
问题和错误应该是不言自明的。谢谢@JOHN,在这里帮助我。
唯一的问题是——
当我将返回类型定义为 TypeMap[String,java.sql.Date]
时,在 return 中,我不应该添加 .withDefaultValue("NONE") 因为 -
如果没有输入,那么它会选择默认值,NONE
即 ie String
。
*************更新的答案******************
根据@Ole VV 的建议
我已经替换了Calendar
, java.sql.Date
,的用法SimpleDateFormat
。
def getWeekStartDate(dateTime:String):String =
{
logInfo(s"********* INSIDE getWeekStartDate FUNCTION ************** Input DateTime - $dateTime")
val setSimpleDateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd")
val dayOfWeek = LocalDate.parse(dateTime,setSimpleDateFormat).getDayOfWeek
if (dayOfWeek != DateTimeConstants.MONDAY)
{
val new_date = LocalDate.parse(dateTime).plusDays(DateTimeConstants.MONDAY - dayOfWeek.getValue()).toString
val firstDayOfTheWeek:String = new_date.format(setSimpleDateFormat)
firstDayOfTheWeek
}
else
{
val new_date = dateTime
val firstDayOfTheWeek:String = new_date.format(setSimpleDateFormat)
firstDayOfTheWeek
}
}
def newgetWeekStartDate(dateTime:String,mode:String):Map[String,java.time.LocalDate] = {
mode match {
case ExecMode.incremental =>
logInfo(s" ****************** ACCESSING TIMEUTILS IN INCREMENTAL MODE ******************")
val currentWeek = weekStartDate.getWeekStartDate(dateTime)
val formattedCurrentWeek = LocalDate.parse(currentWeek)
val weekRelatedInformationMap = Map("currentWeek" -> formattedCurrentWeek)
weekRelatedInformationMap
// getWeekStartDate(dateTime)
case ExecMode.full =>
logInfo(s" ****************** ACCESSING TIMEUTILS in FULL MODE i.e 90 Days ******************")
val sdfOutput = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss")
val cal = LocalDateTime.now()
val rangeWeek = weekStartDate.getWeekStartDate(cal.plusMonths(-3).format(sdfOutput))
val currentWeek = weekStartDate.getWeekStartDate(cal.format(sdfOutput))
val formattedCurrentWeek = LocalDate.parse(currentWeek)
val formattedRangeWeek = LocalDate.parse(rangeWeek)
val weekRelatedInformationMap = Map("currentWeek" -> currentWeek,
"rangeWeek" -> rangeWeek).withDefaultValue("NONE")
val weekRelatedInformationMap1 = Map("currentWeek" -> formattedCurrentWeek,
"rangeWeek" -> formattedRangeWeek)
weekRelatedInformationMap1
}
这看起来像很好的实施吗?
推荐阅读
- matlab - 使用来自 arduino 串行打印的数据更新 matlab 中的线图,为什么会出现“数组形状错误”错误?
- regex - Postgresql 正则表达式仅匹配大写单词
- google-cloud-platform - Stackdriver Monitoring API 在获取时间序列指标数据时抛出错误
- android - 关闭应用程序时如何在Android通知中显示操作按钮?
- sql - 我想删除超过 6 年加上当前财政年度的记录
- regex - 用 OR 条件替换正则表达式
- python - Pythonic 索引而不丢失维度的方法
- express - 如何在 Sequelize 中以一对多关系更新行?
- jquery - prev() 给出未定义的值
- php - 通过一些值过滤PHP中的多维数组