extract year from date type variable

One Star

extract year from date type variable

Hello,
I m using TOS3.10 / java under windows xp and I wonder how I can extract the year part of a date or datatime type?
I will use it in an expression filter of a Tmap. From several tables i want to make a join but not all rows of the input table are suitable for the join.
Can someone help me, since i do not know anything of java.
Michel
One Star

Re: extract year from date type variable

You can get the year using the Talend Date routines :
TalendDate.formatDate("yyyy",rowX.myDate)
One Star

Re: extract year from date type variable

Thanks bcourtine for your respond,
I put your suggestion in the expression filter of the tMap but it returned with an error message which says:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The left-hand side of an assignment must be a variable
See below a screenshot of my tMap design.
Can you please give your reaction. Can you perhaps explain to me what went wrong?
Thanks in advance,
Michel
Community Manager

Re: extract year from date type variable

Hello
There should be double '=',eg:
TalendDate.formatDate("yyyy",row1.date)=="2009"
Best regards

shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: extract year from date type variable

Hello Shong,
thanks for the reply, it works(which means no error message) but I 've no output. And I'm sure the yearpart 2009 or 2999 is in the table.
The date 2999-01-01 can be the value of the variable scd_end which is in the script:
TalendDate.formatDate("yyyy",row1.Scd_end)=="2009"
I doubt if the value of the TalendDate.formatDate("yyyy",row1.Scd_end) is a character value. It is for sure that the value is not the same as 2009 or 2999.
I hoped the following took place: "2009" = "2009" (so that particular row was selected).
Can you explain a solution.
Regards,
Michel
Community Manager

Re: extract year from date type variable

Hello
The method TalendDate.formatDate return a string, so you can try the following expresson:
TalendDate.formatDate("yyyy",row1.Scd_end).equals("2009")
Best regards
shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: extract year from date type variable

Shong,
thank you very much for your help, it works now! One more question, I've submit several question related to TOD, but no answer. Do you know why? Can you inform a colleque of you for me please? I would very much appreciate that.
Thanks in advance,
Michel
Five Stars

Re: extract year from date type variable

Hi all,
I have the same problem like micoppoolse, but in my case i want to get year of the current date.
I do so something like this, but it don't work.
TalendDate.formatDate("yyyy",TalendDate.getCurrentDate())

So may somebody help me please?
Thank you.
One Star

Re: extract year from date type variable

Here is for you IDEMBELE : 
TalendDate.getDate("CCYY")
Five Stars

Re: extract year from date type variable

Hi mathis,
Thank you, but this code    
TalendDate.getDate("CCYY")

     return a boolean
So to resolve the problem, i use this code
TalendDate.formatDate("yyyy",new Date())
One Star

Re: extract year from date type variable

Hi,
My requirement is:
I have a huge csv file and i would like to generate multiple files for each year(Only based on year) and these files  has to save into their respective year folders automatically. I have a date column. 
it is very urgent. Plz help me on this.
kind regards,
Asha
One Star

Re: extract year from date type variable

org.talend.designer.hdfsbrowse.exceptions.HadoopServerException: org.talend.designer.hdfsbrowse.exceptions.HadoopServerException: java.util.concurrent.ExecutionException: java.lang.reflect.InvocationTargetException
            at org.talend.designer.hdfsbrowse.hadoop.service.check.AbstractCheckedServiceProvider.checkService(AbstractCheckedServiceProvider.java:51)
            at org.talend.designer.hdfsbrowse.hadoop.service.HadoopServiceBean.check(HadoopServiceBean.java:102)
            at org.talend.designer.hdfsbrowse.hadoop.service.check.CheckHadoopServicesDialog$5.run(CheckHadoopServicesDialog.java:373)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
            at java.lang.Thread.run(Unknown Source)
Caused by: org.talend.designer.hdfsbrowse.exceptions.HadoopServerException: java.util.concurrent.ExecutionException: java.lang.reflect.InvocationTargetException
            at org.talend.designer.hdfsbrowse.hadoop.service.check.CheckedWorkUnit.execute(CheckedWorkUnit.java:47)
            at org.talend.designer.hdfsbrowse.hadoop.service.check.AbstractCheckedServiceProvider.checkService(AbstractCheckedServiceProvider.java:48)
            ... 5 more
Caused by: java.util.concurrent.ExecutionException: java.lang.reflect.InvocationTargetException
            at java.util.concurrent.FutureTask.report(Unknown Source)
            at java.util.concurrent.FutureTask.get(Unknown Source)
            at org.talend.designer.hdfsbrowse.hadoop.service.check.CheckedWorkUnit.execute(CheckedWorkUnit.java:44)
            ... 6 more
