zoukankan      html  css  js  c++  java
  • HDFS的Java API 对文件的操作

    在本次操作中所用到的命令

    1.首先启动HDFS

    $HADOOP_HOME/sbin/start-dfs.sh

    2.关防火墙

    切换到root用户,执行service iptables stop

    3.拷贝文件到HDFS

    bin/hadoop fs -put 本地 HDFS

    4.查看HDFS根目录的文件

    bin/hadoop fs -ls /

    1.新建Java项目,导入Hadoop相关jar包。

    在hadoop解压包中的hadoop-2.6.0sharehadoopcommon目录下红色标注的文件全部拷贝

    在hadoop-2.6.0sharehadoophdfs目录下红色标注的文件全部拷贝

    然后在Java项目中构建配置路径

    2.编写代码

        FileSystem fileSystem;
        
        /*
         * 初始化
         */
        @Before
        public void init() throws Exception{
            //读取数据由平台上的协议确定
            URI uri = new URI("hdfs://192.168.*.*:9000");
            Configuration conf = new Configuration();
            fileSystem = FileSystem.get(uri, conf);
        }
        
        /*
         * 查看目录
         */
        @Test
        public void Catalog() throws Exception{
            Path path = new Path("/poker");
            FileStatus fileStatus = fileSystem.getFileStatus(path);
            System.out.println("*************************************");    
            System.out.println("文件根目录: "+fileStatus.getPath()); 
            System.out.println("这文件目录为:");
            for(FileStatus fs : fileSystem.listStatus(path)){ 
                System.out.println(fs.getPath()); 
            } 
        }
        
        /*
         * 浏览文件
         */
        @Test
        public void look() throws Exception{
            Path path = new Path("/core-site.xml");
            FSDataInputStream fsDataInputStream = fileSystem.open(path);
            System.out.println("*************************************");
            System.out.println("浏览文件:");
            int c;
            while((c = fsDataInputStream.read()) != -1){
                System.out.print((char)c);
                }
            fsDataInputStream.close();
        }
        
        /*
         * 上传文件
         */
        @Test
        public void upload() throws Exception{
            Path srcPath = new Path("C:/Users/Administrator/Desktop/hadoop/hadoop.txt");  
            Path dstPath = new Path("/");  
            fileSystem.copyFromLocalFile(false, srcPath, dstPath);
            fileSystem.close(); 
            System.out.println("*************************************");
            System.out.println("上传成功!");
        }
        
        /*
         * 下载文件
         */
        @Test
        public void download() throws Exception{
            InputStream in = fileSystem.open(new Path("/hadoop.txt"));  
            OutputStream out = new FileOutputStream("E://hadoop.txt");  
            IOUtils.copyBytes(in, out, 4096, true);  
        }
        
        /*
         * 删除文件
         */
        @Test
        public void delete() throws Exception{
            Path path = new Path("hdfs://192.168.*.*:9000/hadoop.txt");
            fileSystem.delete(path,true);
            System.out.println("*************************************");
            System.out.println("删除成功!");
        }

    3.运行时发现出现用户没有权限的错误。

    解决方法:

    1.修改HDFS根目录的权限

    2.把Hadoop权限验证关闭,把hadoop.dll文件放到C:/windows/system32中,然后修改hdfs-site.xml文件,把验证关闭

    <property>

         <name>dfs.permissions</name>

         <value>false</value>

    </property>

    3.伪造用户 -DHADOOP_USER_NAME=用户名

  • 相关阅读:
    R 读取xls/xlsx文件
    网页免费转换为可编辑的PDF
    Python: NumPy, Pandas学习资料
    鱼油资料
    Activity的四种启动模式和onNewIntent()
    Android Service、IntentService,Service和组件间通信
    Activity生命周期
    Node.js学习起步
    Android 技能图谱学习路线
    Blog
  • 原文地址:https://www.cnblogs.com/langgj/p/6595756.html
Copyright © 2011-2022 走看看