主要内容

本页采用了机器翻译。点击此处可查看最新英文版本。

setvartype

设置用于将变量导入和导出到流的数据类型

自 R2022b 起

    此函数需要 Streaming Data Framework for MATLAB® Production Server™

    说明

    opts = setvartype(opts,selection,type) 将对象 opts 中的所有变量的数据类型设置为 type 指定的数据类型。

    • 如果 optsImportOptions 对象,那么 setvartype 将返回 ImportOptions 对象。

    • 如果 optsExportOptions 对象,那么 setvartype 将返回 ExportOptions 对象。

    opts = setvartype(opts,selection,type)selection 指定的变量的数据类型设置为对象 typeopts 指定的数据类型。

    示例

    示例

    全部折叠

    假设您有一个在网络地址 kafka.host.com:9092 上运行的 Kafka® 服务器,该服务器有一个主题 CoolingFan

    创建一个对象来连接到 Kafka 流处理数据。

    ks = kafkaStream("kafka.host.com",9092,"CoolingFan")

    KafkaStream 对象创建一个导入选项对象。

    io = detectImportOptions(ks)
    io = 
    
      ImportOptions with properties:
    
                VariableNames: ["vMotor"    "wMotor"    "Tmass"    …    ]
                VariableTypes: ["double"    "double"    "double"    …    ]
                  KeyVariable: "key"
        SelectedVariableNames: ["vMotor"    "wMotor"    "Tmass"    …    ]

    检查变量的数据类型。

    disp([io.VariableNames' io.VariableTypes'])
        "vMotor"                  "double"
        "wMotor"                  "double"
        "Tmass"                   "double"
        "ExternalTempAnomaly"     "double"
        "FanDragAnomaly"          "double"
        "VoltageSourceAnomaly"    "double"
        "FanRow"                  "double"
        "FanColumn"               "double"
        "FanID"                   "double"
        "GroupID"                 "double"
        "key"                     "string"

    vMotorwMotor 变量的数据类型更改为 int32

    io = setvartype(io,{"vMotor","wMotor"},"int32")
    io = 
    
      ImportOptions with properties:
    
                VariableNames: ["vMotor"    "wMotor"    "Tmass"    …    ]
                VariableTypes: ["int32"    "int32"    "double"    …    ]
                  KeyVariable: "key"
        SelectedVariableNames: ["vMotor"    "wMotor"    "Tmass"    …    ]

    使用 readtimetable 导入具有更新类型的变量。

    tt = readtimetable(ks,io);

    或者,您可以设置流对象的 ImportOptions 属性并使用 readtimetable

    ks.ImportOptions = io;
    tt = readtimetable(ks);

    假设您有一个在网络地址 kafka.host.com:9092 上运行的 Kafka 服务器,该服务器包含主题 TrianglesnumericTriangles

    创建一个连接到 KafkaStream 主题的 Triangles 对象。

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

    Triangles 主题中的事件读入时间表。通过查看第一行来预览数据。abc 三角形边长存储为字符串。

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

    使用 detectExportOptions 从 Kafka 流对象生成 ExportOptions 对象。该函数获取用于从时间表第一行导出变量的类型。

    opts = detectExportOptions(inKS,row);

    使用 getvartype 确认边长变量当前以字符串的形式导出到流中。

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

    更新导出选项,以便将边长导出为 double 值。使用 getvartype 确认更新的选项。

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

    连接到流以将数据导出到 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"
    

    将时间表导出至新流。此流中的三角形边长为 double 类型。

    writetimetable(outKS,tt);
    

    输入参数

    全部折叠

    事件流选项,指定为 ImportOptionsExportOptions 对象。opts 对象包含控制数据导入/导出过程的属性,例如变量名称和类型。

    选定的变量,指定为字符向量、字符串标量、字符向量元胞数组或字符串数组。

    变量名必须是 opts 对象识别的名称的子集。

    示例: 'FanID'

    示例: "FanID"

    示例: {'FanID','vMotor'}

    示例: ["FanID" "vMotor"]

    数据类型: char | string | cell

    变量的新数据类型,指定为包含有效 MATLAB 数据类型名称的字符串标量。变量 type 指定导入或导出变量时使用的数据类型。使用此表中列出的数据类型之一。

    数据MATLAB 数据类型
    文本

    "char""string"

    数值

    "single""double""int8""int16""int32""int64""uint8""uint16""uint32""uint64"

    未定义的浮点数 NaN-Inf+Inf 仅对 singledouble 数据类型有效。因此,当您将浮点数据类型更改为整数时,导入/导出函数会将未定义的浮点数转换为有效的整数。例如,转换为 "uint8" 数据类型时:

    • NaN 转换为 0

    • -Inf 转换为 intmin("int8")

    • +Inf 转换为 intmax("int8")

    相同的转换过程适用于所有整数数据类型:int8int16int16int32int64uint8uint16uint32uint64

    逻辑型

    "logical"

    示例: io = setvartype(io,"vMotor","int32") 将事件流变量 vMotor 的数据类型更改为 int32

    数据类型: string

    版本历史记录

    在 R2022b 中推出