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()。小有所得。

 

posted @ 2021-11-21 21:43  anyiya  阅读(115)  评论(0编辑  收藏  举报