zoukankan      html  css  js  c++  java
  • 搭建hadoop java开发环境

    package hadoopDemo;
    
    import java.io.IOException;
    import java.net.URI;
    import java.net.URISyntaxException;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.FileSystem;
    import org.apache.hadoop.fs.LocatedFileStatus;
    import org.apache.hadoop.fs.Path;
    import org.apache.hadoop.fs.RemoteIterator;
    
    public class Aa {
    
        public static void main(String[] args)  throws Exception{
            Aa a=new Aa();
            
           
        } 
        public void upload(String src,String dst) throws IOException, InterruptedException, URISyntaxException{
            //拿到一个文件系统与客户端一个实例
            FileSystem filesys=FileSystem.get(new URI("hdfs://linux1:9000"),
                    new Configuration(),"root");
            filesys.copyFromLocalFile(new Path(src), new Path(dst));
            filesys.close();
        }
        public void download(String src,String dst) throws IOException, InterruptedException, URISyntaxException{
            FileSystem fileSystem = FileSystem.get(new URI("hdfs://linux1:9000"), new Configuration(), "root");
               fileSystem.copyToLocalFile(new Path(src), new Path(dst));
               fileSystem.close();
        }
        
        public void getAllFile() throws IOException, InterruptedException, URISyntaxException{
            Configuration cfg=new Configuration();
            FileSystem filesys=FileSystem.get(new URI("hdfs://linux1:9000"),cfg,"root");
            RemoteIterator<LocatedFileStatus> listFiles=filesys.listFiles(new Path("/"), true);
            
            while(listFiles.hasNext()){
                LocatedFileStatus next2=listFiles.next();
                String name=next2.getPath().getName();
                System.out.println(next2.getPath()+"  "+name);
                
            }
        }
        
        public void mkdir(String dir) throws IOException, InterruptedException, URISyntaxException{
            Configuration cfg=new Configuration();
            FileSystem filesys=FileSystem.get(new URI("hdfs://linux1:9000"),cfg,"root");
            filesys.mkdirs(new Path(dir));
            filesys.close();
        }
        public void mv(String src,String dst) throws IOException, InterruptedException, URISyntaxException{
            Configuration cfg=new Configuration();
    //        cfg.set("fs.defaultFs", "hdfs://linux1:9000");
            FileSystem filesys=FileSystem.get(new URI("hdfs://linux1:9000"),cfg,"root");
            filesys.moveFromLocalFile(new Path(src), new Path(dst));
            filesys.close();
        }
        public void delete(String path) throws IOException, InterruptedException, URISyntaxException{
            Configuration cfg=new Configuration();
            FileSystem filesys=FileSystem.get(new URI("hdfs://linux1:9000"),cfg,"root");
            filesys.delete(new Path(path), true);
            filesys.close();
        }
    }
    View Code

    1.解压hadoop-2.6.4.tar.gz

    将此bin文件夹与hadoop-2.6.4文件夹中的bin文件夹合并

    将此bin文件夹中的hadoop.dll文件拷贝到C:WindowsSystem32目录中

    配置windows环境变量

    控制面板——>系统——>更改设置——>高级——>环境变量

    新建变量:HADOOP_HOME,路径:解压文件夹位置

    Path后添加:;%HADOOP_HOME%/bin;%HADOOP_HOME%/sbin

    测试生效:

    cmd输入hadoop

    保险起见可以重启电脑

    2.创建user library

    3.添加jar包

    添加完jar包以后进行简单测试:

    package hadoopDemo;
    
    import java.io.IOException;
    import java.net.URI;
    import java.net.URISyntaxException;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.FileSystem;
    import org.apache.hadoop.fs.LocatedFileStatus;
    import org.apache.hadoop.fs.Path;
    import org.apache.hadoop.fs.RemoteIterator;
    
    public class Aa {
    
        public static void main(String[] args)  throws Exception{
            Aa a=new Aa();
            a.mv("e:/c.txt", "/aaa"); 

           a.getAllFile();
         
         a.delete("/aaa");
         
         a.delete("/b.txt");
         }
    public void upload(String src,String dst) throws IOException, InterruptedException, URISyntaxException{ //拿到一个文件系统与客户端一个实例 FileSystem filesys=FileSystem.get(new URI("hdfs://linux1:9000"), new Configuration(),"root"); filesys.copyFromLocalFile(new Path(src), new Path(dst)); filesys.close(); } public void download(String src,String dst) throws IOException, InterruptedException, URISyntaxException{ FileSystem fileSystem = FileSystem.get(new URI("hdfs://linux1:9000"), new Configuration(), "root"); fileSystem.copyToLocalFile(new Path(src), new Path(dst)); fileSystem.close(); } public void getAllFile() throws IOException, InterruptedException, URISyntaxException{ Configuration cfg=new Configuration(); FileSystem filesys=FileSystem.get(new URI("hdfs://linux1:9000"),cfg,"root"); RemoteIterator<LocatedFileStatus> listFiles=filesys.listFiles(new Path("/"), true); while(listFiles.hasNext()){ LocatedFileStatus next2=listFiles.next(); String name=next2.getPath().getName(); System.out.println(next2.getPath()+" "+name); } } public void mkdir(String dir) throws IOException, InterruptedException, URISyntaxException{ Configuration cfg=new Configuration(); FileSystem filesys=FileSystem.get(new URI("hdfs://linux1:9000"),cfg,"root"); filesys.mkdirs(new Path(dir)); filesys.close(); } public void mv(String src,String dst) throws IOException, InterruptedException, URISyntaxException{ Configuration cfg=new Configuration(); // cfg.set("fs.defaultFs", "hdfs://linux1:9000"); FileSystem filesys=FileSystem.get(new URI("hdfs://linux1:9000"),cfg,"root"); filesys.moveFromLocalFile(new Path(src), new Path(dst)); filesys.close(); } public void delete(String path) throws IOException, InterruptedException, URISyntaxException{ Configuration cfg=new Configuration(); FileSystem filesys=FileSystem.get(new URI("hdfs://linux1:9000"),cfg,"root"); filesys.delete(new Path(path), true); filesys.close(); } }
  • 相关阅读:
    170308、oracle查看被锁的表和解锁
    网络安全、Web安全、渗透测试之笔经面经总结(二)
    网络安全、Web安全、渗透测试之笔经面经总结(一)
    Linux 磁盘新增、分区、挂载等
    需求文件requirements.txt的创建及使用
    每天00:00,MySQL定时弹出一个taskeng.exe
    解决 python中 使用tesserocr,File "tesserocr.pyx", line 2401, in tesserocr._tesserocr.image_to_text 报错问题
    ycharm调整代码长度分割线
    jenkins配置用户角色权限,根据不同权限显示视图、Job
    Python图片裁剪实例代码(如头像裁剪)
  • 原文地址:https://www.cnblogs.com/xiaoaofengyue/p/8117912.html
Copyright © 2011-2022 走看看