Logstash _dateparsefailure matching timestamp with date plugin - date

I have a json input with the String field timestamp that I want to parse to date in the field #timestamp in elasticsearch.
The input timestamp field: 2021-06-20 03:37:14.595000+00:00
This is how I've set up the filter in logstash:
date {
match => ["timestamp", "ISO8601", "yyyy-MM-dd HH:mm:ss.SSSSSS+ZZ:ZZ", "yyyy-MM-dd HH:mm:ss.SSSSSS"]
target => "#timestamp"
}
The input string is in ISO8601 format, so using only "ISO8601" should work. However, I'm getting the _dateparsefailure. Therefore, I've also tried with the patterns "yyyy-MM-dd HH:mm:ss.SSSSSS+ZZ:ZZ" and "yyyy-MM-dd HH:mm:ss.SSSSSS", with no luck.
I've also tried to set the target to something else, like my_timestamp, in case the value of #timestamp is being overwritten, but that didn't work either.
Could you help me understand why this does not work?

ZZ is used to match "colon in between hour and minute offsets" so you should use "yyyy-MM-dd HH:mm:ss.SSSSSS+ZZ".

Related

talend format yyyy-MM-dd'T'HH:mm:ss.SSSz to yyyy-mm-dd HH:mm:ss

I am trying to change the date format in txmlmap component but its not working
i want change date format
from yyyy-MM-dd'T'HH:mm:ss.SSSz to yyyy-mm-dd HH:mm:ss
expected output:- yyyy-mm-dd HH:mm:ss
You can parse your string to a date using your source pattern and then format that date to a string using your target pattern:
TalendDate.formatDate("yyyy-mm-dd HH:mm:ss", TalendDate.parseDate("yyyy-MM-dd'T'HH:mm:ss.SSSz", myDateString))
In almost all coding languages format is text, while date is a double. That means you must first make a date of the first expression, before setting the new format of that date. But in Your case the 'T' is some kind of special format that need to be replaced with a blanck space. I have no idea about what it would look like in talend but in VB it would look like this:
' from yyyy-MM-dd'T'HH:mm:ss.SSSz to yyyy-mm-dd HH:mm:ss
DateTxt = "2022-12-01'T'22:45:10"
DateTxt = Replace(DateTxt, "'T'", " ")
MyDate = CDate(DateTxt)
MsgBox Format(MyDate, "yyyy-mm-dd HH:mm:ss")

Pyspark: Output to csv -- Timestamp format is different

I am working with a dataset with the following Timestamp format: yyyy-MM-dd HH:mm:ss
When I output the data to csv the format changes to something like this: 2019-04-29T00:15:00.000Z
Is there any way to get it to the original format like: 2019-04-29 00:15:00
Do I need to convert that column to string and then push it to csv?
I am saying my file to csv like so:
df.coalesce(1).write.format("com.databricks.spark.csv"
).mode('overwrite'
).option("header", "true"
).save("date_fix.csv")
Alternative
spark >=2.0.0
set option("timestampFormat", "yyyy-MM-dd HH:mm:ss") for format("csv")
df.coalesce(1).write.format("csv"
).mode('overwrite'
).option("header", "true"
).option("timestampFormat", "yyyy-MM-dd HH:mm:ss"
).save("date_fix.csv")
As per documentation-
timestampFormat (default yyyy-MM-dd'T'HH:mm:ss.SSSXXX): sets the string that indicates a timestamp format. Custom date formats follow the formats at java.text.SimpleDateFormat. This applies to timestamp type.
spark < 2.0.0
set option("dateFormat", "yyyy-MM-dd HH:mm:ss") for format("csv")
df.coalesce(1).write.format("com.databricks.spark.csv"
).mode('overwrite'
).option("header", "true"
).option("dateFormat", "yyyy-MM-dd HH:mm:ss"
).save("date_fix.csv")
As per documentation-
dateFormat: specifies a string that indicates the date format to use when reading dates or timestamps. Custom date formats follow the formats at java.text.SimpleDateFormat. This applies to both DateType and TimestampType. By default, it is null which means trying to parse times and date by java.sql.Timestamp.valueOf() and java.sql.Date.valueOf()
ref - readme
Yes, that's correct. The easiest way to achieve this is using pyspark.sql.functions.date_format such as:
import pyspark.sql.functions as f
df.withColumn(
"date_column_formatted",
f.date_format(f.col("timestamp"), "yyyy-MM-dd HH:mm:ss")
)
More info about it here https://spark.apache.org/docs/latest/api/python/pyspark.sql.html#pyspark.sql.functions.date_format.
Hope this helps!

