title: java.net.ConnectException: Connection refused when trying to access a file from HDFS
body: <p>I am writing a simple standalone program using the scala API for Apache-spark. I need to access a file from HDFS. Simple commands from the shell like hadoop fs -ls work fine. however, when i try to access the file from within my program, I get the following error at runtime :</p>
  
  <p>I have been googling the errors for a few hours but can't figure out what the problem is. Can anyone tell me how to resolve this problem?</p>
  
  <pre><code>14/06/25 10:48:27 INFO Remoting: Starting remoting
  14/06/25 10:48:28 INFO Remoting: Remoting started; listening on addresses :[akka.tcp://spark@us-col-svb-0063.dunnhumby.co.uk:48183]
  14/06/25 10:48:28 INFO Remoting: Remoting now listens on addresses: [akka.tcp://spark@us-col-svb-0063.dunnhumby.co.uk:48183]
  14/06/25 10:48:30 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
  14/06/25 10:48:30 WARN snappy.LoadSnappy: Snappy native library not loaded
  [error] (run-main-0) java.net.ConnectException: Call to USHADOOP/10.152.84.251:8020 failed on connection exception: java.net.ConnectException: Connection refused
  java.net.ConnectException: Call to USHADOOP/10.152.84.251:8020 failed on connection exception: java.net.ConnectException: Connection refused
          at org.apache.hadoop.ipc.Client.wrapException(Client.java:1099)
          at org.apache.hadoop.ipc.Client.call(Client.java:1075)
          at org.apache.hadoop.ipc.RPC$Invoker.invoke(RPC.java:225)
          at com.sun.proxy.$Proxy9.getProtocolVersion(Unknown Source)
          at org.apache.hadoop.ipc.RPC.getProxy(RPC.java:396)
          at org.apache.hadoop.ipc.RPC.getProxy(RPC.java:379)
          at org.apache.hadoop.hdfs.DFSClient.createRPCNamenode(DFSClient.java:119)
          at org.apache.hadoop.hdfs.DFSClient.&lt;init&gt;(DFSClient.java:238)
          at org.apache.hadoop.hdfs.DFSClient.&lt;init&gt;(DFSClient.java:203)
          at org.apache.hadoop.hdfs.DistributedFileSystem.initialize(DistributedFileSystem.java:89)
          at org.apache.hadoop.fs.FileSystem.createFileSystem(FileSystem.java:1386)
          at org.apache.hadoop.fs.FileSystem.access$200(FileSystem.java:66)
          at org.apache.hadoop.fs.FileSystem$Cache.get(FileSystem.java:1404)
          at org.apache.hadoop.fs.FileSystem.get(FileSystem.java:254)
          at org.apache.hadoop.fs.Path.getFileSystem(Path.java:187)
          at org.apache.hadoop.mapred.FileInputFormat.listStatus(FileInputFormat.java:176)
          at org.apache.hadoop.mapred.FileInputFormat.getSplits(FileInputFormat.java:208)
          at org.apache.spark.rdd.HadoopRDD.getPartitions(HadoopRDD.scala:140)
          at org.apache.spark.rdd.RDD$$anonfun$partitions$2.apply(RDD.scala:207)
          at org.apache.spark.rdd.RDD$$anonfun$partitions$2.apply(RDD.scala:205)
          at scala.Option.getOrElse(Option.scala:120)
          at org.apache.spark.rdd.RDD.partitions(RDD.scala:205)
          at org.apache.spark.rdd.MappedRDD.getPartitions(MappedRDD.scala:28)
          at org.apache.spark.rdd.RDD$$anonfun$partitions$2.apply(RDD.scala:207)
          at org.apache.spark.rdd.RDD$$anonfun$partitions$2.apply(RDD.scala:205)
          at scala.Option.getOrElse(Option.scala:120)
          at org.apache.spark.rdd.RDD.partitions(RDD.scala:205)
          at org.apache.spark.SparkContext.runJob(SparkContext.scala:898)
          at org.apache.spark.rdd.RDD.count(RDD.scala:726)
          at MovieRecExp$.main(experiment.scala:30)
          at MovieRecExp.main(experiment.scala)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
          at java.lang.reflect.Method.invoke(Method.java:606)
  Caused by: java.net.ConnectException: Connection refused
          at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
          at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:735)
          at org.apache.hadoop.net.SocketIOWithTimeout.connect(SocketIOWithTimeout.java:206)
          at org.apache.hadoop.net.NetUtils.connect(NetUtils.java:489)
          at org.apache.hadoop.ipc.Client$Connection.setupConnection(Client.java:434)
          at org.apache.hadoop.ipc.Client$Connection.setupIOstreams(Client.java:560)
          at org.apache.hadoop.ipc.Client$Connection.access$2000(Client.java:184)
          at org.apache.hadoop.ipc.Client.getConnection(Client.java:1206)
          at org.apache.hadoop.ipc.Client.call(Client.java:1050)
          at org.apache.hadoop.ipc.RPC$Invoker.invoke(RPC.java:225)
          at com.sun.proxy.$Proxy9.getProtocolVersion(Unknown Source)
          at org.apache.hadoop.ipc.RPC.getProxy(RPC.java:396)
          at org.apache.hadoop.ipc.RPC.getProxy(RPC.java:379)
          at org.apache.hadoop.hdfs.DFSClient.createRPCNamenode(DFSClient.java:119)
          at org.apache.hadoop.hdfs.DFSClient.&lt;init&gt;(DFSClient.java:238)
          at org.apache.hadoop.hdfs.DFSClient.&lt;init&gt;(DFSClient.java:203)
          at org.apache.hadoop.hdfs.DistributedFileSystem.initialize(DistributedFileSystem.java:89)
          at org.apache.hadoop.fs.FileSystem.createFileSystem(FileSystem.java:1386)
          at org.apache.hadoop.fs.FileSystem.access$200(FileSystem.java:66)
          at org.apache.hadoop.fs.FileSystem$Cache.get(FileSystem.java:1404)
          at org.apache.hadoop.fs.FileSystem.get(FileSystem.java:254)
          at org.apache.hadoop.fs.Path.getFileSystem(Path.java:187)
          at org.apache.hadoop.mapred.FileInputFormat.listStatus(FileInputFormat.java:176)
          at org.apache.hadoop.mapred.FileInputFormat.getSplits(FileInputFormat.java:208)
          at org.apache.spark.rdd.HadoopRDD.getPartitions(HadoopRDD.scala:140)
          at org.apache.spark.rdd.RDD$$anonfun$partitions$2.apply(RDD.scala:207)
          at org.apache.spark.rdd.RDD$$anonfun$partitions$2.apply(RDD.scala:205)
          at scala.Option.getOrElse(Option.scala:120)
          at org.apache.spark.rdd.RDD.partitions(RDD.scala:205)
          at org.apache.spark.rdd.MappedRDD.getPartitions(MappedRDD.scala:28)
          at org.apache.spark.rdd.RDD$$anonfun$partitions$2.apply(RDD.scala:207)
          at org.apache.spark.rdd.RDD$$anonfun$partitions$2.apply(RDD.scala:205)
          at scala.Option.getOrElse(Option.scala:120)
          at org.apache.spark.rdd.RDD.partitions(RDD.scala:205)
          at org.apache.spark.SparkContext.runJob(SparkContext.scala:898)
          at org.apache.spark.rdd.RDD.count(RDD.scala:726)
          at MovieRecExp$.main(experiment.scala:30)
          at MovieRecExp.main(experiment.scala)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
          at java.lang.reflect.Method.invoke(Method.java:606)
  [trace] Stack trace suppressed: run last compile:run for the full output.
  java.lang.RuntimeException: Nonzero exit code: 1
          at scala.sys.package$.error(package.scala:27)
  [trace] Stack trace suppressed: run last compile:run for the full output.
  [error] (compile:run) Nonzero exit code: 1
  [error] Total time: 16 s, completed Jun 25, 2014 10:48:40 AM
  </code></pre>