Main Content

detectExportOptions

Create export options based on event stream content

Since R2022b

    This function requires Streaming Data Framework for MATLAB® Production Server™.

    Description

    opts = detectExportOptions(stream,row) detects export options from an event stream and returns them in opts. These options specify the rules for transforming MATLAB variables into streaming data. To determine which streaming data type best matches the type of each column variable, detectExportOptions examines a row of timetable data and includes the type information in opts.

    To get and set the types of the variables as they are exported from MATLAB to the stream, use getvartype and setvartype.

    To export data from MATLAB to a stream, use writetimetable.

    example

    opts = detectExportOptions(stream,row,Format=format) optionally sets the output stream format.

    Examples

    collapse all

    Assume that you have a Kafka® server running at the network address kafka.host.com:9092 that has a topic Triangles.

    Create a KafkaStream object connected to the Triangles topic.

    inKS = kafkaStream("kafka.host.com",9092,"Triangles");

    Read events from the Triangles topic into a timetable. Preview the data by viewing the first row. The a, b, and c triangle side lengths are stored as strings.

    tt = readtimetable(inKS);
    row = tt(1,:)
    row =
    
      1×3 timetable
    
         timestamp      a       b       c  
        ___________    ____    ____    ____
    
        03-Sep-2022    "15"    "31"    "36"

    Use detectExportOptions to generate an ExportOptions object from the Kafka stream object. The function obtains the types used to export the variables from the first row of the timetable.

    opts = detectExportOptions(inKS,row);

    Use getvartype to confirm that the side length variables are currently exported to the stream as strings.

    type = getvartype(opts,["a" "b" "c"]);
    
    type = 
    
      1×3 string array
    
        "string"    "string"    "string"

    Update the export options so that the side lengths are exported as double values. Confirm the updated options by using getvartype.

    opts = setvartype(opts,["a","b","c"],"double");
    
    [name,type] = getvartype(opts);
    fprintf("%s: %s\n", [name; type])
    a: double
    b: double
    c: double

    Connect to the stream to export data to numericTriangles.

    outKS = kafkaStream("kafka.host.com",9092,"numericTriangles", ...
        ExportOptions=opts)
    
    outKS = 
    
      KafkaStream with properties:
    
                      Topic: "numericTriangles"
                      Group: "85c42e39-695d-467a-86f0-f0095792e7de"
                      Order: EventTime
                       Host: "kafka.host.com"
                       Port: 9092
          ConnectionTimeout: 30
             RequestTimeout: 61
              ImportOptions: "None"
              ExportOptions: "Source: string"
              PublishSchema: "true"
                 WindowSize: 50
                KeyVariable: "key"
                KeyEncoding: "utf16"
                    KeyType: "text"
               KeyByteOrder: "BigEndian"
               BodyEncoding: "utf8"
                 BodyFormat: "JSON"
                  ReadLimit: "Size"
        TimestampResolution: "Milliseconds"
    

    Export the timetable to the new stream. The triangle side lengths in this stream are of type double.

    writetimetable(outKS,tt);
    

    Input Arguments

    collapse all

    Object connected to an event stream, specified as a KafkaStream or TestStream object.

    Row of MATLAB timetable data. row must be an example of the data that you intend to write to the stream. detectExportOptions uses the columns of this row to set the variable names and types in the export options.

    Example: tt(1,:) extracts the first row of data from timetable tt.

    Format of the output stream, specified as one of these options:

    • "Event" — Native, default even stream format

    • "InfluxDB" — Format specifically used by InfluxDB

    Data Types: char | string

    Output Arguments

    collapse all

    Event stream export options, returned as an ExportOptions object.

    Version History

    Introduced in R2022b