从scala

时间:2016-10-10 01:19:06

标签: scala apache-spark-sql spark-cassandra-connector

我的日期方括号 [2014-11-08 06:27:00.0] ,想删除它。

预期产量 2014-11-08 06:27:00.0

val conf = new SparkConf(true)
   .set("spark.cassandra.connection.host", "127.0.0.1").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")
input.setTimeZone(TimeZone.getTimeZone("GMT"))
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 = data.map(row => {
                         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"))
sc.stop()         

执行此代码后,我收到错误:

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

我认为这个错误是因为日期有方括号,所以,我想删除它。

3 个答案:

答案 0 :(得分:4)

您可以使用.replaceAll和正则表达式删除不需要的字符。

str.replaceAll("[\\[\\]]","")

将从字符串中删除开始和结束方括号。

答案 1 :(得分:1)

解决方案如下:

val result = data.map(row => {
    val day = dia.format(input.parse(row.toString().replace("[", "").replace("]", "").replace("(", "").replace(")", "")))  
    val month = mes.format(input.parse(row.toString().replace("[", "").replace("]", "").replace("(", "").replace(")", "")))                                       
    val year = ano.format(input.parse(row.toString().replace("[", "").replace("]", "").replace("(", "").replace(")", "")))                                        
    val hour = horas.format(input.parse(row.toString().replace("[", "").replace("]", "").replace("(", "").replace(")", "")))                                     
    val minute = minutos.format(input.parse(row.toString().replace("[", "").replace("]", "").replace("(", "").replace(")", "")))
})

我测试了它并且有效。

输入日期:

  data: Array[org.apache.spark.sql.Row] = Array([2015-08-20 21:01:00.0]

输出:

  Array(List(20, 08, 2015, 21, 01)

答案 2 :(得分:0)

dataset.select(“id”)。distinct.as [String] .collect如果使用Spark 2.0则打印为字符串数组

相关问题