作者:虚伪小仔 | 来源:互联网 | 2023-10-11 15:34
1、首先启动hadoop
start-all.sh
2、建立maven工程
2.1 编辑pom.xml文件
1
3 4.0.0
4
5 com.js
6 ss
7 0.0.1-SNAPSHOT
8
jar
9
10 ss
11 http://maven.apache.org
12
13
14
UTF-8
15
16 3.1.1
17
18
19
20
21 junit
22 junit
23 3.8.1
24 test
25
26
27
28
29 jdk.tools
30 jdk.tools
31 1.8
32 system
33 ${JAVA_HOME}/lib/tools.jar
34
35
36
37
38
39 org.apache.hadoop
40 hadoop-hdfs
41 ${hadoop.version}
42
43
44
45 org.apache.hadoop
46 hadoop-client
47 ${hadoop.version}
48
49
50
51 org.apache.hadoop
52 hadoop-common
53 ${hadoop.version}
54
55
56
57
58
59
2.2 新建一个类文件
1 package com.js;
2
3 import java.io.FileOutputStream;
4 import java.io.IOException;
5 import java.net.URI;
6 import java.net.URISyntaxException;
7
8 import org.apache.hadoop.conf.Configuration;
9 import org.apache.hadoop.fs.FSDataInputStream;
10 import org.apache.hadoop.fs.FileSystem;
11 import org.apache.hadoop.fs.Path;
12 import org.apache.hadoop.io.IOUtils;
13
14 /**
15 * Hello world!
16 *
17 */
18 public class App
19 {
20 public static void main( String[] args ) throws IOException, InterruptedException, URISyntaxException {
21 System.out.println( "Hello World!" );
22 System.out.println("hello world");
23
24
25 Configuration cOnf= new Configuration();
26 // 设置hadoop的文件系统格式
27 conf.set("fs.defaultFS", "hdfs://localhost:9000");
28
29 FileSystem fs = FileSystem.get(conf);
30
31 Path src = new Path("/wordcount/input");
32 FSDataInputStream in = fs.open(src);
33
34 FileOutputStream os = new FileOutputStream("./out");
35 IOUtils.copyBytes(in, os, conf, true);
36
37 }
38 }
在当前目录下,会出现out文件