0

My CSV looks like this

id weight
1 38
4 98
4 66
6 89

I would like to produce my output into list of these maps:

Map("id"->1,"weight"->38) 
Map("id->4,weight"->98)

I am using gatling pebble template, I tried to read the records from cCSV like this:

val records=csv(my.csv).readRecords() 

and then from these records I am unable to convert it into this desired map.

The pebble template which takes map in this format:

Iterator[Map[String],List[Map[String][String]]] 

I am using id and weight in my template.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 4
    So what did you try until now? – Filip Jul 02 '21 at 15:38
  • Also, you mention Gatling, do you want a way to read a CSV on Gatling or a plain Scala way? – Gaël J Jul 02 '21 at 17:51
  • Along with the above comments, could you also expand on the expected data structure and if it's correct - you are trying to create multiple maps (1 for each row and keys as id and weight. Shouldn't the more logical DS be Map(id->weight) or List(id:{id},weight:{weight}) – Jahnavi Paliwal Jul 02 '21 at 18:20
  • @Filip : yes i am using gatling pebble template, i tried to read the records from csv like this: val records=csv(my.csv).readRecords() and then from this records i am unable to convert it into this desired map. – surbhi khare Jul 02 '21 at 21:55
  • @JahnaviPaliwal: Actually i am using pebble template which takes map in this format : Iterator[Map[String],List[Map[String][String]]] , i am using id and weight in my template. – surbhi khare Jul 02 '21 at 22:04

1 Answers1

0

Assuming that your CSV file looks like this and resides in data.csv:

id,weight
1,38
4,98
4,66
6,89

A minimal parser could look as follows:

object Main extends App {
    val separator = ","
    val lines = io.Source.fromFile("data.csv").getLines()
    val headers = lines.next().split(separator)

    val result = for {
        line <- lines
        values = line.split(separator)
        mapping = headers.zip(values).toMap
    } yield mapping

    println(result.toList)
}

Output:

List(Map(id -> 1, weight -> 38), Map(id -> 4, weight -> 98), Map(id -> 4, weight -> 66), Map(id -> 6, weight -> 89))

In most cases, using a library is probably a better idea tough.

Szymon Jednac
  • 2,969
  • 1
  • 29
  • 43
  • Thanks alot for your answer , this works , however does not fit with pebble template , i was trying to create that specific map. val feeder=iterator[Map[String],List[Map[String][String]]] - Do you have any idea how can i transform the above map to this one. – surbhi khare Jul 02 '21 at 22:35
  • @surbhikhare It's hard to help without a reference example, that shows what you're trying to do exactly - you should create another question, that reflects the current problem in the Pebble context (i.e. assuming that this part of the issue is solved; i.e. reading the CSV config). BTW, marking this one as "accepted" is probably a good idea. – Szymon Jednac Jul 02 '21 at 22:45
  • Sure i will accept this , for full reference i am using this: https://stackoverflow.com/questions/68182150/how-to-create-dynamic-json-using-for-loop-in-scala-to-pass-in-gatling-post-reque/68190775?noredirect=1#comment120577464_68190775 – surbhi khare Jul 02 '21 at 23:12
  • i think i missed to explain a imp point : val mapValuesFeeder = Iterator.continually(Map("mapValues" -> List( Map("id" -> "1", "weight" -> "10"), Map("id" -> "2", "weight" -> "20"), ))) , i should have asked : how to populate these id's and weights from csv – surbhi khare Jul 02 '21 at 23:29