eclipse 上传文件到hadoop

其实很简单,只要配置正确,就行

 直接给代码吧

package com.younglibin.hadoop;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class HDFSystem {
	public static void uploadLocalFile2HDFS(Configuration config,
			String localFile, String remoteFile) throws IOException {
		FileSystem hdfs = FileSystem.get(config);
		Path src = new Path(localFile);
		Path dst = new Path(remoteFile);

		hdfs.copyFromLocalFile(src, dst);

		hdfs.close();
	}

	public static void createNewHDFSFile(String toCreateFilePath, String content)
			throws IOException {
		Configuration config = new Configuration();
		FileSystem hdfs = FileSystem.get(config);

		FSDataOutputStream os = hdfs.create(new Path(toCreateFilePath));

		os.write(content.getBytes("UTF-8"));

		os.close();

		hdfs.close();
	}

	public static boolean deleteHDFSFile(String dst) throws IOException {
		Configuration config = new Configuration();
		FileSystem hdfs = FileSystem.get(config);

		Path path = new Path(dst);
		boolean isDeleted = hdfs.delete(path);

		hdfs.close();

		return isDeleted;
	}

	public static byte[] readHDFSFile(String dst) throws Exception {
		Configuration conf = new Configuration();
		FileSystem fs = FileSystem.get(conf);

		// check if the file exists
		Path path = new Path(dst);
		if (fs.exists(path)) {
			FSDataInputStream is = fs.open(path);
			// get the file info to create the buffer
			FileStatus stat = fs.getFileStatus(path);

			// create the buffer
			byte[] buffer = new byte[Integer.parseInt(String.valueOf(stat
					.getLen()))];
			is.readFully(0, buffer);

			is.close();
			fs.close();

			return buffer;
		} else {
			throw new Exception("the file is not found .");
		}
	}
}

 注意上传文件的时候添加了  conf.set("fs.default.name", "hdfs://172.16.236.11:9000");  这句话 ,否则出现  找不到文件的问题 参考:http://www.cnblogs.com/bingofworld/archive/2013/06/09/3129299.html

 Hadoop HDFS Wrong FS: hdfs:/ expected file:///

 看代码吧:

public static void main(String[] args) throws Exception {

		File jarFile = EJob.createTempJar("bin");
		EJob.addClasspath("/home/libin/software/hadoop/hadoop-1.2.1/conf");
		ClassLoader classLoader = EJob.getClassLoader();
		Thread.currentThread().setContextClassLoader(classLoader);

		Configuration conf = new Configuration();
		conf.set("mapred.job.tracker", "172.16.236.11:9001");
		conf.set("fs.default.name", "hdfs://172.16.236.11:9000");
		String fileName = "local";
		HDFSystem.uploadLocalFile2HDFS(conf,
				new File(fileName).getAbsolutePath(),
				"hdfs://172.16.236.11:9000/user/libin/input/" + fileName);
		args = new String[] { "/user/libin/input/" + fileName,
				"/user/libin/output/wordcount" + System.currentTimeMillis() };

		String[] otherArgs = new GenericOptionsParser(conf, args)
				.getRemainingArgs();
		if (otherArgs.length != 2) {
			System.err.println("Usage: wordcount <in> <out>");
			System.exit(2);
		}
		Job job = new Job(conf, "word count");
		job.setJarByClass(WordCount.class);
		((JobConf) job.getConfiguration()).setJar(jarFile.toString());
		job.setMapperClass(TokenizerMapper.class);
		job.setCombinerClass(IntSumReducer.class);
		job.setReducerClass(IntSumReducer.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);
		FileInputFormat.addInputPath(job, new Path("hdfs://172.16.236.11:9000"
				+ File.separator + otherArgs[0]));
		FileOutputFormat.setOutputPath(job, new Path(
				"hdfs://172.16.236.11:9000" + File.separator + otherArgs[1]));
		System.exit(job.waitForCompletion(true) ? 0 : 1);
	}

 转自:http://younglibin.iteye.com/blog/1925109

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值