【问题标题】:Too many elements for Tuple: 27, allowed: 22元组的元素太多:27,允许:22
【发布时间】:2016-09-08 22:46:50
【问题描述】:

我是 Slick 的新手并使用 Slick 3.1.1。我的桌子看起来像

import java.sql.{Blob, Timestamp}

import slick.collection.heterogeneous.HNil

import slick.driver.MySQLDriver.api._


case class AnomalyC(id: Int, serviceName: String, serviceId: String, timeUpdated: Timestamp, timestamp: Timestamp, anomalyCategoryId: Int,
                    userGroup:Int, riskValue: Float, activityTypeId: Int, destinationHost: String, userName: String, tenantId: Int, information:Blob, timeCreated: Timestamp, userId: Int, anomalyType:Int, anomalyValue:String, measure:Int,
                    userAction:Int, uniqueIdentifier:Int, similarCount:Int, trainingValue:String, state: Int, riskLevel:Int, userRiskLevel:Int,
                    userRiskScore: Float, response:Int)
class Anomaly(tag:Tag) extends Table[AnomalyC](tag, "Anomaly") {
  def id = column[Int]("id")
  def serviceName = column[String]("ServiceName")
  def serviceId = column[Int]("ServiceId")
  def timeUpdated = column[Timestamp]("TimeUpdated")
  def timestamp = column[Timestamp]("Timestamp")
  def anomalyCategoryId = column[Int]("AnomalyCategoryId")
  def userGroup = column[Int]("UserGroup")
  def riskValue = column[Float]("RiskValue")
  def activityTypeId = column[Int]("ActivityTypeId")
  def destinationHost = column[String]("DestinationHost")
  def userName = column[String]("UserName")
  def tenantId = column[Int]("TenantId")
  def information = column[Blob]("Information")
  def timeCreated = column[Timestamp]("TimeCreated")
  def userId = column[Int]("UserId")
  def anomalyType = column[Int]("AnomalyType")
  def anomalyValue = column[String]("AnomalyValue")
  def measure = column[Int]("Measure")
  def userAction = column[Int]("UserAction")
  def uniqueIdentifier = column[String]("UniqueIdentifier")
  def similarCount = column[Int]("SimilarCount")
  def trainingValue = column[String]("TrainingValue")
  def state = column[Int]("State")
  def riskLevel = column[Int]("RiskLevel")
  def userRiskLevel = column[Int]("UserRiskLevel")
  def userRiskScore = column[Float]("UserRiskScore")
  def response = column[Int]("Response")

  def * = (id, serviceName, serviceId, timeUpdated, timestamp, anomalyCategoryId, userGroup,
    riskValue, activityTypeId, destinationHost, userName, tenantId, information, timeCreated, userId, anomalyType, anomalyValue,
    measure, userAction, uniqueIdentifier, similarCount, trainingValue, state, riskLevel, userRiskLevel, userRiskScore, response)

}

当我运行这个时,我得到错误

