`
caoruntao
  • 浏览: 468061 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

运行Hadoop权威指南中的例子:3.5.3:FileCopyWithProgress

阅读更多

1、编写代码:

package crt.hadoop.test;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.util.Progressable;

public class FileCopyWithProgress {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub
		String localSrc = args[0];
		String dst = args[1];
		InputStream in = new BufferedInputStream(new FileInputStream(localSrc));
		Configuration conf = new Configuration();
		FileSystem fs = FileSystem.get(URI.create(dst), conf);
		OutputStream out = fs.create(new Path(dst), new Progressable(){
			public void progress(){
				System.out.print(".");
			}
		});
		
		IOUtils.copyBytes(in, out, 4096, true);
	}

}

 

2、复制FileCopyWithProgress.java到$HADOOP_HOME/build/classes

 

3、建测试文件

$ cd  /home/hadoop/workspace/

$ mkdir  writedata

$ cd  writedata

$ mkdir  input

$ cd  input

$ cp  -a  /home/hadoop/workspace/writedata/crt/hadoop/test/FileCopyWithProgress.java  ./writdatatestinput.txt

 

4、编译

$  javac -classpath $HADOOP_HOME/hadoop-common-0.21.0.jar 

     $HADOOP_HOME/build/classes/crt/hadoop/test/FileCopyWithProgress.java

 

5、运行

$  export  HADOOP_CLASSPATH=build/classes

$  cd /home/hadoop/workspace/writedata/input

$  hadoop  crt.hadoop.test.FileCopyWithProgress  writedatatestinput.txt  

     hdfs://hadoop-namenode:9001/tmp/writedatatestinput.txt

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics