javaAPI写入HDFS文件。
package com.twq.hdfs;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
/**
* 需要执行
* hadoop fs -chmod 757 hdfs://master:9999/user/hadoop-twq/cmd/
*/
public class FileWriter {
public static void main(String[] args) throws IOException {
String content = "this is an example";
String dest = "hdfs://master:9999/user/hadoop-twq/cmd/java_writer.txt";
Configuration configuration = new Configuration();
FileSystem fileSystem = FileSystem.get(URI.create(dest), configuration);
FSDataOutputStream out = fileSystem.create(new Path(dest));
out.write(content.getBytes("UTF-8"));
out.close();
}
}
javaAPI读取HDFS
package com.twq.hdfs;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.*;
import java.net.URI;
public class FileReader {
public static void main(String[] args) throws IOException {
String dest = "hdfs://master:9999/user/hadoop-twq/cmd/java_writer.txt";
Configuration configuration = new Configuration();
FileSystem fileSystem = FileSystem.get(URI.create(dest), configuration);
FSDataInputStream in = fileSystem.open(new Path(dest));
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
String line = null;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
in.close();
}
}
javaAPI删除HDFS
package com.twq.hdfs;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
public class FileDeleter {
public static void main(String[] args) throws IOException {
String dest = "hdfs://master:9999/user/hadoop-twq/cmd/java_writer.txt";
Configuration configuration = new Configuration();
FileSystem fileSystem = FileSystem.get(URI.create(dest), configuration);
fileSystem.delete(new Path(dest), false);
fileSystem.delete(new Path(""), true);
}
}