Scala中的大块拆分字符串
我是Scala的新手,我很难通过字节拆分字符串。
假设我有一个一定长度的字符串对象,问题是我需要将此字符串分开并将其发送给1048576(最大)字节的消费者。
到目前为止,我已经尝试了以下内容:
val recordDelimiter: String = "\n"
val recordsBatch: String = dataArray.mkString(recordDelimiter)
val recordsCount = dataArray.length
// Send data to stream
recordsBatch
.grouped(1048576)
.foreach(
chunk =>
try {
val putRecordRequest = PutRecordRequest.builder
.streamName(streamName)
.data(AWSUtils.toSdkBytes(chunk))
.partitionKey("partitionKey-1")
.build
val response = kinesis.putRecord(putRecordRequest)
logger.info(s"Records sent to $streamName stream: $recordsCount")
response.sequenceNumber
} catch {
case exception: KinesisException =>
logger.error("Error occurred while sending data to Kinesis", exception)
"ERROR"
} finally {
kinesis.close()
}
)
但是,每次尝试执行代码
I'm a newbie to Scala and I'm having trouble trying to split a String by bytes.
Let's say I have a String object of a certain length, the problem is that I need to split and send this string to a consumer in chunks of 1048576 (max) bytes each.
So far, I have tried the following:
val recordDelimiter: String = "\n"
val recordsBatch: String = dataArray.mkString(recordDelimiter)
val recordsCount = dataArray.length
// Send data to stream
recordsBatch
.grouped(1048576)
.foreach(
chunk =>
try {
val putRecordRequest = PutRecordRequest.builder
.streamName(streamName)
.data(AWSUtils.toSdkBytes(chunk))
.partitionKey("partitionKey-1")
.build
val response = kinesis.putRecord(putRecordRequest)
logger.info(s"Records sent to $streamName stream: $recordsCount")
response.sequenceNumber
} catch {
case exception: KinesisException =>
logger.error("Error occurred while sending data to Kinesis", exception)
"ERROR"
} finally {
kinesis.close()
}
)
However, I'm obtaining "java.lang.NullPointerException" every time I try to execute the code
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论