Example: Creating and Using Modules

Use basic concepts of modularity to create a module that processes raw stock trade information and outputs a list of trades with a price exceeding 1.00.

  1. Create an import file to group your schemas and allow for reuse throughout the project.
    In this example, the import file is called schemas.ccl and contains:
    CREATE SCHEMA TradesSchema (
    	Id integer, 
    	TradeTime date,
    	Venue string,  
    	Symbol string, 
    	Price float, 
    	Shares integer
    );
    
    Note: You can define schemas directly inside a module or project; however, this example uses an import file to decrease code duplication and increase maintainability of the CCL.
  2. In the project, create a module using the CREATE MODULE statement, and import the import file (schemas.ccl) using the IMPORT statement.
    CREATE MODULE FilterByPrice IN TradeData OUT FilteredTradeData
    BEGIN 
    	IMPORT 'schemas.ccl';
    
    	CREATE INPUT STREAM TradeData SCHEMA TradesSchema;
    	CREATE OUTPUT STREAM FilteredTradeData SCHEMA TradesSchema 
    	AS SELECT * FROM TradeData WHERE TradeData.Price > 1.00;
    END;
    
    The module's input stream, TradeData, takes in a raw feed from the stock market, and its output stream, FilteredTradeData, provides filtered results. Using the IMPORT statement inside the module allows you to use all of the schemas grouped in the schemas.ccl file in the module streams.
  3. Load the module into your main project using the LOAD MODULE statement.
    This example also shows how to connect the module to a stock market stream:
    IMPORT 'schemas.ccl';
    
    CREATE INPUT STREAM NYSEData SCHEMA TradesSchema;
    
    LOAD MODULE FilterByPrice AS FilterOver1 IN TradeData = NYSEData OUT FilteredTradeData = NYSEPriceOver1Data;
    
    • The first line of the project file imports schemas.ccl, which allows the use of the same schema as the module.
    • The input stream NYSEData represents trade information from the New York Stock Exchange.
    • The LOAD MODULE statement loads the module, FilterByPrice, which is identified by the instance name of FilterOver1.
    • Binding the module's input stream, TradeData, with the input stream NYSEData allows information to flow from the NYSEData stream into the module.
    • The output of the module is exposed to the project (NYSEPriceOver1Data).
    • To access the output of the module, select the information from the NYSEPriceOver1Data stream.