准备数据
使用 mapreduce 的第一步是为数据集构造数据存储。数据集的数据存储与 map 和 reduce 函数一样,都是 mapreduce 的必要输入,因为 mapreduce 需要利用数据存储来处理数据块中的数据。
mapreduce 可处理大多数数据存储类型。例如,为 airlinesmall.csv 数据集创建一个 TabularTextDatastore 对象。
ds = tabularTextDatastore('airlinesmall.csv','TreatAsMissing','NA')
ds =
TabularTextDatastore with properties:
Files: {
' ...\matlab\toolbox\matlab\demos\airlinesmall.csv'
}
Folders: {
' ...\matlab\toolbox\matlab\demos'
}
FileEncoding: 'UTF-8'
AlternateFileSystemRoots: {}
PreserveVariableNames: false
ReadVariableNames: true
VariableNames: {'Year', 'Month', 'DayofMonth' ... and 26 more}
DatetimeLocale: en_US
Text Format Properties:
NumHeaderLines: 0
Delimiter: ','
RowDelimiter: '\r\n'
TreatAsMissing: 'NA'
MissingValue: NaN
Advanced Text Format Properties:
TextscanFormats: {'%f', '%f', '%f' ... and 26 more}
TextType: 'char'
ExponentCharacters: 'eEdD'
CommentStyle: ''
Whitespace: ' \b\t'
MultipleDelimitersAsOne: false
Properties that control the table returned by preview, read, readall:
SelectedVariableNames: {'Year', 'Month', 'DayofMonth' ... and 26 more}
SelectedFormats: {'%f', '%f', '%f' ... and 26 more}
ReadSize: 20000 rows
OutputType: 'table'
RowTimes: []
Write-specific Properties:
SupportedOutputFormats: ["txt" "csv" "xlsx" "xls" "parquet" "parq"]
DefaultOutputFormat: "txt"
之前描述的多个选项在 mapreduce 的上下文中非常有用。mapreduce 函数对数据存储执行 read,以检索要传递到 map 函数的数据。因此,您可以使用 SelectedVariableNames、SelectedFormats 和 ReadSize 选项来直接配置 mapreduce 传递给 map 函数的数据块大小和数据类型。
例如,要选择 Distance(总航程)变量作为唯一要关注的变量,请指定 SelectedVariableNames。
ds.SelectedVariableNames = 'Distance';
现在,不论何时对 ds 执行 read、readall 或 preview 函数,它们都仅返回 Distance 变量的信息。要确认这一点,您可以预览数据存储中的前几行数据。这样,您可以检查 mapreduce 函数将要传递给 map 函数的数据的格式。
preview(ds)
ans =
8×1 table
Distance
________
308
296
480
296
373
308
447
954
要查看 mapreduce 将要传递给 map 函数的确切数据,请使用 read。
有关可用选项的更多信息和完整摘要,请参阅数据存储。