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 呢 我们再来看一下
写入也是成功的