Shark / Spark Wiki的开发部分非常简短,因此我尝试将代码组合在一起,以便以编程方式查询表。这里是 ...

object Test extends App {
  val master = "spark://localhost.localdomain:8084"
  val jobName = "scratch"

  val sparkHome = "/home/shengc/Downloads/software/spark-0.6.1"
  val executorEnvVars = Map[String, String](
    "SPARK_MEM" -> "1g",
    "SPARK_CLASSPATH" -> "",
    "HADOOP_HOME" -> "/home/shengc/Downloads/software/hadoop-0.20.205.0",
    "JAVA_HOME" -> "/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64",
    "HIVE_HOME" -> "/home/shengc/Downloads/software/hive-0.9.0-bin"
  )

  val sc = new shark.SharkContext(master, jobName, sparkHome, Nil, executorEnvVars)
  sc.sql2console("create table src")
  sc.sql2console("load data local inpath '/home/shengc/Downloads/software/hive-0.9.0-bin/examples/files/kv1.txt' into table src")
  sc.sql2console("select count(1) from src")
}


我可以创建表src并将数据加载到src中,但是最后一个查询引发了NPE并失败了,这是输出...

13/01/06 17:33:20 INFO execution.SparkTask: Executing shark.execution.SparkTask
13/01/06 17:33:20 INFO shark.SharkEnv: Initializing SharkEnv
13/01/06 17:33:20 INFO execution.SparkTask: Adding jar file:///home/shengc/workspace/shark/hive/lib/hive-builtins-0.9.0.jar
java.lang.NullPointerException
    at shark.execution.SparkTask$$anonfun$execute$5.apply(SparkTask.scala:58)
    at shark.execution.SparkTask$$anonfun$execute$5.apply(SparkTask.scala:55)
    at scala.collection.IndexedSeqOptimized$class.foreach(IndexedSeqOptimized.scala:34)
    at scala.collection.mutable.ArrayOps.foreach(ArrayOps.scala:38)
    at shark.execution.SparkTask.execute(SparkTask.scala:55)
    at org.apache.hadoop.hive.ql.exec.Task.executeTask(Task.java:134)
    at org.apache.hadoop.hive.ql.exec.TaskRunner.runSequential(TaskRunner.java:57)
    at org.apache.hadoop.hive.ql.Driver.launchTask(Driver.java:1326)
    at org.apache.hadoop.hive.ql.Driver.execute(Driver.java:1118)
    at org.apache.hadoop.hive.ql.Driver.run(Driver.java:951)
    at shark.SharkContext.sql(SharkContext.scala:58)
    at shark.SharkContext.sql2console(SharkContext.scala:84)
    at Test$delayedInit$body.apply(Test.scala:20)
    at scala.Function0$class.apply$mcV$sp(Function0.scala:34)
    at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
    at scala.App$$anonfun$main$1.apply(App.scala:60)
    at scala.App$$anonfun$main$1.apply(App.scala:60)
    at scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:59)
    at scala.collection.immutable.List.foreach(List.scala:76)
    at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:30)
    at scala.App$class.main(App.scala:60)
    at Test$.main(Test.scala:4)
    at Test.main(Test.scala)
FAILED: Execution Error, return code -101 from shark.execution.SparkTask13/01/06 17:33:20 ERROR ql.Driver: FAILED: Execution Error, return code -101 from shark.execution.SparkTask
13/01/06 17:33:20 INFO ql.Driver: </PERFLOG method=Driver.execute start=1357511600030 end=1357511600054 duration=24>
13/01/06 17:33:20 INFO ql.Driver: <PERFLOG method=releaseLocks>
13/01/06 17:33:20 INFO ql.Driver: </PERFLOG method=releaseLocks start=1357511600054 end=1357511600054 duration=0>


但是,我可以通过在bin / shark-withinfo调用的外壳中键入src中的select *,来查询src表

您可能会问我如何在由“ bin / shark-shell”触发的shell中尝试该sql。好吧,我无法进入那个外壳。这是我遇到的错误...

https://groups.google.com/forum/?fromgroups=#!topic/shark-users/glZzrUfabGc

[编辑1]:此NPE似乎是由SharkENV.sc导致的,因此我添加了

shark.SharkEnv.sc = sc


在执行任何sql2console操作之前。然后,它抱怨scala.tools.nsc的ClassNotFoundException,因此我手动将scala-compiler放在类路径中。在那之后,代码抱怨另一个ClassNotFoundException,因为我确实将鲨鱼罐子放在了类路径中,所以我不知道如何解决它。

13/01/06 18:09:34 INFO cluster.TaskSetManager: Lost TID 1 (task 1.0:1)
13/01/06 18:09:34 INFO cluster.TaskSetManager: Loss was due to java.lang.ClassNotFoundException: shark.execution.TableScanOperator$$anonfun$preprocessRdd$3
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)


[编辑2]:好的,我想出了另一个代码,通过完全遵循shark的有关如何初始化交互式repl的源代码,可以满足我的要求。

System.setProperty("MASTER", "spark://localhost.localdomain:8084")
System.setProperty("SPARK_MEM", "1g")
System.setProperty("SPARK_CLASSPATH", "")
System.setProperty("HADOOP_HOME", "/home/shengc/Downloads/software/hadoop-0.20.205.0")
System.setProperty("JAVA_HOME", "/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64")
System.setProperty("HIVE_HOME", "/home/shengc/Downloads/software/hive-0.9.0-bin")
System.setProperty("SCALA_HOME", "/home/shengc/Downloads/software/scala-2.9.2")

shark.SharkEnv.initWithSharkContext("scratch")
val sc = shark.SharkEnv.sc.asInstanceOf[shark.SharkContext]

sc.sql2console("select * from src")


这很丑,但至少可以奏效。欢迎对如何编写更健壮的代码片段发表任何评论!!

对于希望以编程方式在Shark上进行操作的用户,请注意所有蜂巢和鲨鱼罐都必须位于CLASSPATH中,而scala编译器也必须位于您的classpath中。另一个重要的事情是hadoop的conf也应该在类路径中。

最佳答案

我相信问题是您的SharkEnv尚未初始化。
我使用的是shark 0.9.0(但我相信您也必须在0.6.1中初始化SharkEnv),并且按以下方式初始化我的SharkEnv:

// SharkContext
val sc = new SharkContext(master,
    jobName,
    System.getenv("SPARK_HOME"),
    Nil,
    executorEnvVar)

// Initialize SharkEnv
SharkEnv.sc = sc

// create and populate table
sc.runSql("CREATE TABLE src(key INT, value STRING)")
sc.runSql("LOAD DATA LOCAL INPATH '${env:HIVE_HOME}/examples/files/kv1.txt' INTO TABLE src")

// print result to stdout
println(sc.runSql("select * from src"))
println(sc.runSql("select count(*) from src"))


另外,尝试从src表(带有“ select count(*)...”的注释行)中查询数据而没有聚合函数,当数据查询正常时,我也遇到了类似的问题,但是count(*)抛出异常,通过添加mysql进行了修复在我的情况下-connector-java.jar到yarn.application.classpath。

09-16 05:10