//方法一通过一个类IOUtils来实现
写文件 create 读取文件 open 删除文件delete
创建目录 mkdirs 删除文件或目录 delete 列出目录的内容 listStatus 显示文件系统的目录和文件的元数据信息 getFileStatus
package hdfs; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import org.apache.hadoop.fs.FsUrlStreamHandlerFactory; import org.apache.hadoop.io.IOUtils; public class App1 { /** * @param args */ //首先定义一个连接hadoop的URI(标识符和URL做区别) public static final String HDFS_PATH="hdfs://hadoop:9000/hello"; public static void main(String[] args) throws Exception { // TODO Auto-generated method stub //url不认识hdfs://所以需要解析器 URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory()); //用静态类IOUtils.copyBytes() URL url = new URL(HDFS_PATH); InputStream in = url.openStream(); IOUtils.copyBytes(in, System.out , 1024, true ); } }
//方法二用过类FileSystem中方法.mkdir .create .open get package hdfs; import java.io.FileInputStream; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataInputStream; import org.apache.hadoop.fs.FSDataOutputStream; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.fs.FileSystemTestHelper.fileType; import org.apache.hadoop.io.IOUtils; public class App2 { public static final String HDFS_PATH="hdfs://hadoop:9000"; public static final String DIR_PATH="/d1000"; public static final String FILE_PATH="/d1000/f1000"; public static void main(String[] args) throws Exception, URISyntaxException { /* final FileSystem fileSystem = FileSystem.get(new URI(HDFS_PATH),new Configuration() ); //创建文件mkdir fileSystem.mkdirs(new Path(DIR_PATH)); //上传文件create FSDataOutputStream out = fileSystem.create(new Path(FILE_PATH)); FileInputStream in= new FileInputStream("C:/Users/Administrator/Desktop/01.txt"); IOUtils.copyBytes(in,out,1024,true); //下载文件open final FSDataInputStream in2= fileSystem.open(new Path(FILE_PATH)); IOUtils.copyBytes(in2, System.out, 1024,true ); //删除文件delete fileSystem.delete(new Path(FILE_PATH), true);*/ FileSystem fileSystem = FileSystem.get(new URI(HDFS_PATH),new Configuration()); //mkdir fileSystem.mkdirs(new Path(DIR_PATH)); //上传create创造目标通道 FSDataOutputStream out = fileSystem.create(new Path(FILE_PATH)); FileInputStream in = new FileInputStream("C:/Users/Administrator/Desktop/01.txt"); IOUtils .copyBytes(in,out , 1024, true); //下载文件open一个通道 FSDataInputStream in2 = fileSystem.open(new Path(FILE_PATH)); IOUtils.copyBytes(in2, System.out ,1024,true); //删除delete fileSystem.delete(new Path(DIR_PATH),true ); } }