title: How to write to HDFS using Scala
body: <p>I am learning Scala and i need to write a custom file to HDFS. I have my own HDFS running on a Cloudera image using vmware fusion on my laptop. </p>
  
  <p>This is my actual code:</p>
  
  <pre><code>package org.glassfish.samples
  
  import org.apache.hadoop.conf.Configuration;
  import org.apache.hadoop.fs.FileSystem;
  import org.apache.hadoop.fs.Path;
  import java.io.PrintWriter;
  
  /**
  * @author ${user.name}
  */
  object App {
  
  def main(args : Array[String]) {
  println( "Trying to write to HDFS..." )
  val conf = new Configuration()
  val fs= FileSystem.get(conf)
  val output = fs.create(new Path("hdfs://quickstart.cloudera:8020/tmp/mySample.txt"))
  val writer = new PrintWriter(output)
  try {
      writer.write("this is a test") 
      writer.write("\n")
  }
  finally {
      writer.close()
  }
  print("Done!")
  }
  
  }
  </code></pre>
  
  <p>And i am getting this exception:</p>
  
  <pre><code>Caused by: java.lang.IllegalArgumentException: Wrong FS: hdfs://quickstart.cloudera:8020/tmp, expected: file:///
  at org.apache.hadoop.fs.FileSystem.checkPath(FileSystem.java:645)
  at org.apache.hadoop.fs.RawLocalFileSystem.pathToFile(RawLocalFileSystem.java:80)
  at org.apache.hadoop.fs.RawLocalFileSystem.mkdirs(RawLocalFileSystem.java:414)
  at org.apache.hadoop.fs.ChecksumFileSystem.mkdirs(ChecksumFileSystem.java:588)
  at org.apache.hadoop.fs.ChecksumFileSystem.create(ChecksumFileSystem.java:439)
  at org.apache.hadoop.fs.ChecksumFileSystem.create(ChecksumFileSystem.java:426)
  at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:908)
  at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:889)
  at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:786)
  at org.apache.hadoop.fs.FileSystem.create(FileSystem.java:775)
  at org.glassfish.samples.App$.main(App.scala:19)
  at org.glassfish.samples.App.main(App.scala)
  ... 6 more
  </code></pre>
  
  <p>I can access hdfs using the terminal and Hue</p>
  
  <pre><code>[cloudera@quickstart ~]$ hdfs dfs -ls /tmp
  Found 3 items
  drwxr-xr-x   - hdfs     supergroup          0 2015-06-09 17:54 /tmp/hadoop-yarn
  drwx-wx-wx   - hive     supergroup          0 2015-08-17 15:24 /tmp/hive
  drwxr-xr-x   - cloudera supergroup          0 2015-08-17 16:50 /tmp/labdata
  </code></pre>
  
  <p>this is my <a href="http://pastebin.com/HgA5eNYD" rel="noreferrer" title="pom.xml">pom.xml</a></p>
  
  <p>I ran the project using the command:</p>
  
  <p>mvn clean package scala:run</p>
  
  <p>What do i am doing wrong? thank you in advance!</p>
  
  <p>EDIT after @jeroenr advice</p>
  
  <p>This is actual code:</p>
  
  <pre><code>package org.glassfish.samples
  
  import org.apache.hadoop.conf.Configuration;
  import org.apache.hadoop.fs.FileSystem;
  import org.apache.hadoop.fs.Path;
  import java.io.PrintWriter;
  
  /**
  * @author ${user.name}
  */
  object App {
  
  //def foo(x : Array[String]) = x.foldLeft("")((a,b) =&gt; a + b)
  
  def main(args : Array[String]) {
  println( "Trying to write to HDFS..." )
  val conf = new Configuration()
  //conf.set("fs.defaultFS", "hdfs://quickstart.cloudera:8020")
  conf.set("fs.defaultFS", "hdfs://192.168.30.147:8020")
  val fs= FileSystem.get(conf)
  val output = fs.create(new Path("/tmp/mySample.txt"))
  val writer = new PrintWriter(output)
  try {
      writer.write("this is a test") 
      writer.write("\n")
  }
  finally {
      writer.close()
      println("Closed!")
  }
  println("Done!")
  }
  
  }
  </code></pre>