gpt4 book ai didi

scala - Cassandra 单元与 scala。不是 session

转载 作者:行者123 更新时间:2023-12-02 03:57:38 25 4
gpt4 key购买 nike

我正在尝试使用 cassandraunit 测试 Cassandra。我从 github 粘贴了测试并翻译为 Scala,但它不起作用。

import org.junit.Rule
import org.scalatest.FunSuite
import com.datastax.driver.core.ResultSet
import org.cassandraunit.CassandraCQLUnit
import org.cassandraunit.dataset.cql.ClassPathCQLDataSet

class CassandraTest extends FunSuite{

@Rule
val cassandraCQLUnit = new CassandraCQLUnit(new ClassPathCQLDataSet("statementsWithBlankEndings.cql", "mykeyspace"))

test("really simple transformation") {

val session = cassandraCQLUnit.session
val resultSet = session.execute("select * from testCQLTable WHERE id='1690e8da-5bf8-49e8-9583-4dff8a570737'")
println(resultSet.iterator().next().getString("value"))

}
}


java.lang.NullPointerException was thrown.
java.lang.NullPointerException
at CassandraTest$$anonfun$1.apply$mcV$sp(CassandraTest.scala:21)
at CassandraTest$$anonfun$1.apply(CassandraTest.scala:18)
at CassandraTest$$anonfun$1.apply(CassandraTest.scala:18)
at org.scalatest.OutcomeOf$class.outcomeOf(OutcomeOf.scala:85)
at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
at org.scalatest.Transformer.apply(Transformer.scala:22)
at org.scalatest.Transformer.apply(Transformer.scala:20)
at org.scalatest.FunSuiteLike$$anon$1.apply(FunSuiteLike.scala:186)
at org.scalatest.TestSuite$class.withFixture(TestSuite.scala:196)
at org.scalatest.FunSuite.withFixture(FunSuite.scala:1560)
at org.scalatest.FunSuiteLike$class.invokeWithFixture$1(FunSuiteLike.scala:183)
at org.scalatest.FunSuiteLike$$anonfun$runTest$1.apply(FunSuiteLike.scala:196)
at org.scalatest.FunSuiteLike$$anonfun$runTest$1.apply(FunSuiteLike.scala:196)
at org.scalatest.SuperEngine.runTestImpl(Engine.scala:289)
at org.scalatest.FunSuiteLike$class.runTest(FunSuiteLike.scala:196)
at org.scalatest.FunSuite.runTest(FunSuite.scala:1560)
at org.scalatest.FunSuiteLike$$anonfun$runTests$1.apply(FunSuiteLike.scala:229)
at org.scalatest.FunSuiteLike$$anonfun$runTests$1.apply(FunSuiteLike.scala:229)
at org.scalatest.SuperEngine$$anonfun$traverseSubNodes$1$1.apply(Engine.scala:396)
at org.scalatest.SuperEngine$$anonfun$traverseSubNodes$1$1.apply(Engine.scala:384)
at scala.collection.immutable.List.foreach(List.scala:381)
at org.scalatest.SuperEngine.traverseSubNodes$1(Engine.scala:384)
at org.scalatest.SuperEngine.org$scalatest$SuperEngine$$runTestsInBranch(Engine.scala:379)
at org.scalatest.SuperEngine.runTestsImpl(Engine.scala:461)
at org.scalatest.FunSuiteLike$class.runTests(FunSuiteLike.scala:229)
at org.scalatest.FunSuite.runTests(FunSuite.scala:1560)
at org.scalatest.Suite$class.run(Suite.scala:1147)
at org.scalatest.FunSuite.org$scalatest$FunSuiteLike$$super$run(FunSuite.scala:1560)
at org.scalatest.FunSuiteLike$$anonfun$run$1.apply(FunSuiteLike.scala:233)
at org.scalatest.FunSuiteLike$$anonfun$run$1.apply(FunSuiteLike.scala:233)
at org.scalatest.SuperEngine.runImpl(Engine.scala:521)
at org.scalatest.FunSuiteLike$class.run(FunSuiteLike.scala:233)
at org.scalatest.FunSuite.run(FunSuite.scala:1560)
at org.scalatest.tools.SuiteRunner.run(SuiteRunner.scala:45)
at org.scalatest.tools.Runner$$anonfun$doRunRunRunDaDoRunRun$1.apply(Runner.scala:1340)
at org.scalatest.tools.Runner$$anonfun$doRunRunRunDaDoRunRun$1.apply(Runner.scala:1334)
at scala.collection.immutable.List.foreach(List.scala:381)
at org.scalatest.tools.Runner$.doRunRunRunDaDoRunRun(Runner.scala:1334)
at org.scalatest.tools.Runner$$anonfun$runOptionallyWithPassFailReporter$2.apply(Runner.scala:1011)
at org.scalatest.tools.Runner$$anonfun$runOptionallyWithPassFailReporter$2.apply(Runner.scala:1010)
at org.scalatest.tools.Runner$.withClassLoaderAndDispatchReporter(Runner.scala:1500)
at org.scalatest.tools.Runner$.runOptionallyWithPassFailReporter(Runner.scala:1010)
at org.scalatest.tools.Runner$.run(Runner.scala:850)
at org.scalatest.tools.Runner.run(Runner.scala)
at org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.runScalaTest2(ScalaTestRunner.java:138)
at org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.main(ScalaTestRunner.java:28)

session 变量为空我不知道为什么。据我了解,cassandraCQLUnit 在同一虚拟机中有一个 Cassandra 服务器。还需要做点别的吗?

最佳答案

您需要启动服务器并连接。然后您可以将数据加载到:

import com.datastax.driver.core.{Cluster, Session}
import org.cassandraunit.CQLDataLoader
import org.cassandraunit.dataset.cql.ClassPathCQLDataSet
import org.cassandraunit.utils.EmbeddedCassandraServerHelper
import org.scalatest.{BeforeAndAfterAll, FunSuite}

class CassandraTest extends FunSuite
with BeforeAndAfterAll {

var cluster : Cluster = _
var session : Session = _

override def beforeAll(): Unit = {
EmbeddedCassandraServerHelper.startEmbeddedCassandra(EmbeddedCassandraServerHelper.CASSANDRA_RNDPORT_YML_FILE)
cluster = EmbeddedCassandraServerHelper.getCluster()
session = cluster.connect()

val dataLoader = new CQLDataLoader(session)
dataLoader.load(new ClassPathCQLDataSet("statementsWithBlankEndings.cql","mykeyspace"))
}

test("really simple transformation") {

val resultSet = session.execute("select * from mykeyspace.testCQLTable WHERE id='1690e8da-5bf8-49e8-9583-4dff8a570737'")
println(resultSet.iterator().next().getString("value"))

}

}

关于scala - Cassandra 单元与 scala。不是 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43277855/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com