+1 vote
1 view
in Big Data Hadoop & Spark by (830 points)

I am importing some data from an MS SQL table to Scala program. MS SQL table has around 20 fields so I am making a class to load those rows in my Scala program.

  1. In MS SQL there is one column whose data type is datetime, how do I store this kind of data type in my Scala program, I don't think Scala has this data type?

  2. There is one more column Price (numeric(14,4),not null) whose data type is numeric in MS SQL. How do I store these kind of values in Scala ?

2 Answers

0 votes
by (13.2k points)

1.

For Scala, we can import datetime package from java libraries.

For JAVA 8 + ,

Import the date object and formatter that is

java.time.LocalDate  

and

java.time.format.DateTimeFormatter

then, parse the string to date

LocalDate.parse(“[date]”,DateTimeFormatter.ofPattern("MM/dd/yyyy"))

then format the date to string,

date.format(DateTimeFormatter.ofPattern("[preferred format]"))

NOTE : By default, the ISO format is followed which is YYYY-MM-DD

For JAVA 7,

Use SimpleDateFormat to define format, 

 val originFormat = new SimpleDateFormat("[initial format]")

 val targetFormat = new SimpleDateFormat("[required format]")

targetFormat.format(originFormat.parse("[data]"))

2.

For price column, you can use float or double data type

     Float

32 bit IEEE 754 single-precision float

      Double

64 bit IEEE 754 double-precision float

0 votes
by (26.3k points)

You can practice DateTime, Scala uses a lot of Java libraries and so on beneath. So you can surely use DateTime. I'd like to recommend looking at the DateFormatter. If you want more information regarding the same, refer to the following video:

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...