更新顶点时,Apache Gremlin将属性更新为不支持的属性添加

时间:2018-11-28 13:10:24

标签: scala playframework gremlin tinkerpop3

这是我遇到的错误。

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[IllegalStateException: Property addition is not supported]]
    at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:251)
    at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:178)
    at play.core.server.AkkaHttpServer$$anonfun$1.applyOrElse(AkkaHttpServer.scala:363)
    at play.core.server.AkkaHttpServer$$anonfun$1.applyOrElse(AkkaHttpServer.scala:361)
    at scala.concurrent.Future.$anonfun$recoverWith$1(Future.scala:413)
    at scala.concurrent.impl.Promise.$anonfun$transformWith$1(Promise.scala:37)
    at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:60)
    at akka.dispatch.BatchingExecutor$AbstractBatch.processBatch(BatchingExecutor.scala:55)
    at akka.dispatch.BatchingExecutor$BlockableBatch.$anonfun$run$1(BatchingExecutor.scala:91)
    at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:12)
Caused by: java.lang.IllegalStateException: Property addition is not supported
    at org.apache.tinkerpop.gremlin.structure.Element$Exceptions.propertyAdditionNotSupported(Element.java:133)
    at org.apache.tinkerpop.gremlin.structure.util.detached.DetachedVertex.property(DetachedVertex.java:91)
    at org.apache.tinkerpop.gremlin.structure.util.detached.DetachedVertex.property(DetachedVertex.java:50)
    at gremlin.scala.ScalaElement.$anonfun$updateWith$2(ScalaElement.scala:76)
    at scala.collection.immutable.HashMap$HashMap1.foreach(HashMap.scala:231)
    at scala.collection.immutable.HashMap$HashTrieMap.foreach(HashMap.scala:462)
    at gremlin.scala.ScalaElement.updateWith(ScalaElement.scala:76)
    at gremlin.scala.ScalaElement.updateWith$(ScalaElement.scala:70)
    at gremlin.scala.ScalaVertex.updateWith(ScalaVertex.scala:8)
    at gremlin.scala.ScalaElement.updateAs(ScalaElement.scala:82)

这是我的模型以及定义方法。

import java.util. UUID
import gremlin.scala.{Element, Marshallable, Vertex, id, label, underlying}


@label("trip")
case class TripModel(tripId: Option[String] = None,
                     name: String,
                     createdDate: java.time.Instant = java.time.Instant.now(),
                     startDate: java.time.Instant,
                     duration: Int,
                     totalAdults: Int,
                     totalChildrens: Int,
                     @underlying vertex: Option[Vertex] = None
                    )

object TripModel {

  implicit val marshaller = new Marshallable[TripModel] {
    def toCC(element: Element): TripModel = {
      val tripId = Some(element.value[UUID]("tripId").toString)
      val tripName = element.value[String]("name")
      val duration =  element.value[Int]("duration")
      val totalAdults = element.value[Short]("totalAdults")
      val totalChildren =element.value[Short]("totalChildrens")
      val date = element.value[java.time.Instant]("createdDate")
      val startDate = element.value[java.time.Instant]("startDate")

      models.TripModel(tripId = tripId, name = tripName,
        startDate  = startDate, duration = duration,
        totalAdults = totalAdults, totalChildrens = totalChildren, createdDate = date)
    }

    override def fromCC(cc: TripModel): FromCC = {
      val values = Map("name" -> cc.name,
        "createdDate" -> cc.createdDate,
        "startDate" -> cc.startDate,
        "duration"-> cc.duration,
        "totalAdults" -> cc.totalAdults,
        "totalChildrens" -> cc.totalChildrens,
        "tripId" -> UUID.fromString(cc.tripId.get).toString)
      FromCC(None, "trip", values)
    }
  }
}

我更新顶点的方式如下

def updateTripDetails(id:String, tripUpdate: UpdateTripRequest) = {
    implicit val g = db.g

    val item = db.g.V().has(KeyValue(tripId, id)).head
    item.updateAs[TripModel](cc => cc.copy(name = tripUpdate.tripName.getOrElse(cc.name)))
  }

我想做的是更新某个顶点属性,但是我总是收到上述错误。有什么解决方法吗?

我正在连接到gremlin服务器。这就是我建立连接的方式。

class GrDbConnection @Inject()  (config: Configuration) {

  private val dseCluster = DseCluster.builder()
    .addContactPoints(config.get[String]("dse.cluster.address"))
    .withPort(config.get[Int]("dse.cluster.port"))
    .build()

  private val graphName = "graphName"
  private val graphOptions = new GraphOptions().setGraphName(graphName)
  private val session = dseCluster.connect()

  session.executeGraph(new SimpleGraphStatement("schema.config().option('graph.schema_mode').set('development')").setGraphName(graphName))
  session.executeGraph(new SimpleGraphStatement("schema.config().option('graph.allow_scan').set('true')").setGraphName(graphName))
  // Create a ScalaGraph from a remote Traversal Source using withRemote
  // See: http://tinkerpop.apache.org/docs/current/reference/#connecting-via-remotegraph for more details
  private val connection = DseRemoteConnection.builder(session).withGraphOptions(graphOptions).build()
  val g:ScalaGraph = EmptyGraph.instance().asScala
    .configure(_.withRemote(connection))
}

1 个答案:

答案 0 :(得分:0)

您没有显示如何创建g,但我假设您正在连接到远程图(例如,该图托管在Gremlin Server中),而不是嵌入式图,因此在这种情况下,此行: >

val item = db.g.V().has(KeyValue(tripId, id)).head

返回一个DetachedVertex。它不了解所连接的图形,因此您可以将其视为只读的。您不直接更新其属性,而是使用Gremlin进行更新。我不知道playframework是如何处理基础更新的,但是我敢打赌,它不期望“分离”元素,并且假定本地嵌入式图实例以及因此知道其宿主图的元素。

也就是说,更新顶点属性的正确方法是简单地使用Gremlin而不是直接对Vertex对象本身进行操作,因此您更喜欢这样更新:

g.V(item).property('name', 'some name')