All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.datatorrent.lib.streamquery.AbstractSqlStreamOperator Maven / Gradle / Ivy

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package com.datatorrent.lib.streamquery;

import java.util.ArrayList;
import java.util.HashMap;

import com.datatorrent.api.DefaultInputPort;
import com.datatorrent.api.DefaultOutputPort;
import com.datatorrent.api.annotation.InputPortFieldAnnotation;
import com.datatorrent.api.annotation.OutputPortFieldAnnotation;
import com.datatorrent.common.util.BaseOperator;

/**
 * A base implementation of a BaseOperator that is a sql stream operator.   Subclasses should provide the
   implementation of how to process the tuples.
 * 

* Abstract sql db input operator. *

* @displayName Abstract Sql Stream * @category Stream Manipulators * @tags sql operator * @since 0.3.2 */ public abstract class AbstractSqlStreamOperator extends BaseOperator { public static class InputSchema { public static class ColumnInfo { public String type; public int bindIndex = 0; public boolean isColumnIndex = false; } /** * the name of the input "table" */ public String name; /** * key is the name of the column, and value is the SQL type */ public HashMap columnInfoMap = new HashMap(); public InputSchema() { } public InputSchema(String name) { this.name = name; } public void setColumnInfo(String columnName, String columnType, boolean isColumnIndex) { ColumnInfo t = new ColumnInfo(); t.type = columnType; t.isColumnIndex = isColumnIndex; columnInfoMap.put(columnName, t); } } protected String statement; protected ArrayList inputSchemas = new ArrayList(5); protected transient ArrayList bindings; /** * Input bindings port that takes an arraylist of objects. */ @InputPortFieldAnnotation(optional = true) public final transient DefaultInputPort> bindingsPort = new DefaultInputPort>() { @Override public void process(ArrayList tuple) { bindings = tuple; } }; /** * Input port in1 that takes a hashmap of <string,object>. */ public final transient DefaultInputPort> in1 = new DefaultInputPort>() { @Override public void process(HashMap tuple) { processTuple(0, tuple); } }; /** * Input port in2 that takes a hashmap of <string,object>. */ @InputPortFieldAnnotation(optional = true) public final transient DefaultInputPort> in2 = new DefaultInputPort>() { @Override public void process(HashMap tuple) { processTuple(1, tuple); } }; /** * Input port in3 that takes a hashmap of <string,object>. */ @InputPortFieldAnnotation(optional = true) public final transient DefaultInputPort> in3 = new DefaultInputPort>() { @Override public void process(HashMap tuple) { processTuple(2, tuple); } }; /** * Input port in4 that takes a hashmap of <string,object>. */ @InputPortFieldAnnotation(optional = true) public final transient DefaultInputPort> in4 = new DefaultInputPort>() { @Override public void process(HashMap tuple) { processTuple(3, tuple); } }; /** * Input port in5 that takes a hashmap of <string,object>. */ @InputPortFieldAnnotation(optional = true) public final transient DefaultInputPort> in5 = new DefaultInputPort>() { @Override public void process(HashMap tuple) { processTuple(4, tuple); } }; /** * Output result port that emits a hashmap of <string,object>. */ @OutputPortFieldAnnotation(optional = true) public final transient DefaultOutputPort> result = new DefaultOutputPort>(); public void setStatement(String statement) { this.statement = statement; } public String getStatement() { return this.statement; } public void setInputSchema(int inputPortIndex, InputSchema inputSchema) { inputSchemas.add(inputPortIndex, inputSchema); } public abstract void processTuple(int tableNum, HashMap tuple); }