Reputation: 596
I have recently started to work with Spark-Scala and now I am facing a issue which might be too trivial and basic but I am not able to catch it .
AIM -
I have a function which should take dateTime:String
and should return a Map(String,java.sql.Date)
.
Code -
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
}
}
Now when I am returning the Map, I am getting this error -
[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]
UPDATE -
This is how I am planning on calling this function -
val crrntWeek = utils.newgetWeekStartDate(utils.currentDate,"incremental").getOrElse("currentWeek","currentWeek Not Found")
This will then be used like this -
val existingDF = reader.readFrmHive(ConfigUtils.existingPath).filter(to_date(col("datetime"),"yyyy-MM-dd") >= crrntWeek
Complete Stack Trace -
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
Upvotes: 0
Views: 713
Reputation: 596
The Issue and the Error should have been self explanatory. Thanks @JOHN, for help me out here.
The only issue was -
When i am defining the return type to be of Type Map[String,java.sql.Date]
then, in the return , i shouldn't have added .withDefaultValue("NONE") because -
If there is no Input, then it would have picked up the default value which was NONE
i.e String
.
So, The above mentioned change, fixed the issue.
************* UPDATED ANSWER ****************
As per the recommendation @Ole V.V.
I have replaced the usage of 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
}
Does this look like good Implementation ?
Upvotes: 2