Question

Dynamic Feature Writer Transformer

  • 22 August 2019
  • 7 replies
  • 13 views

Badge

I have a feature writer transformer that exports to .csv. For the dataset parameter and CSV File Name I have an attribute "_dirpath" and "_outputName" that are both dynamical created based on the input and the time.

The problem is I want User Attributes to be set to "Automatic" but at the same time there are certain generated attributes that I don't want showing up in the output.

I can use an "Attribute Remover" on most of them with the exception of "_dirpath" and "_outputName" as I need them to tell the feature writer where to write the file to. How do I get out of this catch-22 of having "_dirpath" and "_outputName" available to the feature writer but NOT outputting to the file?

I tried using "VariableSetter" and "VariableRetriever" to assign the attribute to a Private User param but this does not work as I later read that "FME configures user parameters before starting the translation, and their values cannot be changed in the workflow later." Having a global variable also does not work as the "FeatureWriter" won't take it

I could reopen the file after it is written to delete these two extra fields but this very hacky and don't want to do this.

 

[UPDATE]

I tired putting the feature writer into a custom transformer as mentioned here so that upon creation the attributes show up as params. 

This works and I can use the parmas in the feature writer. However, when I use a AttributeRemover to get rid of "_dirpath" and "_outputName" just before passing to the feature writer, it stops working and gives the following error.

test3_FeatureWriter(WriterFactory): MULTI_WRITER: No dataset was specified for MULTI_WRITER_DATASET or test3_FeatureWriter_0_DATASET or CSV2_DATASET
MULTI_WRITER: No dataset was specified for MULTI_WRITER_DATASET or test3_FeatureWriter_0_DATASET or CSV2_DATASET

Delete the Attribute Remover and it works again, but with the originally problem of "_dirpath" and "_outputName" being in the output... makes no sense to me


7 replies

Userlevel 1
Badge +10

Are the dirpath and outputName derived from the data or just based on an input parameter and the time? If they're not generated from the data you could create a scripted python parameter to generate them.

Badge

Are the dirpath and outputName derived from the data or just based on an input parameter and the time? If they're not generated from the data you could create a scripted python parameter to generate them.

dirpath is taken from fme_dataset of the input (The filepath of the input file)

outputName is the input filename with a timestamp appended to it

For Example:

Input: C:\\Temp\\MyFile.csv

Output: C:\\Temp\\MyFile_2019-8-22.csv

 

Userlevel 1
Badge +10

dirpath is taken from fme_dataset of the input (The filepath of the input file)

outputName is the input filename with a timestamp appended to it

For Example:

Input: C:\Temp\MyFile.csv

Output: C:\Temp\MyFile_2019-8-22.csv

 

In which case a couple of scripted parameters will do the trick

One for the dirpath

import os
filename = FME_MacroValues['SourceDataset']
path = os.path.dirname(filename)
return path

One for the filename

import os
from datetime import datetime

filename = FME_MacroValues['SourceDataset']
filename = os.path.splitext(os.path.basename(filename))[0]
now = datetime.now().strftime('%Y-%m-%d')
return filename+'_'+now
Badge

In which case a couple of scripted parameters will do the trick

One for the dirpath

import os
filename = FME_MacroValues['SourceDataset']
path = os.path.dirname(filename)
return path

One for the filename

import os
from datetime import datetime

filename = FME_MacroValues['SourceDataset']
filename = os.path.splitext(os.path.basename(filename))[0]
now = datetime.now().strftime('%Y-%m-%d')
return filename+'_'+now

Do I use the python caller transformer for these? Or the Python Startup script?

Thank you, I'm done for the day but will try again tomorrow what you said above. 

Also I update my question above with another thing I tired. If what you say does not work just going to write the whole thing in python since FME does not seem to play nice 

Userlevel 1
Badge +10

Do I use the python caller transformer for these? Or the Python Startup script?

Thank you, I'm done for the day but will try again tomorrow what you said above. 

Also I update my question above with another thing I tired. If what you say does not work just going to write the whole thing in python since FME does not seem to play nice 

These are for python scripted parameters, so create a user parameter, choose a type of Scripted (python), give it a name and then paste the code. You'll need to change this line

filename = FME_MacroValues['SourceDataset']

to reference the name of the parameter of the source dataset

Badge

These are for python scripted parameters, so create a user parameter, choose a type of Scripted (python), give it a name and then paste the code. You'll need to change this line

filename = FME_MacroValues['SourceDataset']

to reference the name of the parameter of the source dataset

 I tired that but still running into same problem where if try using the attribute remove it stops working, Thanks anyways

Userlevel 1
Badge +10

One you have these as parameters there should be no need to use attributes to set the output filename

Reply