Scala中的大块拆分字符串

发布于 2025-02-09 18:52:53 字数 1064 浏览 0 评论 0原文

我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文