我正在尝试使用尾递归解决问题。用例是:
我有文件夹列表,每个文件夹都有文件列表,每个文件都有多个记录。我想对记录进行一些转换,并将它们批量写入运动学。
val listOfFolders = Folder1(File1(RF11, RF12, RF13), File2(RF21,RF22))
我想在运动学中一次写两个记录。到目前为止,我已经尝试过:
listOfFolders.map { folder =>
val files = fetchAllFilesFromFolder(folder)
if (files.nonEmpty) {
sendBatch(files, Seq.empty[(ByteBuffer, String)], 2)
} else {
logger.info(s"No files are present in folder")
}
}
@scala.annotation.tailrec
def sendBatch(
files: Seq[Files],
buffer: Seq[(ByteBuffer, String)],
numberOfRecordsToSend: Int
): Unit =
files match {
case Nil => {
if (buffer.nonEmpty) {
sendToKinesis(streamName, buffer) map { putDataResult =>
val putDataList = putDataResult.getRecords.asScala.toList
logger.info(
s"Successfully Sent"
)
}
} else {
logger.info(s"Successfully sent")
}
}
case head :: tail => {
val fileData = readFileData()
val byteData: Seq[(ByteBuffer, String)] = transformDataAndConvertToByteBuffer(fileData)
val currentBatch = buffer ++ byteData
if (currentBatch.size >= numberOfRecordsToSend) {
sendToKinesis(streamName, buffer) map { putRecordRes =>
val putDataList = putRecordRes.getRecords.asScala.toList
logger.info(
s"Sent successfully"
)
}
sendBatch(tail, Seq.empty[(ByteBuffer, String)], 2)
} else {
sendBatch(tail, currentBatch, 2)
}
}
}
sendToKinesis使用KCL putRecords。
上面的代码存在的问题是:
从一个文件读取所有数据。因此,如果文件有5条记录将发送5条记录可以运动,但批大小为2。
无法从地图调用尾部递归方法。
如果-如果file1有3条记录,也应该发送2一起记录RF11,RF12,然后一起记录RF13,RF21,最后RF22。
我不想在我的代码中使用任何var。可以使用尾部记录仪解决吗?
您有两个子问题
@scala.annotation.tailrec
def sendBatch(file: Option[File], buffer: Seq[(ByteBuffer, String)], numbersOfRecrodsToSend: Int): Seq[(ByteBuffer, String)] = {
if (buffer.length < numbersOfRecrodsToSend) {
// case 1: too few records to be sent
file match {
// case 1.1: file was not yet read
case Some(f) => sendBatch(None, buffer ++ getByteData(f), numbersOfRecrodsToSend)
// case 1.2: too few records, file was already read, return leftover records
case None => buffer
}
} else {
// case 2: we can send numbersOfRecrodsToSend to Kinesis
val (toSend, newBuffer) = buffer.splitAt(numbersOfRecrodsToSend)
sendToKinesis(streamName, toSend)
sendBatch(file, newBuffer, numbersOfRecrodsToSend)
}
}
// start with empty list of files to send and for each folder
// add it's files to the buffer and send as many records as you can
// the leftover is going to be passed to next iteration for both files and directories
val partial = listOfFolders.foldLeft(Seq.empty[(ByteBuffer, String)]) { case (acc, folder) =>
fetchAllFilesFromFolder(folder).foldLeft(acc) { case (acc2, file) =>
sendBatch(Some(file), acc2, numbersOfRecrodsToSend)
}
}
// if any records have left - send them too
if (partial.nonEmpty) {
sendToKinesis(streamName, partial)
}
希望您有主意。