Date filter for logstash seems to change date to older value

I am parsing logs in the format
2017-04-03 05:48:16,129....
I am parsing this using grok as
match => {"message" => "%{TIMESTAMP_ISO8601:timestamp},.*\|%{DATA:logLevel}\|.*\|.*\|.*\|.*\|.*\|%{DATA:service}\|.*\|.*\|.*\|%{DATA:successMsg} :%{USERNAME:loginUser}"}
After this I am applying date filter to get timestamp value as -
date {
match => [ "timestamp", "yyyy-mm-dd HH:mm:ss" ]
target => "timestamp"
}
The value I receive in elastic is offset by 3 months.
ie - 2017-04-03 05:48:16 is coverted to January 3rd 2017, 05:48:16
All other fields are fine. Where am I going wrong?
Found an answer somewhere else.
https://discuss.elastic.co/t/date-filter-for-logstash-seems-to-change-date-to-older-value/80983
It's only that the month should be MM and not mm. Not sure how this led to the behavior though.

How to set client specific timezone and date

pardon me if it seems to be a duplicate question.
I have seen many posts already on this topic. However after trying many examples could not find the solution to my problem.
I tried this code
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy", Locale.ENGLISH )
Date newDate = sdf.parse(sdf.format( new Date( dateTimeString ) ) )
However the second line of code always converts the date to the server specific date and timezone which i don't want. I also tried the following
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss zzz", Locale.ENGLISH )
log.info "+++++++++++++++++hidden date ++++++++ " + params.hiddenGameDateTime.substring(35, 38)
log.info "x = " + sdf.format( new Date ( params.hiddenGameDateTime ))
String tzone = params.hiddenGameDateTime.substring(35, 38)
sdf.setTimeZone( TimeZone.getTimeZone( tzone ) )
log.info "Timezone = " + sdf.getTimeZone().getDisplayName()
Please note that
sdf.format( new Date( dateTimeString ) )
gives me the desired result, however it gives me the string value of the date and the actual value to be stored in database is of the Data type date which can't hold the string value.
the value for date and time in my case gets converted to PST date and time. how can i avoid this. The user input date with timezone should be stored in the database as it is with no change in date and timezone.
An observation: The constructor new Date( dateTimeString ) is deprecated. A better replacement would be something like that:
SimpleDateFormat sdfOriginal = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
Date d = sdfOriginal.parse(dateTimeString);
Furthermore: An expression like sdf.parse(sdf.format(...)) using the same format object does not make much sense.
But most important, your statement "the second line of code always converts the date to the server specific date and timezone" seems to be based on test output like:
System.out.println(newDate);
This implicitly uses toString() which is based on jvm default time zone, in your case the server time zone. But keep in mind, the internal state of j.u.Date does not reference any time zone. A Date is just a container for a long, namely the seconds since 1970-01-01T00:00:00Z in UTC time zone, that is a global time.
Additional remark:
If you need the client time zone (in a scenario with multiple users in different time zones) to create user-specific formatted date strings, then you indeed need to store the time zone preference of every user in the database, so you can use this information for output in an expression like:
SimpleDateFormat sdf = new SimpleDateFormat("{pattern}";
sdf.setTimeZone(TimeZone.getTimeZone("{user-preference-time-zone}");
String userOutput = sdf.format(date);
Date is always jvm timezone specific. You need to normalize it to standard time and store it in DB to cater with different timezone servers.

Parsing String to date doesnt work

I tried parsing a string in a namedQuery, but it seems doesnt work. I have this code in my domain class:
searchBirthdaten{ q ->
def dates = Date.parse("yyyyy:MM:dd HH:mm:ss", "2011-9-21 00:00:00")
eq 'birthDate' , dates)
}
But I always got this error:
Unparseable date: "2011-9-21 00:00:00"
I really dont understand why this is happening. Any idea?
Your date input string has to be in the format you defined: yyyy:MM:dd HH:mm:ss (corrected)
So your 3 issues were:
You are using the "-" character to delimit you date for parsing but your format string is using ":"
You have 5 ys in your format string i.e. yyyyy:MM.... Which won't be valid for another 8 thousandish years ;)
You define your month format as MM but you are passing only '9', this will need to be '09' to match your fomat string.