• 姚斌分布式作业一


    首先打开hadoop,

    $ sbin/start-dfs.sh

    $sbin/start-yarn.sh

    执行一下jps 命令看一下.

    ok,各任务都正常。

    上次已经验证过了word count。注意每次生成都需要把上次的输出的文件夹删掉。

    现在开始做hdfs的实验。

    http://hadoop.apache.org/docs/r1.0.4/cn/hdfs_shell.html

    http://blog.csdn.net/bigdatahappy/article/details/10068881

    注意无需重新配置,新建的eclipce mapredruce project 只需要run on hadoop就可以了。

    ---------------------------------------------------------------------------------------------

    作业一的要求是从一个文件动态增加的文件夹中读取文件,上传后删除。附上代码如下:

    import java.io.File;
    import java.io.IOException;
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.FileStatus;
    import org.apache.hadoop.fs.FileSystem;
    import org.apache.hadoop.fs.Path;
    import org.apache.hadoop.fs.FSDataInputStream;
    import org.apache.hadoop.fs.FSDataOutputStream;
    
     
    public class copyFile {
        //initialization
        static Configuration conf = new Configuration();
        static FileSystem hdfs;
        static FileSystem local;
        static {
            conf.set("fs.default.name","hdfs://localhost:9000");
            
            try {
                hdfs = FileSystem.get(conf);
                local = FileSystem.getLocal(conf);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) throws IOException {
        	//Path src = new Path("/home/byshen/HadoopInput/xjbz2.c");
        	//Path dst = new Path("/user/byshen/input");
        	Path inputDir = new Path("/home/byshen/视频");
        	Path acceptDir = new Path("/user/byshen/AcceptFile");
        	hdfs.mkdirs(acceptDir);
        	
        	
        	System.out.println("===== Mkdir ======
    ");
        	
        	FileStatus files[] = local.listStatus(inputDir);
        	FSDataOutputStream out;
        	int i = 0;
        	while(files.length >0) {
        		System.out.println(files[i].getPath().getName());
        		FSDataInputStream in = local.open(files[i].getPath());
        		out = hdfs.create(new Path("/user/byshen/AcceptFile/" + files[i].getPath().getName()));
        		byte buffer[] = new byte[256];
        		int bytesRead = 0;
        		while( (bytesRead = in.read(buffer)) > 0) {
        			out.write(buffer,0,bytesRead);
        		}
        		out.close();
        		in.close();
        		
        		File toDelete = new File("/home/byshen/视频/" + files[i].getPath().getName());
        		
        		toDelete.delete();
        		if(toDelete.exists()) {
        			System.out.println("delete " + files[i].getPath().getName() + "failed");	
        		}
        		files = local.listStatus(inputDir);
        		System.out.println(files.length + " videos remained...");
        	}
        	System.out.println("all transfered success ... ");
        }
    }
    

      

  • 相关阅读:
    oracle sql developer连接信息的保存位置
    (转) Java EE 6无法安装的解决方法
    如何结合使用 Subversion 和 Eclipse
    ASUS P8H61MLE 硬刷激活win7旗舰版
    pb 版本控制
    Subversion Edge by Collabnet 的用户名密码
    (原)导入证书后报:错误应用程序名称: lmadmin.exe,版本: 11.10.0.9,时间戳: 0x4f02e435
    关于Xendesktop的心得
    Eclipse 3.4插件安装方式
    Java创建线程的两个方法
  • 原文地址:https://www.cnblogs.com/shenbingyu/p/4855658.html
Copyright © 2020-2023  润新知