Error:(57, 11) too many elements for tuple: 27, allowed: 22
  def * = (id, serviceName, serviceId, timeUpdated, timestamp, anomalyCategoryId, userGroup,
          ^

我该怎么办?

【问题讨论】:

  • 正如建议的那样,HLists 是目前的发展方向。 Here's an example,包括您如何自定义 Slick 的代码生成功能以自动为您定义大表。

标签: scala slick slick-3.0


【解决方案1】:

您可以使用 HList。 Slick 有它自己的实现,或者为了更好的互操作性,您可以使用 Shapeless。这是一篇解释它的文章:http://underscore.io/blog/posts/2015/08/08/slickless.html

或者,您可以使用案例类而不是元组。

【讨论】:

【解决方案2】:

根据@cvogt 提供的方向,以下内容对我有用。

import java.sql.{Blob, Timestamp}

import slick.collection.heterogeneous.HNil
import slick.collection.heterogeneous.syntax._
import slick.driver.MySQLDriver.api._

class Anomaly(tag:Tag) extends Table[Int :: String :: Int :: Timestamp :: Timestamp :: Int :: Int :: Float :: Int :: String
   :: String :: Int ::Blob :: Timestamp :: Int ::Int ::String ::Int ::Int ::String ::Int ::String :: Int ::Int ::Int ::
  Float :: Int :: HNil ](tag, "Anomaly") {

  def id = column[Int]("id")
  def serviceName = column[String]("ServiceName")
  def serviceId = column[Int]("ServiceId")
  def timeUpdated = column[Timestamp]("TimeUpdated")
  def timestamp = column[Timestamp]("Timestamp")
  def anomalyCategoryId = column[Int]("AnomalyCategoryId")
  def userGroup = column[Int]("UserGroup")
  def riskValue = column[Float]("RiskValue")
  def activityTypeId = column[Int]("ActivityTypeId")
  def destinationHost = column[String]("DestinationHost")
  def userName = column[String]("UserName")
  def tenantId = column[Int]("TenantId")
  def information = column[Blob]("Information")
  def timeCreated = column[Timestamp]("TimeCreated")
  def userId = column[Int]("UserId")
  def anomalyType = column[Int]("AnomalyType")
  def anomalyValue = column[String]("AnomalyValue")
  def measure = column[Int]("Measure")
  def userAction = column[Int]("UserAction")
  def uniqueIdentifier = column[String]("UniqueIdentifier")
  def similarCount = column[Int]("SimilarCount")
  def trainingValue = column[String]("TrainingValue")
  def state = column[Int]("State")
  def riskLevel = column[Int]("RiskLevel")
  def userRiskLevel = column[Int]("UserRiskLevel")
  def userRiskScore = column[Float]("UserRiskScore")
  def response = column[Int]("Response")

   def *  = id :: serviceName :: serviceId :: timeUpdated :: timestamp :: anomalyCategoryId :: userGroup ::
  riskValue :: activityTypeId :: destinationHost :: userName :: tenantId :: information :: timeCreated :: userId :: anomalyType :: anomalyValue ::
  measure :: userAction :: uniqueIdentifier :: similarCount :: trainingValue :: state :: riskLevel :: userRiskLevel :: userRiskScore :: response :: HNil
}

构建运行并测试通过,但是,我仍然看到 IntelliJ 抱怨以下错误

【讨论】:

    【解决方案3】:

    根据@insan-e 的回答,我重新编写了这个,这也有效。我更喜欢这种方法,但是我不完全理解代码

    import java.sql.{Blob, Timestamp}
    
    import slick.driver.MySQLDriver.api._
    
    case class Anomaly1(id:Int, serviceName:String, serviceId: Int, timeUpdated: Timestamp, timeStamp: Timestamp,
                        anomalyCategoryId: Int, userGroup: Int, riskValue: Float, activityTypeId: Int, destinationHost: String, userName: String)
    
    case class Anomaly2(tenantId: Int, information:Blob, timeCreated: Timestamp, userId: Int, anomalyType:Int, anomalyValue: String, measure: Int, userAction: Int,
                       uniqueIdentifier: String, similarCount: Int, trainingValue: String, state: Int, riskLevel: Int,
                        userRiskLevel: Int, userRiskScore: Float, response: Int)
    
    case class AnomalyRow(anomaly1: Anomaly1, anomaly2: Anomaly2)
    
    class Anomaly(tag:Tag) extends Table[AnomalyRow](tag, "Anomaly") {
    
      def id = column[Int]("id")
      def serviceName = column[String]("ServiceName")
      def serviceId = column[Int]("ServiceId")
      def timeUpdated = column[Timestamp]("TimeUpdated")
      def timestamp = column[Timestamp]("Timestamp")
      def anomalyCategoryId = column[Int]("AnomalyCategoryId")
      def userGroup = column[Int]("UserGroup")
      def riskValue = column[Float]("RiskValue")
      def activityTypeId = column[Int]("ActivityTypeId")
      def destinationHost = column[String]("DestinationHost")
      def userName = column[String]("UserName")
      def tenantId = column[Int]("TenantId")
      def information = column[Blob]("Information")
      def timeCreated = column[Timestamp]("TimeCreated")
      def userId = column[Int]("UserId")
      def anomalyType = column[Int]("AnomalyType")
      def anomalyValue = column[String]("AnomalyValue")
      def measure = column[Int]("Measure")
      def userAction = column[Int]("UserAction")
      def uniqueIdentifier = column[String]("UniqueIdentifier")
      def similarCount = column[Int]("SimilarCount")
      def trainingValue = column[String]("TrainingValue")
      def state = column[Int]("State")
      def riskLevel = column[Int]("RiskLevel")
      def userRiskLevel = column[Int]("UserRiskLevel")
      def userRiskScore = column[Float]("UserRiskScore")
      def response = column[Int]("Response")
    
      private type Anomaly1TupleType = (Int, String, Int, Timestamp, Timestamp, Int, Int, Float, Int, String, String)
      private type Anomaly2TupleType = (Int, Blob, Timestamp, Int, Int, String, Int, Int, String, Int, String, Int, Int, Int, Float, Int)
      private type AnomalyRowTupleType = (Anomaly1TupleType, Anomaly2TupleType)
    
      private val anomalyShapedValue = (
        (id, serviceName, serviceId, timeUpdated, timestamp, anomalyCategoryId, userGroup, riskValue, activityTypeId, destinationHost, userName),
        (tenantId, information, timeCreated, userId, anomalyType, anomalyValue, measure, userAction, uniqueIdentifier, similarCount, trainingValue, state, riskLevel, userRiskLevel, userRiskScore, response)
        ).shaped[AnomalyRowTupleType]
    
      private val toAnomalyRow: (AnomalyRowTupleType => AnomalyRow) = { anomalyTuple =>
        AnomalyRow(anomaly1 = Anomaly1.tupled.apply(anomalyTuple._1), anomaly2 = Anomaly2.tupled.apply(anomalyTuple._2))
      }
    
      private val toAnomalyTuple: (AnomalyRow => Option[AnomalyRowTupleType]) = { anomalyRow =>
        Some(Anomaly1.unapply(anomalyRow.anomaly1).get, Anomaly2.unapply(anomalyRow.anomaly2).get)
      }
    
      def * = anomalyShapedValue <> (toAnomalyRow, toAnomalyTuple)
    }
    

    【讨论】:

    • 很好 .. 但如果您的任何字段的类型为 BigDecimal,则无法正常工作
    【解决方案4】:

    正如 cvogt 已经说过的,您可以使用嵌套案例类,它们更容易使用,请参阅 here。我知道 HLists 很强大,但恕我直言,人们强迫他们太多了......案例类有什么问题? xD

    您应该从包含 AnomalyC 类。

    【讨论】:

      【解决方案5】:

      由于 slick 不允许超过 22 列,并抱怨找不到 tupled 和 unapply 方法。最简单的解决方案是尽可能使用嵌套案例类。示例:

      https://github.com/timgent/spray-slick-template/blob/master/src/main/scala/com/timmeh/openhr/openholidays/model/LargeTableExample1.scala

      【讨论】:

        【解决方案6】:

        使用 HList 方法,我们只需更改默认投影*

        来自文档https://scala-slick.org/doc/3.3.3/cookbook.html

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-10-25
          • 1970-01-01
          • 2013-07-29
          • 1970-01-01
          • 2011-05-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多