Hadoop HDFS
本文最后更新于:2024年3月18日 凌晨
Hadoop HDFS
Shell 命令
目录操作
创建目录
实例
1
| ./bin/hdfs dfs -mkdir -p /user/hadoop
|
- 该命令中表示在HDFS中创建一个
/user/hadoop
目录。
-p
:表示如果是多级目录,则父目录和子目录一起创建。
查看目录
实例
~
:表示HDFS中的当前用户目录也就是/user/hadoop
目录,因此,上面的命令和下面的命令是等价的:
1
| ./bin/hdfs dfs –ls /home/hadoop
|
删除目录
-rm
:表示删除目录或文件。
-r
:表示递归删除,即删除文件夹。
实例
1
| ./bin/hdfs dfs -rm -r /user
|
文件操作
上传文件
实例
- 把本地文件系统的
/home/hadoop/myLocalFile.txt
上传到HDFS中的当前用户目录的input目录下,也就是上传到HDFS的/user/hadoop/input/
目录下。
1
| ./bin/hdfs dfs -put /home/hadoop/myLocalFile.txt input
|
下载文件
实例
- 把HDFS中的
myLocalFile.txt
文件下载到本地文件系统中的/home/hadoop/下载/
目录下。
1
| ./bin/hdfs dfs -get input/myLocalFile.txt /home/hadoop/下载。
|
查看文件
实例
- 查看HDFS中的
myLocalFile.txt
文件的内容。
1
| ./bin/hdfs dfs –cat input/myLocalFile.txt
|
复制文件
1
| hdfs dfs -cp <originPath> <destinationPath>
|
实例
- 把HDFS的
/user/hadoop/input/myLocalFile.txt
文件,拷贝到HDFS的另外一个目录/input
中。
1
| ./bin/hdfs dfs -cp input/myLocalFile.txt /input
|
移动文件
1
| hdfs dfs -mv <originPath> <destinationPath>
|
实例
- 把HDFS的
/user/hadoop/input/myLocalFile.txt
文件,移动到HDFS的另外一个目录/input
中,并且将文件名修改为test.txt
1
| ./bin/hdfs dfs -cp input/myLocalFile.txt /input/test.txt
|
Java API
写入文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.FSDataOutputStream; import org.apache.hadoop.fs.Path;
public class Test { public static void main(String[] args) { try { Configuration conf = new Configuration(); conf.set("fs.defaultFS","hdfs://localhost:9000"); conf.set("fs.hdfs.impl","org.apache.hadoop.hdfs.DistributedFileSystem"); FileSystem fs = FileSystem.get(conf); byte[] buff = "Hello world".getBytes(); String filename = "test"; FSDataOutputStream os = fs.create(new Path(filename)); os.write(buff,0,buff.length); System.out.println("Create:"+ filename); os.close(); fs.close(); } catch (Exception e) { e.printStackTrace(); } } }
|
判断文件是否存在
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path;
public class Test { public static void main(String[] args) { try { String filename = "test";
Configuration conf = new Configuration(); conf.set("fs.defaultFS","hdfs://localhost:9000"); conf.set("fs.hdfs.impl","org.apache.hadoop.hdfs.DistributedFileSystem"); FileSystem fs = FileSystem.get(conf); if(fs.exists(new Path(filename))){ System.out.println("文件存在"); }else{ System.out.println("文件不存在"); } fs.close(); } catch (Exception e) { e.printStackTrace(); } } }
|
读取文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| import java.io.BufferedReader; import java.io.InputStreamReader;
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.fs.FSDataInputStream;
public class Test { public static void main(String[] args) { try { Configuration conf = new Configuration(); conf.set("fs.defaultFS","hdfs://localhost:9000"); conf.set("fs.hdfs.impl","org.apache.hadoop.hdfs.DistributedFileSystem"); FileSystem fs = FileSystem.get(conf); Path file = new Path("test"); FSDataInputStream getIt = fs.open(file); BufferedReader d = new BufferedReader(new InputStreamReader(getIt)); String content = d.readLine(); System.out.println(content); d.close(); fs.close(); } catch (Exception e) { e.printStackTrace(); } } }
|