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.

1

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Great! Thanks! I'm getting some values now.
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 github.com/krux/kafka-metrics-reporter NO DOCUMENTATION at all
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)
}

3 Comments

Exception in thread "reporterBG" java.lang.NoSuchMethodError: scala.Predef$.refArrayOps([Ljava/lang/Object;)Lscala/collection/mutable/ArrayOps;
What version of Scala are you using? You can try replacing deep.mkString with deepMkString.
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 (pastebin.com/7CLDkhp4) is my pom.xml in reference to docs.scala-lang.org/tutorials/scala-with-maven.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.