我有一个文件,里面有这样的文本
{"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条答案
按热度按时间8ljdwjyq1#
将您的文件转换为一列csv feeder并使用标准的gatling方式:输入一条记录,发送您的请求,并根据需要重复多次。
yfwxisqw2#
对于这个目标,您唯一真正需要的就是打开文件并逐行遍历它@斯蒂芬的评论有点生硬,但他的意思是:
或者,如果您不想编辑文件的内容,可以使用更简单的方法:
我希望这有帮助,干杯。