Developing Apama Applications > Developing Adapters > Standard Apama Adapters > Apama File adapter > Opening files for writing
Opening files for writing
To open a file for writing, emit an OpenFileForWriting event. The definition of the OpenFileForWriting event is similar to the definition of the OpenFileForReading event:
event OpenFileForWriting
{
/* The name of the transport being used within the file adapter.
This should match the transport name used in the IAF config
file so the transport can recognize events intended for it */
string transportName;
 
/* Request ID for this open file event. The response, either a
FileHandle or a FileError event, will contain this ID */
integer requestId;
 
/* The name of the codec to use with the file. This should match
one of the codecs specified in the (static) config file. Use
the null codec (by default this is called JNullCodec) to write
entire lines */
string codec;
 
/* Full filename to write to */
string filename;
 
/* Boolean representing whether the file is to be overwritten or
appended */
boolean appendData;
 
/* This field is used to specify extra parameters to the codecs,
such as the CSVCodec and the FixedWidthCodec */
dictionary<string, string> payload;
}
The procedure for opening a file for writing CSV or fixed width files is effectively the same as for reading. Specify the relevant fields in the payload to describe the format of the file you want to write. When subsequently sending FileLine events, populate the data sequence field with the data for each field.
Again, once constructed, emit the OpenFileForWriting event to the "FILE" channel, for example:
emit new com.apama.file.OpenFileForWriting("FileTransport",
integer.getUnique(), "JNullCodec","/home/writeFile.txt",false);
For fixed width files, you can construct a more complex OpenFileForWriting event in a similar way to that described in Opening fixed width files.
Again, as with reading a file, the File adapter sends a FileHandle or FileError event (see Sending the read request), which your application should listen for, filtering on the requestId for the FileHandle event you are interested in.
Once a FileHandle event has been received, the file has successfully opened and the application can begin to send FileLine events to be written:
event FileLine
{
/* The name of the transport being used within the file adapter */
string transportName;
 
/* When sending these upstream (i.e. writing):
1) If this value is negative, the adapter will send no
acknowledgement.
A FileError may be sent back in response if this request
generates an error. If the user is always using the same
requestId e.g. -1, then they will not be able to work out
which line generated the error. The application writer is
free to ignore listening for these errors should they wish.
 
2) If this value is 0 or greater then the adapter will send an
acknowledgement LineWritten event back to the application.
*/
integer requestId;
 
/* The session Id this FileLine event is for or from */
integer sessionId;
 
/* The data as a sequence of strings. */
sequence<string> data;
}
Notice that the data field is a sequence of strings, rather than a string. This allows you to have the fields you want to write as separate entries in the sequence, and it lets the File adapter format the sequence for writing according to the chosen codec. For the fixed width codec, the number of elements in the sequence should match the number of fields originally specified when opening the file. For the null codec, if the sequence contains more than one element, each field will be written out using a separator defined in the IAF configuration file. This separator can be blank, in which case each element will be written out immediately after the previous one, with a newline after the last element.
The FileLine event is exactly the same as the one received when reading; however, the requestId takes on a more important role. If you specify a positive requestId, your application receives an acknowledgement
When a file is already open for reading, you can write to that file only by appending new data. Of course, you must send an OpenFileForWriting event, and then the File adapter can process FileLine events for writing to that file. You receive a FileError event if the file is open for reading and for writing and you try to write data into the file but not by appending the new data.
Copyright © 2013 Software AG, Darmstadt, Germany and/or Software AG USA Inc., Reston, VA, USA, and/or Terracotta Inc., San Francisco, CA, USA, and/or Software AG (Canada) Inc., Cambridge, Ontario, Canada, and/or, Software AG (UK) Ltd., Derby, United Kingdom, and/or Software A.G. (Israel) Ltd., Or-Yehuda, Israel and/or their licensors.