hadoop編程遇到的jvm問題爲內存不夠的解決辦法

在ubuntu系統下開發hadoop程序時,遇到幾個問題,小結如下。


問題:內存不足,報錯:There is insufficient memory for the Java Runtime Environment to continue

解決方法如下:

1、使用進程查看命令:ps -e | grep java。

然後刪除過多的java進程,釋放出內存資源。這種方法治標。

打開系統的限制文件:vi  /etc/security/limits.conf,在文件最後添加:* - nofile 278528   保存(已經設置得夠大了)

使用命令查看open files數值:vim /etc/security/limits.conf,看到open files.(這個方法沒試成功,修改後查看open files 還是1024,也許需要重啓系統。)

詳見參考文章1.

還有說是因爲java設置的最大可用內存,不夠maven使用,所以出現警告(這種解釋最靠譜)。

對應的解決辦法是,設置/etc/profile文件中maven的opts:export MAVEN_OPTS="-Xms256m -Xmx512m"

2、測試mapreduce程序時,設置input,output文件位置如下代碼。

3、輸出文件output不能存在,程序自動刪除output文件代碼如下。

	public static void main(String[] args) throws Exception {
		Configuration conf = new Configuration();
		// String[] otherArgs = new GenericOptionsParser(conf,
		// args).getRemainingArgs();
		String[] otherArgs = new String[] { "input", "output" }; /* 直接設置輸入參數 */
		if (otherArgs.length != 2) {
			System.err.println("Usage: wordcount <in>  <out>");
			System.exit(2);
		}

		/* 刪除輸出目錄 */
		Path outputPath = new Path(otherArgs[1]);
		outputPath.getFileSystem(conf).delete(outputPath, true);
		
		Job job = Job.getInstance(conf, "word count");
		job.setJarByClass(WordCount.class);
		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(otherArgs[0]));
		FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));	//輸出在本地
//		FileOutputFormat.setOutputPath(job, new Path("output06"));		//輸出爲HDFS?
		System.exit(job.waitForCompletion(true) ? 0 : 1);
	}

4、運行程序,能夠在本工程目錄下,看到output文件夾。

文件夾下的文件即是程序運行後的結果,如下圖。

5、運行通過後,即可將程序打成jar包。

將打包後的jar放在某個位置,假設名字爲×××.jar。

調用./bin/hadoop jar ×××.jar WordCount input(HDFS input文件的位置) output(HDFS輸出文件的位置)

運行即可的到結果。

6、用hadoop eclipse插件刷新後,查看運行結果。

也可以將結果文件通過hadoop文件下載到本地查看,或保存。


補充說明:

我的程序在本地運行很好。

可始終在hadoop eclipse插件上,看不到hdfs上文件的更新。

如何能使用hadoop eclipse插件,調試、運行源程序,同事在插件上查看到結果,後一篇文章再解決。


參考文章

1、http://www.linuxidc.com/Linux/2013-07/86954.htm

2、http://hadoop.apache.org/docs/current/hadoop-mapreduce-client/hadoop-mapreduce-client-core/MapReduceTutorial.html(hadoop官方說明)

發佈了139 篇原創文章 · 獲贊 19 · 訪問量 20萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章