Writing to Sockets
To write to files to a socket, you use the Extensible Common Data Adapter (ECDA) or ECD Agent.
To write data, you first define a server object with connection information, including the directory and information on file rotation. Once you define this server object, you can write to the file system by referencing it. See the topic CREATE SERVER in the s-Server Streaming SQL Reference Guide for more details.
For adapters, you configure and launch the adapter in SQL, using either server or foreign stream/table options. For agents, you configure such options using a properties file and launch the agent at the command line. Many of the options for the ECD adapter and agent are common to all I/O systems. The CREATE FOREIGN STREAM topic in the Streaming SQL Reference Guide has a complete list of options for the ECD adapter.
Note: Because of the nature of streaming data, you will need to set up a pump in order to move rows continually from an s-Server stream to another stream, file, Kafka topic, RDBMS table or other location. Pumps are INSERT macros that continually pass data from one point in a streaming pipeline to the other. A model for setting up a pump is provided below. See the topic CREATE PUMP in the s-Server Streaming SQL Reference Guide for more details.
Implementing the ECD Adapter and Agent for Sockets
You configure the ECD Adapter and Agent to read or write over sockets using server options and foreign table/stream options.
CREATE OR REPLACE SERVER "NetWriterServer" TYPE 'NET'
FOREIGN DATA WRAPPER ECDA;
Note: ECD Adapter server definitions need to reference the ECD foreign data wrapper. You can do so with the syntax FOREIGN DATA WRAPPER ECDA.
Next, you create a foreign stream object. This object contains connection information for the socket, such as format type, port, character encoding, whether or not to write a header, and the host name. Streams, like most SQL objects (but unlike data wrappers and servers), should be created within a schema. The following code first creates a schema in which to run the rest of the sample code below, then creates a foreign stream named "NetWriterStream."
CREATE OR REPLACE SCHEMA "NetWriterSchema"
SET SCHEMA 'NetWriterSchema';
CREATE OR REPLACE FOREIGN STREAM "NetWriterStream"
--Columns for the new stream
write_header 'FALSE', );
CREATE OR REPLACE SCHEMA "Pumps";
SET SCHEMA '"Pumps"';
CREATE OR REPLACE PUMP "writerPump" STOPPED AS
--We recommend creating pumps as stopped
--then using ALTER PUMP "Pumps"."writerPump" START to start it
INSERT INTO "NetWriterSchema"."NetWriterStream"
SELECT STREAM * FROM "MyStream";
--where "MyStream" is a currently existing stream
To start writing data, use the following code:
ALTER PUMP "Pumps"."writerPump" START;
Implementing the ECD Agent for Sockets
The ECD agent takes similar options, but these options need to be formatted in a properties file along the lines of the following. These properties correspond to those defined for the adapter above.
# Port, format, host
# Schema, name, and parameter signature of origin stream
ROWTYPE=RECORDTYPE(VARCHAR(2040) id, VARCHAR(2040) reported_at, VARCHAR(2040) shift_no, VARCHAR(2040) trip_no, VARCHAR(2040) route_variant_id)
Additional Columns Generated for Sockets
The following columns may be auto generated by the file adapter if you are doing a select, that is, reading from the file.
Input only. Remote host from which this row was read.
Input only. Remote port from which this row was read.
Format Type Options
Other options are specific to format type.