Java examples for Big Data:Hadoop
Use apache hadoop file system
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.net.URI; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataInputStream; import org.apache.hadoop.fs.FSDataOutputStream; import org.apache.hadoop.fs.FileStatus; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IOUtils; public class FileSystemTest { public static void main(String[] args) throws Exception { System.setProperty("HADOOP_USER_NAME", "root"); System.setProperty("HADOOP_USER_PASSWORD", "neusoft"); FileSystem fileSystem = FileSystem.newInstance(new URI( "hdfs://neusoft-master:9000"), new Configuration()); fileSystem.delete(new Path("/a.jpg"), false); }//from w w w . ja v a2s . c o m private static void show(FileSystem fileSystem) throws IOException, FileNotFoundException { FSDataInputStream in = fileSystem.open(new Path( "/test.jpg")); IOUtils.copyBytes( in, new FileOutputStream( "C:\\Users\\test\\Desktop\\___test.jpg"), 1024, true); } private static void put(FileSystem fileSystem) throws IOException, FileNotFoundException { FSDataOutputStream out = fileSystem.create(new Path( "/test.jpg")); IOUtils.copyBytes( new FileInputStream( "C:\\Users\\test\\Desktop\\test.jpg"), out, 1024, true); } private static void ls(FileSystem fileSystem) throws Exception { FileStatus[] listStatus = fileSystem.listStatus(new Path("/")); for (FileStatus fileStatus : listStatus) { System.out.println(fileStatus); } } }