Overview
Hadoop 2.6.0をJava Clientライブラリを使って操作する方法を解説します。
`hdfs_skim`は`hdfs://{active_namenode_url}:{port}`形式のString型の変数です。
Hadoop Clientのインストール方法
[hadoop-client Maven Repository](https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client)でHadoopバージョンに合ったMavenリポジトリを見つけます。
プロジェクトのpom.xmlにhadoop-clientをdependencyとして登録します。
hadoop-client
create directory
`hdfs`の`/tmp/`ディレクトリ配下に`test`という名前のフォルダを作成します。
public static void createDirectory() throws IOException {
// create directory
Configuration configuration = new Configuration();
configuration.set("fs.defaultFS", hdfs_skim);
FileSystem fileSystem = FileSystem.get(configuration);
String directoryName = "/tmp/test";
Path path = new Path(directoryName);
fileSystem.mkdirs(path);
}
hdfs dfs -ls /tmp
drwxr-xr-x - user supergroup 0 2022-10-03 19:04 /tmp/test
create file
`hdfs`の`/tmp/test`ディレクトリ配下に`read_write_hdfs_example.txt`という名前のテキストファイルを作成します。
public static void writeFileToHDFS() throws IOException {
Configuration configuration = new Configuration();
configuration.set("fs.defaultFS", hdfs_skim);
FileSystem fileSystem = FileSystem.get(configuration);
//Create a path
String fileName = "read_write_hdfs_example.txt";
Path hdfsWritePath = new Path("/tmp/test/" + fileName);
FSDataOutputStream fsDataOutputStream = fileSystem.create(hdfsWritePath,true);
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(fsDataOutputStream,StandardCharsets.UTF_8));
bufferedWriter.write("Java API to write data in HDFS");
bufferedWriter.newLine();
bufferedWriter.close();
fileSystem.close();
}
hdfs dfs -ls /tmp/test
Found 1 items
-rw-r--r-- 3 user supergroup 31 2022-10-03 19:40 /tmp/test/read_write_hdfs_example.txt
edit file
`hdfs`の`/tmp/test/read_write_hdfs_example.txt`ファイルにテキスト行を1行追加します。
public static void writeFileToHDFS() throws IOException {
Configuration configuration = new Configuration();
configuration.set("fs.defaultFS", hdfs_skim);
FileSystem fileSystem = FileSystem.get(configuration);
//Create a path
String fileName = "read_write_hdfs_example.txt";
Path hdfsWritePath = new Path("/tmp/test/" + fileName);
FSDataOutputStream fsDataOutputStream = fileSystem.create(hdfsWritePath,true);
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(fsDataOutputStream,StandardCharsets.UTF_8));
bufferedWriter.write("Java API to write data in HDFS");
bufferedWriter.newLine();
bufferedWriter.close();
fileSystem.close();
}
hdfs dfs -cat /tmp/test/read_write_hdfs_example.txt
Java API to write data in HDFS
Java API to append data in HDFS file
read file
`hdfs`の`/tmp/test/read_write_hdfs_example.txt`ファイルを読み込みます。
public static void readFileFromHDFS() throws IOException {
Configuration configuration = new Configuration();
configuration.set("fs.defaultFS", hdfs_skim);
FileSystem fileSystem = FileSystem.get(configuration);
//Create a path
String fileName = "read_write_hdfs_example.txt";
Path hdfsReadPath = new Path("/tmp/test/" + fileName);
//Init input stream
FSDataInputStream inputStream = fileSystem.open(hdfsReadPath);
//Classical input stream usage
// String out= IOUtils.toString(inputStream, "UTF-8");
// System.out.println(out);
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(inputStream, StandardCharsets.UTF_8));
String line = null;
while ((line=bufferedReader.readLine())!=null){
System.out.println(line);
}
inputStream.close();
fileSystem.close();
}
Java API to write data in HDFS
Java API to append data in HDFS file
Process finished with exit code 0
クイズ
Java Hadoop Clientの使い方
hdfsの/tmp/ディレクトリ配下にtestという名前のフォルダを作成します。
hdfsの/tmp/testディレクトリ配下にread_write_hdfs_example.txtという名前のテキストファイルを作成します。
hdfsの/tmp/test/read_write_hdfs_example.txtファイルにテキスト行を1行追加します。
hdfsの/tmp/test/read_write_hdfs_example.txtファイルを読み込みます。
현재 단락 (1/82)
Hadoop 2.6.0をJava Clientライブラリを使って操作する方法を解説します。