Main Content

writetimetable

R2026b

Write timetable to event stream

Since R2022b

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

    Description

    writetimetable(stream,tt) writes the timetable tt to the end of the event stream stream.

    writetimetable converts rows of a timetable into events in an event stream, where:

    • The column names in the timetable become variable names in the event body.

    • The values in each event row become the values of those variables.

    • The row timestamp becomes the event timestamp.

    You can append data to a stream but cannot modify data that is already written to a stream.

    example

    writetimetable(stream,tt,opts) additionally specifies an ExportOptions input.

    You can use detectExportOptions to specify a value for opts.

    example

    writetimetable(ks,tt,MissingTopic=action) specifies whether to create a topic when writing a timetable to an event stream hosted by Kafka® or fail the write operation when the topic is missing. If the Kafka cluster that you are writing to is configured to auto-create topics, specifying action has no effect.

    example

    Examples

    collapse all

    Load air quality data and weather measurements into a timetable.

    load indoors

    Create an InMemoryStream object to connect to an event stream hosted by MATLAB.

    i = inMemoryStream;

    Write the timetable to the event stream.

    writetimetable(i,indoors)

    Preview the data in the event stream.

    preview(i)
    ans =
    
      8×2 timetable
    
               Time            Humidity    AirQuality
        ___________________    ________    __________
    
        2015-11-15 00:00:24       36           80    
        2015-11-15 01:13:35       36           80    
        2015-11-15 02:26:47       37           79    
        2015-11-15 03:39:59       37           82    
        2015-11-15 04:53:11       36           80    
        2015-11-15 06:06:23       36           80    
        2015-11-15 07:19:35       36           80    
        2015-11-15 08:32:47       37           80    

    Load air quality data and weather measurements into a timetable.

    load indoors

    Assume that you have a Kafka host running at network address kafka.host.com:9092. Create a KafkaStream object that processes 10 stream events at a time.

    ks = kafkaStream("kafka.host.com",9092,"IndoorTemp",Rows=10);

    Create the IndoorTemp topic and write the timetable to it.

    writetimetable(ks,indoors)

    Read the first 10 events from the stream.

    tt1 = readtimetable(ks)
    tt1 =
    
      10×3 timetable
    
             timestamp          Humidity    AirQuality    key
        ____________________    ________    __________    ___
    
        15-Nov-2015 00:00:24       36           80        "" 
        15-Nov-2015 01:13:35       36           80        "" 
        15-Nov-2015 02:26:47       37           79        "" 
        15-Nov-2015 03:39:59       37           82        "" 
        15-Nov-2015 04:53:11       36           80        "" 
        15-Nov-2015 06:06:23       36           80        "" 
        15-Nov-2015 07:19:35       36           80        "" 
        15-Nov-2015 08:32:47       37           80        "" 
        15-Nov-2015 09:45:59       37           79        "" 
        15-Nov-2015 10:59:11       36           80        "" 

    Read the next 10 events from the stream.

    tt2 = readtimetable(ks)
    tt2 =
    
      10×3 timetable
    
             timestamp          Humidity    AirQuality    key
        ____________________    ________    __________    ___
    
        16-Nov-2015 00:24:22       36           81        "" 
        16-Nov-2015 01:37:34       37           80        "" 
        16-Nov-2015 02:50:46       36           79        "" 
        16-Nov-2015 04:03:58       37           80        "" 
        16-Nov-2015 05:17:09       37           81        "" 
        16-Nov-2015 06:30:21       36           79        "" 
        16-Nov-2015 07:43:33       37           79        "" 
        16-Nov-2015 08:56:45       37           79        "" 
        16-Nov-2015 10:09:57       37           85        "" 
        16-Nov-2015 11:23:09       37           80        ""  

    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")
    
    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: "None"
              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,opts);
    

    Input Arguments

    collapse all

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

    Input timetable.

    Object connected to a Kafka stream topic, specified as a KafkaStream object.

    Action to take if the topic to write the timetable to does not exist, specified as one of the following values:

    • "create" — Creates new topic, if you have the required permissions on the Kafka host.

    • "fail" — Does not create a new topic and the write operation fails.

    Data Types: char | string

    Export options, specified as an ExportOptions object. You can use detectExportOptions to generate this.

    Version History

    Introduced in R2022b