如何解决使用Akka.io从Azure SQL数据库提取数据
当前,我能够使用intelliJ创建会话:
//sqlserver is the name of application.conf {}
val databaseConfig = DatabaseConfig.forConfig[JdbcProfile]("sqlserver")
implicit val session = SlickSession.forConfig(databaseConfig)
这是配置:
sqlserver = {
profile = "slick.jdbc.SQLServerProfile$"
db {
driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"
host = <myHostName> e.g. myresource.database.windows.net
port = <myPortNumber> e.g 1433
databaseName = <myDatabaseName>
url = <jdbc:sqlserver:myHostName:port;database=myDatabase>
user = <user>
password = <password>
connectionTimeout = "30 seconds"
}
}
建议的一些方法是:
// The example domain
case class User(id: Int,name: String)
val users = (1 to 42).map(i => User(i,s"Name$i"))
// This import enables the use of the Slick sql"...",// sqlu"...",and sqlt"..." String interpolators.
// See "http://slick.lightbend.com/doc/3.2.1/sql.html#string-interpolation"
import session.profile.api._
// Stream the users into the database as insert statements
val done: Future[Done] =
Source(users)
.via(
// add an optional first argument to specify the parallelism factor (Int)
Slick.flow(user => sqlu"INSERT INTO ALPAKKA_SLICK_SCALADSL_TEST_USERS VALUES(${user.id},${user.name})")
)
.log("nr-of-updated-rows")
.runWith(Sink.ignore)
我找不到用于从Akka.io使用SQL命令提取任何数据的方法的任何示例。最接近的链接位于此链接: [Akka.io Slink JDBC] [1]
目前,连接没有错误,但是我仍然缺少使用Query方法访问和从Azure SQL数据库下载的方法。
这个看起来好像是在创建自己的矢量列表。
case class User(id: Int,s"Name$i"))
结果:Vector(User(1,Name1),User(2,Name2),....)
是否可以从Azure SQL服务器提取数据? [1]:https://doc.akka.io/docs/alpakka/current/slick.html
解决方法
如果要将数据从SQL Server获取到Akka流中,则需要Source
,而不是Sink
(用于将Akka写入数据库)。
由于Alpakka将JDBC集成推迟到Slick库中,因此值得在该库中阅读。
从文档中,您将需要以下内容:
import slick.jdbc.GetResult
import session.profile.api._
case class User(id: Int,name: String)
// Define how to transform result rows (each row being a PositionedResult)
// into Users. See https://scala-slick.org/doc/3.3.2/sql.html
implicit val getUserFromResult = GetResult(r => User(r.nextInt,r.nextString))
val gotAllUsers: Future[Done] =
Slick.source(sql"SELECT id,name FROM table".as[User])
.log("user")
.runWith(Sink.ignore)
// Wait for the query to complete before exiting,only useful for this example.
Await.result(gotAllUsers,Duration.Inf)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。