Java Timestamp to BigInt for Impala

强颜欢笑 提交于 2019-12-11 11:49:49

问题


I am reading a text file which has a field in Timestamp in this format "yyyy-MM-dd HH:mm:ss"

I want to be able to convert it to a field in Impala as BigInt and should like yyyMMddHHmmss in Java.

I am using Talend for the ETL but I get this error "schema's dbType not correct for this component" and so I want to have the right transformation in my tImpalaOutput component


回答1:


One obvious option is to read the date in as a string, format it to the output you want and then convert it to a long before sending it to Impala.

To do this you would start by using Talend's parseDate function with something like:

TalendDate.parseDate("yyyy-MM-dd HH:mm:ss",row1.date)

This parses the date string into a Date type object. From here you can convert this into your desired string format with:

TalendDate.formatDate("yyyMMddHHmmss",row2.date)

Alternatively this can be done in one go with:

TalendDate.formatDate("yyyMMddHHmmss",TalendDate.parseDate("yyyy-MM-dd HH:mm:ss",row1.date))

After this you should have a date string in your desired format. You can then cast this to a Long using a tConvertType component or the following Java code:

Long.valueOf(row3.date)

Or, once again we can do the whole thing in a one liner:

Long.valueOf(TalendDate.formatDate("yyyMMddHHmmss",TalendDate.parseDate("yyyy-MM-dd HH:mm:ss",row1.date)))

From here you should be able to send this to Impala as a Java Long to an Impala BIGINT field.



来源:https://stackoverflow.com/questions/27282037/java-timestamp-to-bigint-for-impala

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!