简体   繁体   English

Kafka Scala:如何将 val 移动到 try catch 块中

[英]Kafka Scala: How to move val into try catch block

I'd like to move:我想搬家:

val kafkaPartitionOffset = kafkaConsumer.endOffsets(consumedPartitions.asJava)

into a try catch block like so:像这样进入try catch块:

val kafkaPartitionOffset : SomeClass = 
            try {
               kafkaConsumer.endOffsets(consumedPartitions.asJava)
            } catch {
              case e: Exception => {
                log.error(s"${consumerGroupId} Could not get Kafka offset", e)
                None
              }
            }

But I'm having trouble on what the SomeClass should be.但是我在SomeClass应该是什么方面遇到了麻烦。 I've tried Map[TopicPartition, Long] but it says Type mismatch .我试过Map[TopicPartition, Long]但它说Type mismatch Any help is appreciated, thank you!任何帮助表示赞赏,谢谢!

Update: I've also tried Any but I'm unable to do a kafkaPartitionOffset.get(topicPartition) below ( get is highlighted red with error message cannot resolve symbol get :更新:我也尝试过Any但我无法在下面执行kafkaPartitionOffset.get(topicPartition)get以红色突出显示,错误消息cannot resolve symbol get

        for((topicPartition,OffsetAndMetadata) <- mapTopicPartitionOffset){
          
          val bbCurrentOffset =  OffsetAndMetadata.get(topicPartition)

          // latest offset
          val partitionLatestOffset = kafkaPartitionOffset.get(topicPartition)

          // Log for a particular partition
          val delta = partitionLatestOffset - bbCurrentOffset

          topicOffsetList += delta.abs

        }

Take a look at this:看看这个:

val x = try { 
  throw new RuntimeException("runtime ex")
  "some string"
} catch { case _: RuntimeException => 2 }

The compiler needs to know the type of x before runtime, since x can be used somewhere else in your code, right?编译器需要在运行前知道x的类型,因为x可以在代码的其他地方使用,对吧? So the compiler says:所以编译器说:

"Hmm, what is a type that this literal "some string" is of that type, and also, literal 2 is of that type?" “嗯,这个字面量“某个字符串”属于那种类型,而且字面量 2 属于那种类型是什么类型?”

So it looks for the lowest super-type of both String and Int, which is Any in this case!所以它会寻找 String 和 Int 的最低超类型,在这种情况下是Any so val x: Any = ... .所以val x: Any = ... Now I'm not aware of what this expression kafkaConsumer.endOffsets(...) returns, in case it returns Option[T] , then SomeClass would also be Option[T] since you're returning None in the catch block, and if not, do not use None there just because nothing else would fit, there are better approaches to exception handling.现在我不知道这个表达式kafkaConsumer.endOffsets(...)返回什么,如果它返回Option[T] ,那么 SomeClass 也将是Option[T]因为你在 catch 块中返回 None ,并且如果没有,请不要在那里使用 None ,因为没有其他东西适合,有更好的异常处理方法。
But anyways, Scala provides some utility types to avoid this kind of try catch as much as possible, I would recommend you to use Try in this case.但无论如何,Scala 提供了一些实用程序类型来尽可能避免这种 try catch,我建议你在这种情况下使用Try

val kafkaPartitionOffset: Try[Whatever-endOffsets-returns] = 
  Try(kafkaConsumer.endOffsets(consumedPartitions.asJava))

By the way, the title of the question doesn't match the actual question, please consider changing the title :)顺便说一句,问题的标题与实际问题不匹配,请考虑更改标题:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM