How to return a list of tuples in a Custom Java Function using the StreamBase Client API

How to return a list of tuples in a Custom Java Function using the StreamBase Client API

book

Article ID: KB0076355

calendar_today

Updated On:

Products Versions
TIBCO Streaming -

Description

What code is needed to return a list of tuples in a Custom Java Function using the StreamBase Client API?

Issue/Introduction

How to return a list of tuples in a Custom Java Function using the StreamBase Client API

Resolution

To define a schema with a list of tuples in a Custom Java Function, do the following: 

1. Create a member variable to hold the list's tuple schema (example):
public class TheClass {
private static Schema OutputSchema = null;
2. Create a custom function resolver that defines the schema (example)
public static CompleteDataType CombineWithListCustomFunctionResolver0(
    CompleteDataType arg0, 
    CompleteDataType arg1, 
    CompleteDataType arg2) {
  ArrayList<Schema.Field> Fields = new ArrayList<Schema.Field>();
  Fields.add(Schema.createListField("theList",CompleteDataType.forString()));
  Fields.add(Schema.createField(DataType.DOUBLE, "theDouble"));
  Fields.add(Schema.createField(DataType.INT, "theInt"));
  OutputSchema = new Schema(null,Fields);
  return CompleteDataType.forList(CompleteDataType.forTuple(OutputSchema));
}
Note that this doesn't show the input value typechecking logic.

3. Create a function that builds the list of tuples (example):  
@CustomFunctionResolver("CombineWithListCustomFunctionResolver0")
public static List<?> CombineWithList(List<String> arg0, Double arg1, Integer arg2) {
    Tuple dataTuple = OutputSchema.createTuple(); // create the tuple
    ArrayList<Tuple> dataList = new ArrayList<Tuple>(); // create the list to be emitted
    try { // load the tuple
        dataTuple.setList(0, arg0);
        dataTuple.setDouble(1, arg1);
        dataTuple.setInt(2, arg2);
    } catch (NullValueException e) {
        e.printStackTrace();
    } catch (TupleException e) {
        e.printStackTrace();
    }
    dataList.add(dataTuple); // add the tuple to the list, repeat as necessary with different values
    return dataList;
}