Marisa Cruz Marisa Cruz - 1 year ago 160
Scala Question

Remove square brackets [] from Array in scala

I have the date with square brackets [2014-11-08 06:27:00.0], and a want remove it.

Expected output is 2014-11-08 06:27:00.0

val conf = new SparkConf(true)
.set("", "").setAppName("CasteDate").setMaster("local[*]")
.set("spark.cassandra.connection.port", "9042")
.set("spark.driver.allowMultipleContexts", "true")
.set("spark.streaming.receiver.writeAheadLog.enable", "true")

val sc = new SparkContext(conf)

val ssc = new StreamingContext(sc, Seconds(1))
val csc=new CassandraSQLContext(sc)

val sqlContext = new org.apache.spark.sql.SQLContext(sc)

var input: SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S")
var dia: SimpleDateFormat = new SimpleDateFormat("dd")
var mes: SimpleDateFormat = new SimpleDateFormat("MM")
var ano: SimpleDateFormat = new SimpleDateFormat("yyyy")
var horas: SimpleDateFormat = new SimpleDateFormat("HH")
var minutos: SimpleDateFormat = new SimpleDateFormat("mm")

val data=csc.sql("SELECT timecol from smartgrids.analyzer_temp").collect()

import sqlContext.implicits._

val result = => {
val day = dia.format(input.parse(row.toString()))
val month = mes.format(input.parse(row.toString()))
val year = ano.format(input.parse(row.toString()))
val hour = horas.format(input.parse(row.toString()))
val minute = minutos.format(input.parse(row.toString()))

val collection = sc.parallelize(Seq(("day", 2), ("month", 2), ("year", 4), ("hour", 2), ("minute", 2)))
collection.saveToCassandra("features", "datepart", SomeColumns("day", "month", "year", "hour", "minute"))

After execute this code i receive the error:

java.text.ParseException: Unparseable date: "[2015-08-20 21:01:00.0]"
at java.text.DateFormat.parse(

I think that this error is because the date have square brackets, so, i want remove it.

Answer Source

You can use .replaceAll and a regular expression to remove the unwanted characters.


would remove opening and closing square brackets from a string.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download