복붙노트

[SCALA] 스파크 날짜를 유닉스 타임 스탬프로 변환하는 방법

SCALA

스파크 날짜를 유닉스 타임 스탬프로 변환하는 방법

내가 nscala 시간을 시도했지만이 일을하지 않습니다, 나는 유닉스 타임 스탬프 (eg.1435655706000)의 열이있는 데이터 프레임을 가지고 있고, 나는 형식 'YYYY-MM-DD'와 데이터로 변환합니다.

val time_col = sqlc.sql("select ts from mr").map(_(0).toString.toDateTime)
time_col.collect().foreach(println)

나는 오류가 발생했습니다 : 예외 : IllegalArgumentException : 잘못된 형식 : "1435655706000"는 "6000"에서, 부정한다

해결법

  1. ==============================

    1.spark1.5 때문에, 그 일을위한 내장의 UDF있다.

    spark1.5 때문에, 그 일을위한 내장의 UDF있다.

    val df = sqlContext.sql("select from_unixtime(ts,'YYYY-MM-dd') as `ts` from mr")
    

    추가 정보를 원하시면 스파크 1.5.2 API의 문서를 확인하시기 바랍니다.

  2. ==============================

    2.여기 스칼라 DataFrame 기능을 사용하고 있습니다 : FROM_UNIXTIME 및 TO_DATE을

    여기 스칼라 DataFrame 기능을 사용하고 있습니다 : FROM_UNIXTIME 및 TO_DATE을

    // NOTE: divide by 1000 required if milliseconds
    // e.g. 1446846655609 -> 2015-11-06 21:50:55 -> 2015-11-06 
    mr.select(to_date(from_unixtime($"ts" / 1000))) 
    
  3. ==============================

    3.

    import org.joda.time.{DateTimeZone}
    import org.joda.time.format.DateTimeFormat
    

    다음과 같은 라이브러리를 가져와야합니다.

    val stri = new DateTime(timeInMillisec).toDateTime.toString("yyyy/MM/dd")
    

    또는 귀하의 경우에 조정 :

     val time_col = sqlContext.sql("select ts from mr")
                         .map(line => new DateTime(line(0).toInt).toDateTime.toString("yyyy/MM/dd"))
    

    또 다른 방법이있을 수 있습니다 :

      import com.github.nscala_time.time.Imports._
    
      val date = (new DateTime() + ((threshold.toDouble)/1000).toInt.seconds )
                 .toString("yyyy/MM/dd")
    

    도움이 되었기를 바랍니다 :)

  4. ==============================

    4.나는 DataFrame에 매핑하여 joda 시간 라이브러리를 사용하여 문자열로 날짜 시간 변환이 문제를 해결했다 :

    나는 DataFrame에 매핑하여 joda 시간 라이브러리를 사용하여 문자열로 날짜 시간 변환이 문제를 해결했다 :

    import org.joda.time._
    val time_col = sqlContext.sql("select ts from mr")
                             .map(line => new DateTime(line(0)).toString("yyyy-MM-dd"))
    
  5. ==============================

    5.당신은 nscala_time와 toDataTime을 적용하기 전에 문자열로 변환 할 필요는 없다

    당신은 nscala_time와 toDataTime을 적용하기 전에 문자열로 변환 할 필요는 없다

    수입 com.github.nscala_time.time.Imports._

    scala> 1435655706000L.toDateTime
    res4: org.joda.time.DateTime = 2015-06-30T09:15:06.000Z
    

    `

  6. ==============================

    6.당신이 할 수있는 것은 :

    당신이 할 수있는 것은 :

    input.withColumn("time", concat(from_unixtime(input.col("COL_WITH_UNIX_TIME")/1000,
    "yyyy-MM-dd'T'HH:mm:ss"), typedLit("."), substring(input.col("COL_WITH_UNIX_TIME"), 11, 3), 
    typedLit("Z")))
    

    여기서 시간은 새 열 이름이고 COL_WITH_UNIX_TIME은 변환 할 컬럼의 이름입니다. 이 같은 데이터가 더 정확하고, 밀리에 데이터를 제공 할 것이다 "YYYY-MM-dd'T'HH : mm : ss.SSS'Z '"

  7. ==============================

    7.당신은 자바에서 다음과 같은 구문을 사용할 수 있습니다

    당신은 자바에서 다음과 같은 구문을 사용할 수 있습니다

    input.select("timestamp)
                .withColumn("date", date_format(col("timestamp").$div(1000).cast(DataTypes.TimestampType), "yyyyMMdd").cast(DataTypes.IntegerType))
    
  8. from https://stackoverflow.com/questions/31134969/how-to-convert-unix-timestamp-to-date-in-spark by cc-by-sa and MIT license