zoukankan      html  css  js  c++  java
  • Hadoop_第一次作业

    很久没有写随笔了,水两篇...见谅。

    (1)从键盘输入文件名(设:"/abc/a.txt"),在Hadoop中对应的文件中,完成写操作:

    从键盘输入若干行字符,写入文件a.txt

    遇到EndOfFile时,结束键盘输入

     

    package hadoop;
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.FileSystem;
    import org.apache.hadoop.fs.Path;
    import org.apache.hadoop.fs.FSDataOutputStream;
    import org.apache.log4j.BasicConfigurator;
    
    import java.util.Date;
    import java.util.Scanner;
    public class Ljh_work1 {
    
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		BasicConfigurator.configure();
    		try{
    			Configuration conf = new Configuration();
    			conf.set("fs.defaultFS","hdfs://192.168.200.200:9000");
    			conf.set("fs.hdfs.impl","org.apache.hadoop.hdfs.DistributedFileSystem");
    			FileSystem fs=FileSystem.get(conf);
    			String filename=null;
    			Scanner sc=new Scanner(System.in);
    			filename=sc.nextLine();
    			FSDataOutputStream os=fs.create(new Path(filename));
    			String txt=null;
    			while(sc.hasNext()){
    				txt=sc.nextLine()+"\n";
    				byte[] buff=txt.getBytes();
    				os.write(buff,0,buff.length);
    			}
    			System.out.println(filename+"文件上传成功");
    			os.close();
    			fs.close();
    		}catch(Exception e){
    			e.printStackTrace();
    		}
    		Date date=new Date();
            System.out.print(date);
    	}
    }
    

      

     

    (2)从键盘输入文件名(设:"/abc/a.txt"),将文件全部内容读出,在显示器上显示。

    package hadoop;
    
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.FSDataInputStream;
    import org.apache.hadoop.fs.FileSystem;
    import org.apache.hadoop.fs.Path;
    
    import java.util.Date;
    import java.util.Scanner;
    public class Ljh_work2 {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
            try {
                Configuration conf = new Configuration();
                conf.set("fs.defaultFS","hdfs://192.168.200.200:9000"); 
                conf.set("fs.hdfs.impl","org.apache.hadoop.hdfs.DistributedFileSystem");
                FileSystem fs = FileSystem.get(conf);
    			Scanner cin=new Scanner(System.in);
    			String name=cin.nextLine();
                Path file = new Path(name); 
                FSDataInputStream getIt = fs.open(file);
                BufferedReader d = new BufferedReader(new InputStreamReader(getIt));
                System.out.println("文件读入成功!文件内容如下:");
                String content; //读取文件一行
                while((content = d.readLine())!=null){
                	System.out.println(content);
                }
                d.close(); //关闭文件
                fs.close(); //关闭hdfs
            } catch (Exception e) {
                e.printStackTrace();
        }
            Date date=new Date();
            System.out.print(date);
            
    	}
    
    }
    

      

    大概就是很久没写过Java了,Scanner都不会用了。不知道那个黄色提示应该怎么消掉....然后就是Java的判断EOF,hasNext()。小有所得。

  • 相关阅读:
    hadoop2.0.x【3】--Yarn Commands
    hadoop2.0.x【2】--Apache Hadoop MapReduce
    hadoop2.0.x【1】--Apache Hadoop NextGen MapReduce (YARN)--翻译与分析
    基于jquery的提交、编辑页面js编写框架
    UIStatusBarStyle PreferredStatusBarStyle does not work on iOS 7
    Protecting resources in iPhone and iPad apps
    使用Xcode 5创建Cocoa Touch Static Library(静态库)
    Example of how to implement a view-based source list (NSOutlineView) using Cocoa Bindings
    转载:收费版APP三年总结(个人经验+数据图分享)
    OC 导入类 #import和@class 区别
  • 原文地址:https://www.cnblogs.com/thx2199/p/15586076.html
Copyright © 2011-2022 走看看