使用Gatling在文件中逐行循环,并一次向Kafka发送一条消息

时间:2016-01-05 21:22:02

标签: apache-kafka gatling

我的文件包含类似这样的文字

{"content_type":"Twitter","id":"77f985b0-a30a-11e5-8791-80000bc51f65","source_id":"676656486307639298","date":"2015-12-15T06:54:12.000Z","text":"RT @kokodeikku_bot: ?????: ??,}
{"content_type":"Twitter","id":"7837a020-a30a-11e5-8791-80000bc51f65","source_id":"676656494700568576",}
{"content_type":"Twitter","id":"7838d8a0-a30a-11e5-8791-80000bc51f65","source_id":"676656507266703360",}

我无法一次读取每一行作为场景中的Kafka主题的字符串,因为我无法迭代Gatling中的场景。

这是我的代码

class KafkaSimulation extends Simulation {

   val line = Source.fromFile(<passing locn of file>)("UTF-8").getLines.mkString("\n") // one way by reading source from file

   val br = new BufferedReader(new FileReader("<passing locn of file>"))
   var line:String = ""

   while ({ line = br.readLine() ; line != null } ) {
       //In this while loop i can print line by line but i cant use while loop within scenario below  
        println(listOfLines.mkString("\n"))      
        }

  val kafkaConf = kafka

    // Kafka topic name
    .topic("test")
    // Kafka producer configs
    .properties(
      Map(
        ProducerConfig.ACKS_CONFIG -> "1",
        // list of Kafka broker hostname and port pairs
          ProducerConfig.BOOTSTRAP_SERVERS_CONFIG -> "localhost:9092",
       // Required since Apache Kafka 0.8.2.0
        ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG ->
          "org.apache.kafka.common.serialization.ByteArraySerializer",
        ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG ->
          "org.apache.kafka.common.serialization.ByteArraySerializer"))

     val scn = scenario("Kafka Test")
    .exec(kafka("request") 
       // message to send 
       .send(line.toString())) //Here if i put line.toString(), it doesnt read line by line instead it will post entire 3 lines as one message

  setUp(
      scn.inject(constantUsersPerSec(10) during (1 seconds)))
     .protocols(kafkaConf)

}

有关如何迭代文件并在场景中逐行读取的任何提示?

2 个答案:

答案 0 :(得分:1)

将您的文件转换为单列CSV Feedder并使用标准的Gatling方式:提供记录,发送您的请求,并根据需要重复。

答案 1 :(得分:0)

对于这个目标,你唯一真正需要的是打开文件并逐行迭代它。 @stephane评论有点粗糙,但他的意思是:

Source
  .fromFile("files/yourtargetfile.txt")
  .getLines
  .map { line =>
    //do your stuff  
  }.foreach(println)

或者,如果您不想编辑文件的内容,则更简单:

Source
  .fromFile("files/ChargeNames")
  .getLines
  .foreach { line =>
    //do your stuff 
  }

我希望这有帮助, 欢呼声。