Need help with calling and returning value from anon function in scala

56 views
Skip to first unread message

tushar pandit

unread,
May 7, 2019, 10:06:54 PM5/7/19
to scala-functional
Hi,

I am trying to call function fetchFromDruid

def main(args: Array[String]): Unit = {
val res = fetchFromDruid()
  // res comes as null here
}

def fetchFromDruid(): GroupByResponse {
implicit val executionContext = ExecutionContext.Implicits.global
val client = DruidClient("http://localhost:8082")

val query = GroupByQuery(
source = "wikipedia",
interval = new Interval(new DateTime().minusMonths(60), new DateTime()),
dimensions = Seq("countryName"),
descending = true,
granularity = Granularity.All,
aggregate = Seq(
DSL.count("row_count")
),
postAggregate = Seq(
),
limit = Some(100)
)

client(query).onComplete {
case Success(resp) =>
resp.data.foreach { row =>
println(row)
}
      return resp
println("none")
//System.exit(0)
case Failure(ex) =>
ex.printStackTrace()
      return null
//System.exit(0)
}
}

But somehow I am not able to return the response to the caller; i.e the main function. What could be the issue?

Thanks,
Tushar

Jed Wesley-Smith

unread,
May 7, 2019, 10:13:41 PM5/7/19
to scala-fu...@googlegroups.com
Tushar, this seems to be an issue with whatever Druid is. This mailing list is for questions about Functional Programming in Scala, both in general and more specifically the book with that title. If you ask your question in a more relevant forum somebody may be able to provide you with an answer.

--
You received this message because you are subscribed to the Google Groups "scala-functional" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-function...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/scala-functional/8bc53b5e-729d-497d-89bd-ab2568674cc7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

tushar pandit

unread,
May 7, 2019, 10:18:26 PM5/7/19
to scala-functional
Yeah. I am sorry to put it in a confusing manner. Yeah I am working with Druid in Scala, but my question is about Scala code itself. So consider I get some data from some data store (in my case it is Druid), I get the dataset in an object and now I need to pass this object to the calling function. I am unable to do this.

To simplify the code:

def main(args: Array[String]): Unit = {
  val res = fetchData()
  // res comes as null here
}

def fetchData():Any {

client(query).onComplete {
case Success(resp) =>
      return resp
case Failure(ex) =>
ex.printStackTrace()
      return null
}
}

So my "res" in "main" method does not hold any results after the call is done. But when I try to print the "resp" in "success" it prints all the fetched data. 
To unsubscribe from this group and stop receiving emails from it, send an email to scala-fu...@googlegroups.com.

tushar pandit

unread,
May 7, 2019, 10:19:37 PM5/7/19
to scala-functional
now I need to return* this object

Jed Wesley-Smith

unread,
May 7, 2019, 10:27:12 PM5/7/19
to scala-fu...@googlegroups.com
Scala is an expression oriented language, so the last thing you refer to is returned.

def foo: Int = 3

returns 3. There is no need to use the return keyword, indeed its use can complicate matters as you can create non-local returns, which I suspect is what is confusing you in your example.

There are some very good books that explain how Scala works, as well as some good online tutorials. The book "Functional Programming in Scala" is an excellent book introducing the various concepts of FP and how to use them effectively. It does help to have a basic general knowledge of the language though, so may not be the ideal introduction.

For further questions about how the basics of the language work, there are many links on the https://www.scala-lang.org/community/ page.

To unsubscribe from this group and stop receiving emails from it, send an email to scala-function...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/scala-functional/c8a0dac0-5163-4de9-a389-e63767b4b4f6%40googlegroups.com.

Steven Parkes

unread,
May 7, 2019, 11:00:41 PM5/7/19
to scala-fu...@googlegroups.com
The issue is here is that the `onComplete` call is async (assuming https://github.com/daggerrz/druid-scala-client is the library you're using)

It looks like `DruidClient#apply` returns a `Future`: https://github.com/daggerrz/druid-scala-client/blob/master/src/main/scala/com/tapad/druid/client/DruidClient.scala

In this case, `onComplete` gets called at some point in the future (possibly/probably after `fetchFromDruid` has returned.) The result of `onComplete` on a `Future` is `Unit`: the `onComplete` is synchronous but the callback is async.

If you want your method to wait until the Druid call returns, you want to use `Future#transform` (https://www.scala-lang.org/api/current/scala/concurrent/Future.html) and `Await.result` (https://www.scala-lang.org/api/current/scala/concurrent/Await$.html)

tushar pandit

unread,
May 11, 2019, 7:49:05 PM5/11/19
to scala-functional
Thanks Steven. Can I; in any way modify the code to not have a Future? I mean I need the response immediately and not in the future and using await is not an option here as I don't know the actual duration each request will take.

--
You received this message because you are subscribed to the Google Groups "scala-functional" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-fu...@googlegroups.com.

Steven Parkes

unread,
May 13, 2019, 12:00:51 PM5/13/19
to scala-fu...@googlegroups.com
You can use `Await#result`, just give the wait time (`atMost`) as `Duration.Inf``


To unsubscribe from this group and stop receiving emails from it, send an email to scala-function...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/scala-functional/e65ceeae-5107-4de4-bbb3-922e121d0be5%40googlegroups.com.

Vlad Patryshev

unread,
May 13, 2019, 9:39:58 PM5/13/19
to scala-fu...@googlegroups.com
I'm just curious if we could apply this approach to other problems. Like "I want this code decrypted right away, not 1000 years from now. Can Scala do that, instead of making me wait?

Thanks,
-Vlad


On Sat, May 11, 2019 at 4:49 PM tushar pandit <tushar....@gmail.com> wrote:
To unsubscribe from this group and stop receiving emails from it, send an email to scala-function...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/scala-functional/e65ceeae-5107-4de4-bbb3-922e121d0be5%40googlegroups.com.

toby

unread,
May 14, 2019, 8:25:30 AM5/14/19
to scala-functional


On Monday, 13 May 2019 21:39:58 UTC-4, Vlad Patryshev wrote:
I'm just curious if we could apply this approach to other problems. Like "I want this code decrypted right away, not 1000 years from now. Can Scala do that, instead of making me wait?

Yes…………just do it synchronously?

 

Thanks,
-Vlad


Jed Wesley-Smith

unread,
May 16, 2019, 11:07:54 PM5/16/19
to scala-fu...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages