java对Hadoop进行文件的操作(二)

本次主要是简单的对Hadoop中HDFS中文件的操作,可自行先添加文件,或者直接上传文件操作实验。

去不代码如下:

package hadoop1;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FsUrlStreamHandlerFactory;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;

public class hdfs {

    public static void main(String[] args) throws MalformedURLException {
        /*URL url=new URL("http://www.baidu.com");//用URL判断路径
        try {
            InputStream in=url.openStream();//路径打开一个输入流文件
             IOUtils.copyBytes(in, System.out, 4096,true);//将文件内容拷贝出来
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }*/
        
        /*
        URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());//用URL的方法先建立一个url的工厂
        URL url=new URL("hdfs://192.168.83.100:9000/hello.txt");//获取我们想要文件的路径
        try {
            InputStream in=url.openStream();//文件打开
             IOUtils.copyBytes(in, System.out, 4096,true);//文件内容输出
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }*/
        
        
        
        Configuration conf =new Configuration();//configuration的对象建立
        conf.set("fs.defaultFS", "hdfs://192.168.83.100:9000");//将要访问的路径放入
        try {
            FileSystem fileSystem=FileSystem.get(conf);//获取路径信息
            
            /*boolean success=fileSystem.mkdirs(new Path("/asb"));//判断是否创建一个目录文件
            System.out.println(success);
            
            success=fileSystem.exists(new Path("\\hello.txt"));//判断是否存在文件
            System.out.println(success);
            
            success=fileSystem.delete(new Path("/asb"));//删除文件
            System.out.println(success);
            
            success=fileSystem.exists(new Path("/asb"));
            System.out.println(success);*/
            
            /*FSDataOutputStream out =fileSystem.create(new Path("/test.data"),true);//重新创建一个data目录
            FileInputStream fis =new FileInputStream("C:\\Users\\ZB\\Desktop\\data\\Hamlet.txt");//将桌面文件读入
            IOUtils.copyBytes(fis, out, 4096,true);*///读取放入的文件
            
            /*FSDataOutputStream out =fileSystem.create(new Path("/test.data"),true);//手动添加
            FileInputStream fis =new FileInputStream("C:\\Users\\ZB\\Desktop\\data\\Hamlet.txt");
            byte[] buf=new byte[4096];//一个字节一个字节的上传
            int len=fis.read(buf);
            while(len!=-1) {
                out.write(buf,0,len);
                len=fis.read(buf);
            }
            fis.close();
            out.close();*/
            
            FileStatus[] status=fileSystem.listStatus(new Path("/"));//查找存在根目录下的文件
            for(FileStatus status2 : status) {
                System.out.println(status2.getPath());//获取地址
                System.out.println(status2.getPermission());//获取认证信息
                System.out.println(status2.getReplication());//获取响应
                
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         
        
        
                
            
    }
}

 

转载于:https://www.cnblogs.com/qianshuixianyu/p/9319063.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值