I need help for passing HashMap to queryParam as shown below, as I have so many request with same queryParams and just last queryParam changes with each request.
val asset_sort_by_displays = exec(http("Sort by Displays")
.get("/data/analytics/reports/")
.queryParamMap(GatlingConfig.asset_query_string_map)
.queryParam("""sort""", """displays""")
Where in my config file I have a object GatlingConfig{} where I've defined asset_query_string_map as.
val asset_query_string_map = Map("""report_type""" -> """performance""",
"""start_date""" -> "2014-07-07",
"""end_date""" -> "2014-07-10",
"""metrics""" -> """plays""",
"""metrics""" -> """displays""",
"""metrics""" -> """video_starts""")
But, it throws " value asset_query_string_map is not a member of io.gatling.http.request.builder.HttpRequestBuilder " error.
Please guide me , how can I pass Map value to queryParams?
Where in my config file I have created
I don't get it. This is to be defined in some Scala code and imported/placed in scope.
Then, queryParam is for unique values. You'd have to use queryParamMap instead.
With current snapshot, you'd have:
val asset_query_string_map = Map("""report_type""" -> """performance""",
"""start_date""" -> "2014-07-07",
"""end_date""" -> "2014-07-10",
"""metrics""" -> """plays""",
"""metrics""" -> """displays""",
"""metrics""" -> """video_starts""")
val asset_sort_by_displays = exec(http("Sort by Displays")
.get("/data/analytics/reports/")
.queryParamMap(asset_query_string_map)
.queryParam("""sort""", """displays"""))
Related
I have a dataframe with a column named source_system that has the values contained in the keys of this Map:
val convertSourceSystem = Map (
"HON_MUREX3FXFI" -> "MX3_FXFI",
"MAD_MUREX3FXFI" -> "MX3_FXFI",
"MEX_MUREX3FXFI" -> "MX3_LT",
"MX3BRASIL" -> "MX3_BR",
"MX3EUROPAEQ_MAD" -> "MX3_EQ",
"MX3EUROPAEQ_POL" -> "MX3_EQ",
"MXEUROPA_MAD" -> "MX2_EU",
"MXEUROPA_PT" -> "MX2_EU",
"MXEUROPA_UK" -> "MX2_EU",
"MXLATAM_CHI" -> "MX2_LT",
"MXLATAM_NEW" -> "MX2_LT",
"MXLATAM_SOV" -> "MX2_LT",
"POR_MUREX3FXFI" -> "MX3_FXFI",
"SHN_MUREX3FXFI" -> "MX3_FXFI",
"UK_MUREX3FXFI" -> "MX3_FXFI",
"SOV_MX3LATAM" -> "MX3_LT"
)
I need to replace them to the short code, and using a foldLeft to do a withColumn is giving me only null values, because its replacing all the values and the last source_system is not in the map:
val ssReplacedDf = irisToCreamSourceSystem.foldLeft(tempDf) { (acc, filter) =>
acc.withColumn("source_system", when( col("source_system").equalTo(lit(filter._1)),
lit(filter._2)))
}
I would suggest another solution by joining the translation table :
// convert Map to a DataFrame
val convertSourceSystemDF = convertSourceSystem.toSeq.toDF("source_system","source_system_short")
tempDf.join(broadcast(convertSourceSystemDF),Seq("source_system"),"left")
// override column with short name, alternatively use withColumnRenamed
.withColumn("source_system",$"source_system_short")
.drop("source_system_short)
I have one map like
val strMap = Map[String, String]("a" -> "a1", "b" -> "b1") // Map(a -> a1, b -> b1)
and I want to create another map with same key but different value, based on value in strMap. For example
case class Data(data: String) {}
var dataMap = scala.collection.mutable.Map[String, Data]()
strMap.foreach (keyVal => {dataMap(keyVal._1) = Data(keyVal._2)})
val dataMapToUse = dataMap.toMap // Map(a -> Data(a1), b -> Data(b1))
but writing this in imperative style is causing issue like creation of "var dataMap", though I want to get immutable map. Because of this, I have to call toMap to get same.
How can I achieve same in functional programming?
Scala Version: 2.11
Why not simply use,
val dataMapToUse = strMap.map{case(k,v) =>(k -> Data(v))}
I'm running a ScalaTest asserting the right data type is returned by my actor.
The actor named "testActor" converts data from SortedMap[Long, SortedMap[String, Double]] to SortedMap[String, Array[Double]]
The current code is:
val data: SortedMap[Long, SortedMap[String, Double]] = SortedMap (1000L -> SortedMap("c1" -> 1., "c2" -> 2.1), 2000L -> SortedMap("c1" -> 1.1), 3000L -> SortedMap("c1" -> 0.95))
val expectedResult = SortedMap("t1" -> Array(1., 1.1, 0.95), "t2" -> Array(2.1))
actor ! testActor(data)
expectMsg(replyTestActor(expectedResult)
For some reason the assert is done on the map physical address, i.e.
assertion failed: expected replyTestActor(Map(c1 -> [D#60b8a8f6, c2 -> [D#7b5ce015),2,2000), found replyTestActor(Map(c1 -> [D#e7bc1f9, c2 -> [D#5efbc9dc),2,2000)
I must comment that in debug mode, when i enter "Expression Evaluation" on a break point the actor message and the "expectedValue" are identical×¥
The problem is the values in your SortedMap.
> Array(42) == Array(42)
res0: Boolean = false
Array does not provide a friendly equal implementation.
Edit: plus, Array is a mutable structure, usually not recommend to use them while passing messages between actors.
I've a Scala Map that contains a bunch of parameters that I get in a HTTP request.
val queryParams = Map( ("keyword" -> "k"), ("from" -> "f"), ("to" -> "t"), ("limit" -> "l") )
I've a method that takes all these parameters.
def someMethod( keyword: String, from: String, to: String, limit: String) = { //do something with input params }
I want to pass the parameters from the map into my method someMethod.
queryParams.get returns an Option. So I can call something like queryParams.get("keyword").getOrElse("") for each input parameter.
someMethod( queryParams.get("keyword").getOrElse(""), queryParams.get("from").getOrElse(""), queryParams.get("to").getOrElse(""), queryParams.get("limit").getOrElse(""))
Is there a better way ?
If all the parameters have the same default value you can set a default value to the entire Map:
val queryParams = Map( ("keyword" -> "k"), ("from" -> "f"), ("to" -> "t"), ("limit" -> "l") ).withDefaultValue("")
someMethod( queryParams("keyword"), queryParams("from"), queryParams("to"), queryParams("limit"))
withDefaultValue return a Map which for any non exist value return the default value. Now that you are sure you always get a value you can use queryParams("keyword") (without the get function).
I'm trying to send an object to a remote actor and I got this exception:
ERROR akka.remote.EndpointWriter - Transient association error (association remains live)
java.io.NotSerializableException: scala.collection.immutable.MapLike$$anon$2
The object being serialized is a case class:
case class LocationReport(idn: String, report: String, timestamp: Option[String], location: Attr, status: Attr, alarms: Attr, network: Attr, sensors: Attr) extends Message(idn) {
val ts = timestamp getOrElse location("fix_timestamp")
def json =
(report ->
("TIME" -> ts) ~
("location" -> location) ~
("alarms" -> alarms) ~
("network" -> network) ~
("sensors" -> ((status ++ sensors) + ("CUSTOMCLOCK" -> Report.decodeTimestamp(ts)))))
}
And Attr is a type re-definition:
type Attr = Map[String, String]
The Message class is pretty simple:
abstract class Message(idn: String) {
def topic = idn
def json(): JValue
}
I'm wondering if the type alias/redefinition is confusing the serializer. I think I'm using ProtoBuf serialization, but I do see JavaSerializer in the stacktrace.
More Debugging Info
I newed up a JavaSerializer and individually serialized each of the Maps. Only one (alarms) fails to serialize. Here's the toString of each of them:
This one failed:
alarms = Map(LOWBATTERY -> 1373623446000)
These succeeded:
location = Map(a_value -> 6, latitude -> 37.63473, p_value -> 4, longitude -> -97.41459, fix_timestamp -> 3F0AE7FF, status -> OK, fix_type -> MSBL, CUSTOMCLOCK -> 1373644159000)
network = Map(SID -> 1271, RSSI -> 85)
sensors = Map(HUMIDITY -> -999, PRESSURE -> -999, LIGHT -> -999 9:52 AM)
status = Map(TEMPERATURE_F -> 923, CYCLE -> 4, TEMPERATURE1_C -> 335, CAP_REMAINING -> 560, VOLTAGE -> 3691, CAP_FULL -> 3897)
The problem is that Map.mapValues produces an object that's not serializable. When alarms was created, it's run through something like alarms.mapValues(hex2Int). The problem and workaround is described here:
https://issues.scala-lang.org/browse/SI-7005
In short, the solution is to do alarms.mapValues(hex2Int).map(identity)
Not sure whether this works in all cases but my workaround was simply to convert the map into a sequence (just .toSeqbefore the sequence) before serialization. toMap should give you the same map after deserialization.