setvartype
此函数需要 Streaming Data Framework for MATLAB® Production Server™。
说明
opts = setvartype(
将对象 opts
,selection
,type
)opts
中的所有变量的数据类型设置为 type
指定的数据类型。
如果
opts
是ImportOptions
对象,那么setvartype
将返回ImportOptions
对象。如果
opts
是ExportOptions
对象,那么setvartype
将返回ExportOptions
对象。
opts = setvartype(
将 opts
,selection
,type
)selection
指定的变量的数据类型设置为对象 type
中 opts
指定的数据类型。
示例
假设您有一个在网络地址 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"
将 vMotor
和 wMotor
变量的数据类型更改为 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 服务器,该服务器包含主题 Triangles
和 numericTriangles
。
创建一个连接到 KafkaStream
主题的 Triangles
对象。
inKS = kafkaStream("kafka.host.com",9092,"Triangles");
将 Triangles
主题中的事件读入时间表。通过查看第一行来预览数据。a
、b
和 c
三角形边长存储为字符串。
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);
输入参数
事件流选项,指定为 ImportOptions
或 ExportOptions
对象。opts
对象包含控制数据导入/导出过程的属性,例如变量名称和类型。
选定的变量,指定为字符向量、字符串标量、字符向量元胞数组或字符串数组。
变量名必须是 opts
对象识别的名称的子集。
示例: 'FanID'
示例: "FanID"
示例: {'FanID','vMotor'}
示例: ["FanID" "vMotor"]
数据类型: char
| string
| cell
变量的新数据类型,指定为包含有效 MATLAB 数据类型名称的字符串标量。变量 type
指定导入或导出变量时使用的数据类型。使用此表中列出的数据类型之一。
数据 | MATLAB 数据类型 |
---|---|
文本 | |
数值 | 未定义的浮点数
相同的转换过程适用于所有整数数据类型: |
逻辑型 |
|
示例: io = setvartype(io,"vMotor","int32")
将事件流变量 vMotor
的数据类型更改为 int32
。
数据类型: string
版本历史记录
在 R2022b 中推出
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)