【问题标题】:Scala Compile Error Inserting into Postgres via Slick通过 Slick 插入 Postgres 的 Scala 编译错误
【发布时间】:2013-10-28 09:50:39
【问题描述】:

更新:关于 Projection 与 ColumnBase 的错误已得到解决,但是我仍然遇到重载插入方法的问题。代码已更新以反映这一点。


我正在尝试编写我的第一个中等复杂的 Scala 程序。我希望它从我的 Twitter 时间轴中读取值并将它们写入 postgres 表。

我使用 twitter4j 进行 Twitter 连接,使用 Slick 将数据写入 postgres,但是出现两个编译错误:

错误:

[error] /Users/trenthauck/Dropbox/Code/twitter-api/src/main/scala/org/trenthauck/TwitterApi.scala:45: overloaded method value insert with alternatives:
[error]   [TT](query: scala.slick.lifted.Query[TT,org.trenthauck.Timeline])(implicit session: scala.slick.session.Session)Int <and>
[error]   (value: org.trenthauck.Timeline)(implicit session: scala.slick.session.Session)Int
[error]  cannot be applied to (java.util.Date, Int, String)
[error]       Timeline.insert(date, id, text)
[error]                ^
[error] one error found
[error] (compile:compile) Compilation failed

DatabaseApi.scala 文件中的我的 Slick 定义:

package org.trenthauck

import slick._
import scala.slick.driver.PostgresDriver.simple._
import Database.threadLocalSession

import java.sql.Date

case class Timeline (date: Date, id: Long, text: String)

object Timeline extends Table[Timeline]("timeline") {
  def date = column[Date]("date")
  def id = column[Long]("id")
  def text = column[String]("text")
  def * = date ~ id ~ text <> (Timeline.apply _, Timeline.unapply _)
}

TwitterApi.scala 文件中的我的 Twitter 定义:

package org.trenthauck

import twitter4j._

import slick._
import scala.slick.driver.PostgresDriver.simple._
import Database.threadLocalSession

trait TwitterInstance {
  val consumerKey = ""
  val consumerSecret = ""
  val accessToken = ""
  val accessSecret = ""

  val cb = new conf.ConfigurationBuilder()

  cb.setOAuthConsumerKey(consumerKey)
    .setOAuthConsumerSecret(consumerSecret)
    .setOAuthAccessToken(accessToken)
    .setOAuthAccessTokenSecret(accessSecret)

  val twitter = new TwitterFactory(cb.build()).getInstance

}

class Twitter extends TwitterInstance {
  //get timeline
  def getUserTimeline() = twitter.getUserTimeline()

  //insert tweets
  def insertTweets() = {
    val tweets = this.getUserTimeline.iterator
    implicit val session = Database.forURL("jdbc:postgres//localhost/twitter",
                                            driver="org.postgresql.Driver",
                                            user="trenthauck")
    session withTransaction {

      Timeline.ddl.create

      while(tweets.hasNext) {
        var tweet = tweets.next
        Timeline.insert(tweet.getCreatedAt, tweet.getId, tweet.getText)
      }
    }
  }
}

与 twitter 的连接似乎工作得很好,它将数据插入到时间线表中导致了问题。

谢谢

【问题讨论】:

    标签: postgresql scala twitter slick


    【解决方案1】:

    您需要为Timeline 类定义映射。试试这个。。

    def * = date ~ id ~ text <> (Timeline.apply _, Timeline.unapply _)
    

    【讨论】:

    • 感谢 (+1),解决了有关投影与列基的问题,但仍然存在重载问题。
    【解决方案2】:

    vidit 发布的内容意味着您将默认投影映射到 Timeline 案例类,因此您的插入应该使用案例类而不是原始元组。

    但是,由于您的代码示例将案例类和表定义为具有相同的名称,因此它可能仍会出错。我建议你至少重命名你的表,然后你的插入看起来像TimelineTable.insert(Timeline(tweet.getCreatedAt, tweet.getId, tweet.getText))

    【讨论】:

      【解决方案3】:

      object Timeline更改映射器对象

      收件人:object Timelines

      然后做Timelines.insert(...)

      case class Timeline 的内置伴生对象会干扰 Slick 功能。

      查看 Slick 文档中的所有 example code,伴生对象总是复数形式,或者完全删除,而是使用普通的类/实例化。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-12
        • 2021-03-21
        相关资源
        最近更新 更多