Posts

Showing posts with the label scala

how do I fix the type mismatch issue?

how do I fix the type mismatch issue? I have a spark streaming job and had a question about type converting. the below is my code: val component = data.get("viewed_objects").get.asInstanceOf[ListBuffer[Map[String, Any]]] but the exception is scala.collection.immutable.$colon$colon cannot be cast to scala.collection.ListBuffer what caused the issue and how do I fix it? yes,it is a json String,like this: u'data': {u'viewed_objects': [{u'location': 3, u'keyword': u'baby carriers'}]} – Zhang Xin Jul 1 at 3:47 I print the data value, it is for example: data:Map(viewed_objects -> List(Map(location -> 2, category_name -> Toys, Kids & Babies, category_id -> 27))), how do I get the viewed_objects and traversal the elements ...

What is correct way to use operator orElse in Scala?

What is correct way to use operator orElse in Scala? I want to write two services, and then use orElse to make the two services combine together, which means service_one or service_two. They are all PartialFunctions. The service one is: val usersService = HttpService { case request @ GET -> Root / "users" / IntVar(userId) => Ok("test") } The service two is: val versionService = HttpService{ case req @ GET -> Root / "version" => { val jsonmap = ("origin" -> req.remoteAddr.getOrElse("unknown ip")) Ok(compact(render(jsonmap))) } } and then I want to combine then together. val service = userService orElse versionService //the error happens here. The error is: [error] F:workspacefrankcheckAPIsrcmainscalacomcardaccessServiceApp.scala:46: value orElse is not a member of org.http4s.HttpService [error] val service = usersService orElse versionService [error] ^ [error] one error foun...

Scala - type of collection created by cons operators

Scala - type of collection created by cons operators What is the type of collection created by cons operator? 1::2::Nil // println((1::2::Nil).getClass) // class scala.collection.immutable.$colon$colon "abc"::1::2::Nil // println(("abc"::1::2::Nil).getClass) // class scala.collection.immutable.$colon$colon val x:List[Int] = 1::2::Nil // println(x.getClass) // class scala.collection.immutable.$colon$colon This makes it seem that lists can contain both string and Int at the same time. However, this has been specifically called out in "Programming in Scala", that list contains homogeneous objects. Like lists, tuples are immutable, but unlike lists, tuples can contain different types of elements. Whereas a list might be a List[Int] or a List[String], a tuple could contain both an integer and a string at the same time. I think at runtime, we see the same types due to type erasure. If that's true, then what is the correct type of the list/collection ge...

Kafka server Shutdown after continue wrting data to topic

Kafka server Shutdown after continue wrting data to topic when i am writing data to kafka topic using spark Direct streaming 2.1, after some time kafka geting shutdown. val props = new HashMap[String, Object]() props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "ip:9092") props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer") props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer") val producer = new KafkaProducer[String, String](props) val message = new ProducerRecord[String, String](topic, partition,compression key,message) producer.send(message) What is an exception? – Popeye Jun 29 at 19:07 java.io.IOException: Too many open files, and l...

How to remove a parenthesis from an output in Scala

How to remove a parenthesis from an output in Scala I have the following output: The food order cost is: $84.9 () from this function: def foodBill(foodCost: List[Double]) = { if (foodCost.length <= 1) { println("The food order cost is: ") foodTotal.foreach(x=>println("$"+ x)) } else { println("The food order for each table is: ") foodCost.foreach(x => println("$"+ x)) } } Thus, my question is how to remove the () from the output and have the following: The food order cost is: $84.9 Thanks How do you call your function? – user152468 Jun 30 at 10:58 And what are you trying to achieve? – user152468 Jun 30 at 11:01 ...