Caused by: java.lang.reflect.InvocationTargetException
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
            at java.lang.reflect.Method.invoke(Unknown Source)
            at org.talend.core.utils.ReflectionUtils.invokeMethod(ReflectionUtils.java:166)
            at org.talend.designer.hdfsbrowse.hadoop.service.check.provider.CheckedNamenodeProvider.check(CheckedNamenodeProvider.java:61)
            at org.talend.designer.hdfsbrowse.hadoop.service.check.AbstractCheckedServiceProvider$1.run(AbstractCheckedServiceProvider.java:43)
            at org.talend.designer.hdfsbrowse.hadoop.service.check.CheckedWorkUnit$1.call(CheckedWorkUnit.java:65)
            at java.util.concurrent.FutureTask.run(Unknown Source)
            ... 3 more
Caused by: java.io.IOException: Failed on local exception: com.google.protobuf.InvalidProtocolBufferException: Protocol message end-group tag did not match expected tag.; Host Details : local host is: "CD390-HFYVVS1/10.1.10.2"; destination host is: "10.240.42.106":50070; 
            at org.apache.hadoop.net.NetUtils.wrapException(NetUtils.java:773)
            at org.apache.hadoop.ipc.Client.call(Client.java:1431)
            at org.apache.hadoop.ipc.Client.call(Client.java:1358)
            at org.apache.hadoop.ipc.ProtobufRpcEngine$Invoker.invoke(ProtobufRpcEngine.java:229)
            at com.sun.proxy.$Proxy50.getListing(Unknown Source)
            at org.apache.hadoop.hdfs.protocolPB.ClientNamenodeProtocolTranslatorPB.getListing(ClientNamenodeProtocolTranslatorPB.java:573)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
            at java.lang.reflect.Method.invoke(Unknown Source)
            at org.apache.hadoop.io.retry.RetryInvocationHandler.invokeMethod(RetryInvocationHandler.java:187)
            at org.apache.hadoop.io.retry.RetryInvocationHandler.invoke(RetryInvocationHandler.java:102)
            at com.sun.proxy.$Proxy51.getListing(Unknown Source)
            at org.apache.hadoop.hdfs.DFSClient.listPaths(DFSClient.java:2094)
            at org.apache.hadoop.hdfs.DFSClient.listPaths(DFSClient.java:2077)
            at org.apache.hadoop.hdfs.DistributedFileSystem.listStatusInternal(DistributedFileSystem.java:791)
            at org.apache.hadoop.hdfs.DistributedFileSystem.access$700(DistributedFileSystem.java:106)
            at org.apache.hadoop.hdfs.DistributedFileSystem$18.doCall(DistributedFileSystem.java:853)
            at org.apache.hadoop.hdfs.DistributedFileSystem$18.doCall(DistributedFileSystem.java:849)
            at org.apache.hadoop.fs.FileSystemLinkResolver.resolve(FileSystemLinkResolver.java:81)
            at org.apache.hadoop.hdfs.DistributedFileSystem.listStatus(DistributedFileSystem.java:849)
            ... 12 more
Caused by: com.google.protobuf.InvalidProtocolBufferException: Protocol message end-group tag did not match expected tag.
            at com.google.protobuf.InvalidProtocolBufferException.invalidEndTag(InvalidProtocolBufferException.java:94)
            at com.google.protobuf.CodedInputStream.checkLastTagWas(CodedInputStream.java:124)
            at com.google.protobuf.AbstractParser.parsePartialFrom(AbstractParser.java:202)
            at com.google.protobuf.AbstractParser.parsePartialDelimitedFrom(AbstractParser.java:241)
            at com.google.protobuf.AbstractParser.parseDelimitedFrom(AbstractParser.java:253)
            at com.google.protobuf.AbstractParser.parseDelimitedFrom(AbstractParser.java:259)
            at com.google.protobuf.AbstractParser.parseDelimitedFrom(AbstractParser.java:49)
            at org.apache.hadoop.ipc.protobuf.RpcHeaderProtos$RpcResponseHeaderProto.parseDelimitedFrom(RpcHeaderProtos.java:3167)
            at org.apache.hadoop.ipc.Client$Connection.receiveRpcResponse(Client.java:1083)
            at org.apache.hadoop.ipc.Client$Connection.run(Client.java:977)

Please help me
One Star

Re: extract year from date type variable

Lol i can't say for the error just ahead but for the date you have several options.
The easiest way is to open n times your file, not very efficient bur pretty simple
1)you get all the years, uniq those years, then you iterate (tflowtoiterate)
At this point you have an iteration for each different year in your csv
2) you put the year in a variable, then you reopen your file and filter on the year you need, and when you write on your files, you just have to put your variable in the file path. 
could be something like

tFileInputDelimited
tUniqRow
tFlowToIterate
tFileInputDelimited
tFilterRow
tFileOutputDelimited
One Star

Re: extract year from date type variable

Hi guys, 
Need your help on getting the year only of a date field, i tried using this TalendDate.formatDate("yyyy",rowX.myDate)
but I am having an error below "The cell format is not Date in ( Row. 1 and ColumnNum. 2 )"

can you help me ?