1. 程式人生 > >eclipse maven 搭建hadoop開發環境

eclipse maven 搭建hadoop開發環境

1 建立一個maven專案  

2 引入 hadoop依賴包  

 <properties>
  	<hadoop.version>2.7.3</hadoop.version>
  </properties>
  <dependencies>
	<dependency>
	    <groupId>org.apache.hadoop</groupId>
	    <artifactId>hadoop-common</artifactId>
	    <version>${hadoop.version}</version>
	</dependency>
	<dependency>
	    <groupId>org.apache.hadoop</groupId>
	    <artifactId>hadoop-client</artifactId>
	    <version>${hadoop.version}</version>
	</dependency>
	
  	<dependency>  
	    <groupId>jdk.tools</groupId>  
	    <artifactId>jdk.tools</artifactId>  
	    <version>1.8</version>  
	    <scope>system</scope>  
	    <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>  
</dependency>  
  </dependencies>

3  建立一個類來測試一下   

@Test
	public void create(){
		Configuration conf=new Configuration();
		Path path=new Path("hdfs://server1:8020/file/wy/abcde");
		conf.set("fs.defaultFS", "hdfs://server1:8020");
		FileSystem fileSystem;
		try {
			fileSystem = path.getFileSystem(conf);
			Writer file=SequenceFile.createWriter(fileSystem, conf, path, IntWritable.class, Text.class); 
			for(int i=1;i<=100;i++){
				IntWritable key=new IntWritable(i);
				Text value=new Text("wy"+i);
				file.append(key, value);
			}
			file.close();
			System.out.println("OK");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

4 執行之後我們看一下   

我們可以看到 檔案已經建立了   那麼有沒有寫入key value 呢    我們再來看一下  

寫入也是成功的