title: shark/spark throws NPE when querying a table
body: <p>The development part of shark/spark wiki is really brief, so I tried to put together a code in an effort to programmatically query a table. Here it is ...</p>
  
  <pre><code>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" -&gt; "1g",
      "SPARK_CLASSPATH" -&gt; "",
      "HADOOP_HOME" -&gt; "/home/shengc/Downloads/software/hadoop-0.20.205.0",
      "JAVA_HOME" -&gt; "/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64",
      "HIVE_HOME" -&gt; "/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")
  }
  </code></pre>
  
  <p>I can create table src and load data into src fine, but the last query threw NPE and failed, here is the output...</p>
  
  <pre><code>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: &lt;/PERFLOG method=Driver.execute start=1357511600030 end=1357511600054 duration=24&gt;
  13/01/06 17:33:20 INFO ql.Driver: &lt;PERFLOG method=releaseLocks&gt;
  13/01/06 17:33:20 INFO ql.Driver: &lt;/PERFLOG method=releaseLocks start=1357511600054 end=1357511600054 duration=0&gt;
  </code></pre>
  
  <p>However, I can query src table by typing in select * from src within the shell invoked by bin/shark-withinfo</p>
  
  <p>You might ask me how about trying that sql in the shell trigged by "bin/shark-shell". Well, I cannot get into that shell. Here is the error I came across...</p>
  
  <p><a href="https://groups.google.com/forum/?fromgroups=#!topic/shark-users/glZzrUfabGc" rel="nofollow">https://groups.google.com/forum/?fromgroups=#!topic/shark-users/glZzrUfabGc</a></p>
  
  <p>[EDIT 1]: this NPE seems to be resulting from SharkENV.sc has not been set, so I added </p>
  
  <pre><code>shark.SharkEnv.sc = sc
  </code></pre>
  
  <p>right before any sql2console opertions are executed. It then complained ClassNotFoundException of scala.tools.nsc, so I manually put scala-compiler in the classpath. After that, the code complained another ClassNotFoundException, which I cannot figure out how to fix it, since I did put shark jar in classpath.</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>[EDIT 2]: OK, I figured out another code which can fulfill what I want by following exactly shark's source code of how to initialize the interactive repl.</p>
  
  <pre><code>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")
  </code></pre>
  
  <p>this is ugly, but at least it works. Any comments of how to write a more robust piece of code is welcome!!</p>
  
  <p>For whoever wishes to programmatically operate on shark, please note that all hive and shark jars must be in your CLASSPATH, and scala compiler has to be in your classpath too. The other important thing is hadoop's conf should be in the classpath too. </p>