ElasticClient给出NullPointerException

时间:2016-10-25 14:56:53

标签: scala elasticsearch apache-spark apache-spark-sql

使用Scala 2.11.8和Elasticsearch 2.3.5的本地实例以及以下依赖项:

libraryDependencies ++ = Seq(
"com.sksamuel.elastic4s" %% "elastic4s-core" % "2.3.1",
"org.apache.spark" %% "spark-core" % 1.6.2,
"org.apache.spark" %% "spark-sql" % 1.6.2,
"org.elasticsearch" % "elasticsearch" % 2.4.0,
"org.elasticsearch" %% "elasticsearch-spark" % 2.4.0
)

运行下面的最后一行时出现NullPointerException:

val conf = new SparkConf().setAppName("DataIndexer").setMaster("local[*]")
val sc = new SparkContext(conf)
val sqlContext = new SQLContext(sc)
val uri = ElasticsearchClientUri("elasticsearch://localhost:9300")
val client = ElasticClient.transport(uri)
client.execute(indexExists(name)).await.isExists

我是Scala / Spark的新手,我还没有找到任何有关此错误的信息。我认为这是显而易见的,但我不确定是什么。任何帮助将不胜感激。

编辑: 这是堆栈跟踪:

Exception in thread "main" java.lang.NullPointerException
at com.unleashbts.thor.ElasticIngestor$.ensureIndex(ElasticIngestor.scala:46)
at com.unleashbts.thor.ElasticIngestor$.ingest(ElasticIngestor.scala:59)
at com.unleashbts.thor.ElasticIngestor$.main(ElasticIngestor.scala:42)
at com.unleashbts.thor.ElasticIngestor.main(ElasticIngestor.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

1 个答案:

答案 0 :(得分:0)

看起来因为我在一个单独的类中这样做,我收到了这个错误(上面的示例代码不够准确)。我将代码移动到调用execute命令的类中,这解决了我的问题。