The
Simply uses an example to use the HDFs Java interface.
Package cn.zhoucy.Hadoop;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.net.URI;
Import java.net.URISyntaxException;
Import org.apache.hadoop.conf.Configuration;
Import Org.apache.hadoop.fs.FSDataOutputStream;
Import Org.apache.hadoop.fs.FileSystem;
Import Org.apache.hadoop.fs.Path;
Import Org.apache.hadoop.io.IOUtils;
Import Org.junit.Before;
Import Org.junit.Test;
public class Hdfsdemo {private filesystem fs = null; @Before public void Init () throws IOException, URISyntaxException, interruptedexception{fs = Filesystem.get (n
EW URI ("hdfs://123.206.xxx.xxx:9000"), New Configuration (), "root"); @Test public void Testdel () throws IllegalArgumentException, ioexception{boolean flag = Fs.delete (new
Path ("/words.txt"), true);
SYSTEM.OUT.PRINTLN (flag); @Test public void Testmkdir () throws IllegalArgumentException, IOException{Boolean flag = Fs.mkdirs (new Path ("/itcast88888888"));
SYSTEM.OUT.PRINTLN (flag); @Test public void Testupload () throws IllegalArgumentException, ioexception{fsdataoutputstream out = FS
. Create (New Path ("/words.txt"));
FileInputStream in = new FileInputStream (New File ("E:/w.txt"));
Ioutils.copybytes (in, out, 2048, true);
public static void Main (string[] args) throws Exception {Configuration conf = new Configuration ();
Conf.set ("Fs.defaultfs", "hdfs://123.206.xxx.xxx:9000");
Conf.set ("Fs.hdfs.impl", "Org.apache.hadoop.hdfs.DistributedFileSystem");
Tool class filesystem FileSystem FS = Filesystem.get (conf);
System.out.println ("Meiwentoi");
InputStream in = Fs.open (new Path ("/woo");//download, download from HDFs System.out.println ("Chucuo");
FileOutputStream out = new FileOutputStream (New File ("/home/woo"));
Ioutils.copybytes (in, out, 2048, true);}
}
This use case test is to store downloaded files from HDFs to the Linux file system. Under pseudo distributed deployments, you can run code directly in Eclipse, test with JUnit, and test the download method inside the main method. In the case of distributed deployment, there will be an error in the run, which may be configured incorrectly, and can be run as a jar pack on the server. Here is the way to play the jar that can be run, the name is called Hadoop. Use Java-jar Hadoop.jar on the server to complete the download function.