title: Writing to HBase via Spark: Task not serializable
body: <p>I'm trying to write some simple data in HBase (0.96.0-hadoop2) using Spark 1.0 but I keep getting getting serialization problems. Here is the relevant code:</p>
  
  <pre><code>import org.apache.hadoop.hbase.client._
  import org.apache.hadoop.hbase.io.ImmutableBytesWritable
  import org.apache.hadoop.hbase.util.Bytes
  import org.apache.spark.rdd.NewHadoopRDD
  import org.apache.hadoop.hbase.HBaseConfiguration
  import org.apache.hadoop.mapred.JobConf
  import org.apache.spark.SparkContext
  import java.util.Properties
  import java.io.FileInputStream
  import org.apache.hadoop.hbase.client.Put
  
  object PutRawDataIntoHbase{
    def main(args: Array[String]): Unit = {
      var propFileName = "hbaseConfig.properties"
      if(args.size &gt; 0){
        propFileName = args(0)
      }
  
      /** Load properties here **/
     val theData = sc.textFile(prop.getProperty("hbase.input.filename"))
       .map(l =&gt; l.split("\t"))
       .map(a =&gt; Array("%010d".format(a(9).toInt)+ "-" + a(0) , a(1)))
  
     val tableName = prop.getProperty("hbase.table.name")
     val hbaseConf = HBaseConfiguration.create()
     hbaseConf.set("hbase.rootdir", prop.getProperty("hbase.rootdir"))
     hbaseConf.addResource(prop.getProperty("hbase.site.xml"))
     val myTable = new HTable(hbaseConf, tableName)
     theData.foreach(a=&gt;{
       var p = new Put(Bytes.toBytes(a(0)))
       p.add(Bytes.toBytes(hbaseColFamily), Bytes.toBytes("col"), Bytes.toBytes(a(1)))
        myTable.put(p)
      })
    }
  }
  </code></pre>
  
  <p>Running the code results in:</p>
  
  <pre><code>Failed to run foreach at putDataIntoHBase.scala:79
  Exception in thread "main" org.apache.spark.SparkException: Job aborted due to stage failure: Task not serializable: java.io.NotSerializableException:org.apache.hadoop.hbase.client.HTable
  </code></pre>
  
  <p>Replacing the foreach with map doesn't crash but I doesn't write either. 
  Any help will be greatly appreciated.</p>