Commit 3af73789 authored by 杨林's avatar 杨林

格式化工具

parent b589b91e
package tools.dataprocess
import model.TrackItem
import tools.geoip.GeoIp
import tools.json.JsonUtils
import tools.timestamp.DateHelper
/**
* Created with IntelliJ IDEA.
* Class: FormatTrack
* Description:
* User: lin
* Date: 2021-07-22
* Time: 17:03
*/
object DataFormat {
def formatTrack(x: String): TrackItem = {
val data = JsonUtils parseFirstKey x
val data_decode = data getOrElse("data_decode", "")
val dataMap = JsonUtils parseFirstKey data_decode
val lib = dataMap.getOrElse("lib", "")
val libMap = JsonUtils parseFirstKey lib
val ip = data.getOrElse("ip", "").replaceAll("\"", "")
TrackItem(
track_id = dataMap.getOrElse("_track_id", "").replaceAll("\"", "").toLong,
distinct_id = dataMap.getOrElse("distinct_id", "").replaceAll("\"", ""),
lib_detail = libMap.getOrElse("$lib_detail", "").replaceAll("\"", ""),
lib_version = libMap.getOrElse("$lib_version", "").replaceAll("\"", ""),
lib = libMap.getOrElse("$lib", "").replaceAll("\"", ""),
app_version = libMap.getOrElse("$app_version", "").replaceAll("\"", ""),
lib_method = libMap.getOrElse("$lib_method", "").replaceAll("\"", ""),
event_name = dataMap.getOrElse("event", "").replaceAll("\"", ""),
`type` = dataMap.getOrElse("type", "").replaceAll("\"", ""),
properties = dataMap.getOrElse("properties", ""),
HOST = data.getOrElse("Host", "").replaceAll("\"", ""),
user_agent = data.getOrElse("User_Agent", "").replaceAll("\"", ""),
ua_platform = data.getOrElse("ua_platform", "").replaceAll("\"", ""),
ua_version = data.getOrElse("ua_version", "").replaceAll("\"", ""),
ua_language = data.getOrElse("ua_language", "").replaceAll("\"", ""),
`connection` = data.getOrElse("Connection", "").replaceAll("\"", ""),
Pragma = data.getOrElse("Pragma", "").replaceAll("\"", ""),
cache_control = data.getOrElse("Cache_Control", "").replaceAll("\"", ""),
accept = data.getOrElse("Accept", "").replaceAll("\"", ""),
accept_encoding = data.getOrElse("Accept_Encoding", "").replaceAll("\"", ""),
ip = ip,
ip_info = JsonUtils.parseBeanToString(GeoIp(ip).getCity),
url = data.getOrElse("url", "").replaceAll("\"", ""),
referrer = data.getOrElse("referrer", "").replaceAll("\"", ""),
remark = data.getOrElse("remark", "").replaceAll("\"", ""),
user_id = data.getOrElse("user_id", "").replaceAll("\"", ""),
created_at = DateHelper.parseTimestampToDataTime(data.getOrElse("created_at", "").toLong)
)
}
}
package tools.timestamp
import java.text.SimpleDateFormat
import java.time.format.{DateTimeFormatter, DateTimeFormatterBuilder}
import java.time.{OffsetDateTime, ZoneId, ZonedDateTime}
import java.util.Date
import scala.language.postfixOps
/**
* Created with IntelliJ IDEA.
* Class: DateHelper
* Description: 时间处理类
* User: lin
* Date: 2021-07-19
* Time: 16:17
*/
object DateHelper {
private[this] val DATE_TIME_FORMATTER: DateTimeFormatter = new DateTimeFormatterBuilder().appendOptional(DateTimeFormatter.ISO_DATE_TIME).appendOptional(DateTimeFormatter.ISO_OFFSET_DATE_TIME).appendOptional(DateTimeFormatter.ISO_INSTANT).appendOptional(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SX")).appendOptional(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ssX")).appendOptional(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")).toFormatter.withZone(ZoneId.systemDefault())
/**
* 把时间字符串转换成标准时间
*
* @param str 时间字符串
* @return 标准时间
*/
def parseDateTimeString(str: String): OffsetDateTime = ZonedDateTime.from(DATE_TIME_FORMATTER.parse(str)).toOffsetDateTime
/**
* 时间戳转换成时间
*
* @param timestamp 时间戳(毫秒)
* @param pattern 时间格式
* @return 指定格式的日期
*/
def parseTimestampToDataTime(timestamp: Long, pattern: String): String = new SimpleDateFormat(pattern).format(new Date(formatTimestamp(timestamp)))
/**
* 时间戳转换成时间(默认格式)
*
* @param timestamp 时间戳(毫秒)
* @return 默认格式“yyyy-MM-dd HH:mm:ss”
*/
def parseTimestampToDataTime(timestamp: Long): String = parseTimestampToDataTime(formatTimestamp(timestamp), "yyyy-MM-dd HH:mm:ss")
/**
* 把时间格式定为毫秒,如果不是毫秒 x1000
*
* @param timestamp 时间戳
* @return 格式化时间戳
*/
def formatTimestamp(timestamp: Long): Long = if (timestamp.toString.length < 13) timestamp * 1000 else timestamp
/**
* 把时间转换成时间戳
*
* @param time 时间
* @param pattern 时间格式
* @return 时间戳
*/
def parseDateTimeToTimestamp(time: String, pattern: String): Long = new SimpleDateFormat(pattern).parse(time).getTime
/**
* 把时间转换成时间戳(默认格式)
*
* @param time 时间
* @return 时间戳(默认格式:”yyyy-MM-dd HH:mm:ss“)
*/
def parseDateTimeToTimestamp(time: String): Long = parseDateTimeToTimestamp(time, "yyyy-MM-dd HH:mm:ss")
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment