Kafka更新offset,scala反射调用进行测试

来源:互联网 发布:得力33113考勤软件 编辑:程序博客网 时间:2024/06/13 21:43
实现的类为KafkaCluster,自定义实现必须放在包名为
org.apache.spark.streaming.kafka

目录下,不然集群上会报错(当时打算采用反射来实现),打印参数发现不对,反射测试代码记录下

  val cls = Class.forName("org.apache.spark.streaming.kafka.KafkaCluster")  val clsLeaderOffset = Class.forName("org.apache.spark.streaming.kafka.KafkaCluster")  //clsLeaderOffset.asInstanceOf[LeaderOffset]  val constructors = cls.getConstructors  constructors.foreach(constructor => {println("**********Class: " + constructor)})  //val methods = cls.getDeclaredMethods()  //methods.foreach(method => {println("**********Methods: " + method)})  val kc = constructors(0).newInstance(kafkaParams)  val getPartitions = cls.getDeclaredMethod("getPartitions", classOf[Set[String]])  val getConsumerOffsets = cls.getDeclaredMethod("getConsumerOffsets", classOf[String], classOf[Set[TopicAndPartition]])  val setConsumerOffsets = cls.getDeclaredMethod("setConsumerOffsets", classOf[String], classOf[Map[TopicAndPartition, Long]])  val getEarliestLeaderOffsets = cls.getDeclaredMethod("getEarliestLeaderOffsets", classOf[Set[TopicAndPartition]])  val getLatestLeaderOffsets = cls.getDeclaredMethod("getLatestLeaderOffsets", classOf[Set[TopicAndPartition]])  getPartitions.setAccessible(true)  getConsumerOffsets.setAccessible(true)  setConsumerOffsets.setAccessible(true)  getEarliestLeaderOffsets.setAccessible(true)  getLatestLeaderOffsets.setAccessible(true)



原创粉丝点击