Reading Configuration in Play (2.6.5) with Scala without #Inject - scala

I'm using Play 2.6.5 with Scala. Most of the times reading from the configuration works with injecting the configuration.
In some parts it would be too difficult to get a class into injection scope.
Is there a way to get the configuration object without injection?

You can use ConfigFactory ->
ConfigFactory.load().getString("db.url")
import for the same ->
import com.typesafe.config.ConfigFactory

Related

Scala/Spark/Databricks: How to import code from user-created JAR?

I have a JAR that I created with intellij and sbt, that defines a case class and Object. I've uploaded it to my databricks workspace, and attached it to a cluster as a library.
How do I actually import code from it/reference it in my notebook? So far all I can think of is to try
import NameOfJar._
which gives
java.lang.NoClassDefFoundError: DataFrameComparison$
Do I need to have built the jar differently somehow? (Package statement or something?)
you should import import packageName._, jar name is not used in import statement. It should work the same as in usual local java/scala code.
You can check this article for details - https://docs.databricks.com/libraries.html
Btw, does your notebook fail on import itself, or later, when you're trying to use class, that exists inside jar?

Problems importing akka scala

I am writing a toy Akka example to get some understanding of the concepts. I am having trouble importing the akka model into my project in intelij.
I am making the below import statement
import akka.actor.Actor
The error messgae reads
cannot resolve the symbol Akka
Is there an additional step to working with akka that I have missed ?
Why does the compiler not recognise the import ?
You have to add Akka dependency to your build.sbt
https://mvnrepository.com/artifact/com.typesafe.akka/akka-actor_2.11/2.5.18
Then you should reimport the dependency, using sbt tool in your ide as mentioned here: https://stackoverflow.com/a/20466144/2201566

Upgrading Play to 2.4, Slick to 3.1.1, value withTransaction is not a member of play.api.db.slick.Database

I am trying to upgrade my application from using Play 2.3.x to Play 2.4.x (will end at 2.6, but going one step at a time) and Slick from 2.1.0 to 3.1.1.
I have done my best to follow Play's migration guide, the Play Slick migration guide, and the Slick upgrade guides.
One of the problems I'm having right now is with the following line:
val db: slick.Database = play.api.db.slick.DB
This no longer seems to be the correct way to do this b/c I get errors like:
value withTransaction is not a member of play.api.db.slick.Database
From the Play slick migration guide, it seems like I should modify this to something like
val dbConfig = DatabaseConfigProvider.get[JdbcProfile](Play.current)
But idk if I just don't have the right imports or what, but I get errors like:
object driver is not a member of package play.api.db.slick
not found: value DatabaseConfigProvider
For more context, here is one of the files I'm working with that gives this error: https://github.com/ProjectSidewalk/SidewalkWebpage/blob/2c48dfa2e34c691e40568bfa9d50493aa3fe9971/app/models/attribute/GlobalAttributeTable.scala
Anyone know what I missed among these migration guides?
Thank you in advance!
Turns out that I was missing a few things:
I had not realized that I needed to use a more recent version of the play-slick library (I was using 0.8.0 still instead of 1.1.1).
I needed to add the import import play.api.Play instead of the import import play.api.Play.current that I already had.
I had an import import play.api.db.slick that was causing the "object driver is not a member of package play.api.db.slick" error at the line with this import: import slick.driver.JdbcProfile. I just removed the former import that was not needed.
As #Valerii said, withTransaction has been removed in Slick 3.1, and the replacement is documented in the various links in the comments above.

Unable to use Apache Commons CLI Option.builder() in Scala

In a spark shell or application (written in Scala/maven build), I am unable to use the static builder method from the Apache Commons CLI package. I have confirmed that I am including the jar in the class path and have access to the Option class along with other classes in the package like Options, DefaultParser, etc. Why can I not use this public static method in Scala?
import org.apache.commons.cli.Option
val opt = Option.builder("foo").build()
error: value builder is not a member of object org.apache.commons.cli.Option
I can however see the static fields Option.UNINITIALIZED and Option.UNLIMITED_VALUES
using commons-cli 1.3.1
Scala version: 2.11.8
Spark version: 2.2.0
command to start the shell: spark-shell --jars .m2/repository/commons-cli/commons-cli/1.3.1/commons-cli-1.3.1.jar
Let me help you clarify your problem scenario.
You can open your .idea folder, find that it have some internal jar dependencies already, and of the list commons_cli exists, but 1.2 version.
This would lead to class collision.
The solution is straightforward, refer the doc, use the compatible constructor method.

Importing anorm.SQL in IntelliJ IDEA 13

IntelliJ 13 Community Edition
Play Framework 2.2
Scala 2.10.2
I am importing anorm._ and using SQL in my object. The object starts off as follows:
package controllers
import play.api.mvc._
import play.api.db.DB
import play.api.Play.current
import anorm._
object Walks extends Controller {
val futureWalksSql = SQL("SELECT * FROM walks where evt_date > now()")
IntelliJ cannot resolve symbol SQL. If I ctrl+Enter, after anorm. there is no SQL option, although there is a .Sql trait, object and class.
When I run the play project, it all works just fine, with no compilation errors so this Scala is syntactically correct but IntelliJ isn't picking this up. I have created the idea files by calling idea from within the play console and I've also tried idea with-sources=yes.
How do I get IntelliJ Community Editon to pick up anorm.SQL? What is so special about this object? I'm still learning Scala and so this might be a Scala issue.
SQL is a method defined in a package object anorm. So when you import anorm._ you import the entire package with the package object as well. I have actually no clue why Idea does not pick this up. But if you look into the package object sources you can see that the SQL method is just a wrapper on anorm.Sql.sql(inSql: String).
As a workaround you may try to import anorm.Sql._ and use sql("select 1") instead of SQL("select 1")