Hadoop 2
java API 对HDFS的几个常用文件操作:
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; import org.junit.Before; import org.junit.Test; public class Demo1 { public static void main(String[] args) { /** * 操作HDFS使用的一个类是FileSystem * 不是只能读取HDFS上的数据,根据不同子类,能读取不同平台上的数据 * 如:本机文件,ftp服务器上的文件,S3上的数据,HDFS上的数据 */ try{ //读取哪个平台上的数据由协议名称来判断 URI uri = new URI("hdfs://dyc:9000"); Configuration conf = new Configuration(); FileSystem fileSystem = FileSystem.get(uri, conf); Path path = new Path("/core-site.xml"); FSDataInputStream fsDataInputStream = fileSystem.open(path); int c; while ((c=fsDataInputStream.read())!=-1){ System.out.print((char)c); } fsDataInputStream.close(); }catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } FileSystem fileSystem; URI uri; Configuration conf; @Before public void init(){ try{ //读取哪个平台上的数据由协议名称来判断 uri = new URI("hdfs://dyc:9000"); conf = new Configuration(); fileSystem = FileSystem.get(uri, conf); }catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } /** * 创建txt等文件 */ @Test public void createFile(){ try { Path path = new Path("/a/c.txt"); if(fileSystem.exists(path)){ System.out.println("已存在此文件"); } else{ FSDataOutputStream outputStream = fileSystem.create(path); //write写文件的内容 outputStream.write(null); outputStream.close(); fileSystem.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 创建文件夹 * 写的时候会出现权限问题,有三种方法 * 1.将hadoop的权限验证关闭 * 在$hadoop-2.6.0/etc/hadoop/hdfs-site.xml中添加 * <property> * <name>dfs.permissions.enabled</name> * <value>false</value> * </property> * 2.把HDFS根目录权限修改为777 * * 3.伪造hadoop用户 */ @Test public void mkdir(){ try { Path path = new Path("/a/d/t"); if(fileSystem.exists(path)){ System.out.println("已存在此目录"); } else{ fileSystem.mkdirs(path); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 删除文件夹 */ @Test public void rmdir(){ try { Path path = new Path("/a"); fileSystem.delete(path, true); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 打开文件 */ @Test public void open2(){ try { Path path = new Path("/core-site.xml"); FSDataInputStream fsDataInputStream = fileSystem.open(path); IOUtils.copyBytes(fsDataInputStream, System.out, 1000); fsDataInputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 查看目录 * */ @Test public void cat(){ try { fileSystem = FileSystem.get(uri, conf); FileStatus fileList[] = fileSystem.listStatus(new Path("/a")); for(int i = 0 ; i < fileList.length ; i++){ System.out.println("name:"+fileList[i].getPath().getName()+" size:"+fileList[i].getLen()); } fileSystem.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 重命名或移动文件 * 当重命名的文件已存在时会将前一个文件移动到重命名后的文件内 */ @Test public void rename(){ try{ fileSystem = FileSystem.get(uri, conf); Path oldpath = new Path("/a/ddd"); Path newPath = new Path("/a/c"); fileSystem.rename(oldpath, newPath); fileSystem.close(); }catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } /** * 上传文件 * 注意上传文件的扩展名 */ @Test public void upload(){ try{ Path localPath = new Path("F:/local"); Path upPath = new Path("/a/upload"); if(!fileSystem.exists(upPath)){ fileSystem.mkdirs(upPath); } fileSystem.copyFromLocalFile(false, localPath, upPath); fileSystem.close(); }catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } /** * 下载文件 */ @Test public void downLoad(){ try{ //此处下载地址即为上传地址 Path download = new Path("/a/upload/"); Path local = new Path("F:/local"); //第四个参数表示 是否用原生的文件系统作文本地文件系统。 fileSystem.copyToLocalFile(false,download, local,true); fileSystem.close(); }catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } }