1

I have zero encounter with Scala. I'm returned an scala object by a library(kafka) of the following type:

kafka.coordinator.GroupMetadataManager$$anon$2@2b9ee60f

I tried to pass this to Scala to get some meaningful information. i.e. to access the data members.

package metricsReporter;
import kafka.coordinator.GroupMetadataManager;
class processMetrics {
  def hello() { println("Hello (class)") } // [1]
  def printGM(gmObject: AnyRef ) {
       println(gmObject)
  }

}

I'm calling the above function from java like this

new processMetrics().printGM(metric);

This function is also ouputing the same text. I also get other objects like anon$2, anon$3 (which should have data about different kafka partitions) of the same type.

Ansel Zandegran
  • 636
  • 8
  • 18
  • Have a look at pattern matching, to access the object in it's actual type: http://docs.scala-lang.org/tutorials/tour/pattern-matching.html – thwiegan Jun 26 '17 at 12:02

2 Answers2

1

What you see is just the output default toString implementation.

Maybe you could use reflection to get values of fields?

def printGM(gmObject: AnyRef ) {
    val result = gmObject.getClass().getDeclaredFields().map { field => 
      field.setAccessible(true)
      s"${field.getName()}:${field.get(gmObject).toString()}"
    }.deep.mkString("[", ", ", "]")

    println(result)
}
Krzysztof Atłasik
  • 21,985
  • 6
  • 54
  • 76
  • Exception in thread "reporterBG" java.lang.NoSuchMethodError: scala.Predef$.refArrayOps([Ljava/lang/Object;)Lscala/collection/mutable/ArrayOps; – Ansel Zandegran Jun 26 '17 at 14:39
  • What version of Scala are you using? You can try replacing `deep.mkString` with `deepMkString`. – Krzysztof Atłasik Jun 26 '17 at 15:07
  • Thanks @Randall replacing that didn't even compile. (value deepMkString is not a member of Array[String]) How can I check the version? I'm using scala only to get values from these metric objects thrown at me by Kafka. I'm using maven and this (https://pastebin.com/7CLDkhp4) is my pom.xml in reference to http://docs.scala-lang.org/tutorials/scala-with-maven.html – Ansel Zandegran Jun 26 '17 at 15:28
1

You don't need to do it in scala. What you've written in your question is equivalent to System.out.prinln(metric). What the other answer suggests you to do is similar to:

for (Field field: metric.getClass().getDeclaredFields()) {
   field.setAccessible(true);
   System.out.println(field.getName() + ": " + field.get(metric));
}

There is nothing magical going on here.

It's another issue, that neither of these approaches seem to make any sense. You shouldn't need to use reflection to access data returned to you by public APIs. It seems that there definitely should be a better way to do what you are trying to do here ... if only we knew what the latter really is ... Edit: Syntax

Ansel Zandegran
  • 636
  • 8
  • 18
Dima
  • 39,570
  • 6
  • 44
  • 70
  • Great! Thanks! I'm getting some values now. – Ansel Zandegran Jun 28 '17 at 08:45
  • This is not part of a public API. Kafka uses JMX as a standard to expose metrics and our environment prevents us from using JMX so we are left with building a metrics reporting service that uses kafka metrics reporters similar to this https://github.com/krux/kafka-metrics-reporter NO DOCUMENTATION at all – Ansel Zandegran Jun 28 '17 at 08:51