你好,我想在Scala AKKA MySql中实现sql语句,该语句使用Slick.source连接到MySQL数据库,我想 在具有特定时间段的计时器中执行查询。我想在10秒钟内从数据库中获取信息,但我没有找到有关如何执行此操作的任何信息。 我的问题是:在Slick中是否可以使用SQL语句实现计时器的任何选项。知道吗??
class AKKAMYSQL {
def run(connectionMysql: Config,mqttUrlSub:String,mqttUserSub:String,mqttPasswordSub:String,mqttTopicSub:String): Unit = {
print(" AKKAMYSQL ")
implicit val system = ActorSystem()
implicit val mat = ActorMaterializer()
val configFile = new File("application.conf")
val fileConfig = ConfigFactory.parseFile(configFile)
val config = ConfigFactory.load(fileConfig)
val sinkSettingsInflux = MqttConnectionSettings(
mqttUrlSub,
"",
new MemoryPersistence
).withAuth(mqttUserSub, mqttPasswordSub)
implicit val session = SlickSession.forConfig(connectionMysql)
system.registerOnTermination(session.close())
case class User(plant: String,source: String,value:String,timeStamp:String,tipo: String)
implicit val getUserResult = GetResult(r => User(r.nextString(), r.nextString(),r.nextString(),r.nextString(),r.nextString()))
import session.profile.api._
val done: Future[Done] =
Slick.source(sql"SELECT plantnodes.xid as planta,profilesources.xid as profileSource,profilevalues.value as value,profilevalues.ts as timeStamp,profiletypes.xid as tipo FROM profileheaders INNER JOIN profilevalues ON profilevalues.profileHeaderId = profileheaders.id INNER JOIN plantnodes ON plantnodes.id = profileheaders.plantNodeId INNER JOIN profilesources ON profilesources.id = profileheaders.profileSourceId INNER JOIN profiletypes ON profiletypes.id = profileheaders.profileTypeId WHERE profiletypes.xid = 'RTV#FR#00' OR profiletypes.xid = 'RTV#CAVG#00' OR profiletypes.xid = 'RTV#AP#00' OR profiletypes.xid = 'RTV#RP#00' OR 'RTV#SP#00' OR 'RTV#PF#00' OR 'RTV#VAVG#00' OR 'RTV#SOC#00' OR 'RTV#TH#00' LIMIT 3".as[User])
.via(Flow[User].map(f = u => {
val jsonString =
"""
{
}
val json: JsValue = Json.parse(jsonString)
new MqttMessage(topic =mqttTopicSub,ByteString.apply(Json.stringify(res)))
}))
.log("user")
.runWith(Sink.ignore)
done.onComplete {end => {
if (end.isFailure) {
println("Error " + end.get.toString())
System.exit(1)
}
else
println("Finished")
}
}
}
}
答案 0 :(得分:2)
我不认为slick
具有所需的功能。但是,您可以使用其他akka
构造来实现相同的目标。
首先,构造一个执行您的SQL查询的Actor
:
object Tick
class TickActor(akkamysql : AKKAMYSQL) extends Actor {
override def receive : Receive = {
case Tick => akkamysql.run()
}
}
然后,将此Actor与ActorSystem的Scheduler
结合使用:
val akkamysql : AKKAMYSQL = ???
val actorSystem : ActorSystem = ???
val tickActorRef = actorSystem.actorOf(Props(classOf[TickActor], akkamysql))
import system.dispatcher
val cancellable =
system.scheduler.schedule(0 milliseconds, 10000 milliseconds, tickActor, Tick)