2016-05-12 78 views
2

我是新來的Slick並使用Slick 3.1.1。我的表看起來像Tuple的元素太多:27,允許:22

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, 
     ^

我該怎麼辦?

回答

2

根據來自@ 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) 
} 
1

您可以使用HList。 Slick有它自己的實現,或者可能使用無形的更好的互操作性。這裏有一篇文章解釋它:http://underscore.io/blog/posts/2015/08/08/slickless.html

或者,你可以使用一個case類而不是一個元組。

+0

哪個沒形?我正在使用'Maven',這是你正在使用的? http://mvnrepository.com/search?q=shapeless – daydreamer

+0

這一個:https://github.com/milessabin/shapeless。看起來這是最新的com.chuusai:無形:2.3.1 – cvogt

+0

http://mvnrepository.com/artifact/com.chuusai/shapeless_2.11/2.3.0 – daydreamer

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抱怨,出現以下錯誤

enter image description here

0

正如cvogt已經說了,你可以使用嵌套case類,他們更容易與之合作,見here。我知道HLists是強大的,但恕我直言,人們迫使他們太多......案例類有什麼問題? xD

您應該從包含< = 22個字段的2個或更多個案例類組成AnomalyC類。