Writing to Sockets

<< Click to Display Table of Contents >>

Navigation:  Integrating SQLstream Blaze with Other Systems > Writing Data Out of s-Server > Writing to Other Destinations >

Writing to Sockets

Previous pageReturn to chapter overviewNext page

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"

("recNo" INTEGER,

"ts" TIMESTAMP,

"accountNumber" INTEGER,

"loginSuccessful" BOOLEAN,

"sourceIP" VARCHAR(32),

"destIP" VARCHAR(32),

"customerId" INTEGER)

--Columns for the new stream

SERVER "NetWriterServer"

OPTIONS

(formatter 'CSV',

server_port '5601',

character_encoding 'UTF-8',

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

FORMATTER=CSV

SERVER_PORT=5601

CHARACTER_ENCODING=UTF-8

SKIP_HEADER=FALSE

# Schema, name, and parameter signature of origin stream

SCHEMA_NAME='SOCKETWRITER'

TABLE_NAME=NetWriterStream

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.

Column

Meaning

SOURCE_HOST

Input only. Remote host from which this row was read.

SOURCE_PORT

Input only. Remote port from which this row was read.

Format Type Options

Other options are specific to format type.