您的位置:首页 > 大数据 > Hadoop

JavaAPI操作HDFS文件

2020-07-12 16:58 155 查看

创建maven工程
new project -Maven quickstart
配置prm.xml

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.6.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.6.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-hdfs -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>

在$HADOOP_HOME/etc/hadoop/hdfs-site.xml中添加

<property>
<name>dfs.permissions</name>
<value>false</value>
</property>

1.创建目录

@Test
public void testMkdirs() throws URISyntaxException, IOException, InterruptedException {
//创建配置
Configuration conf = new Configuration();
//2获取文件系统
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.106.107:9000"), conf, "root");
//3调用API
fs.mkdirs(new Path("/hdfs/shell"));
fs.close();
}

上传本地一个文件

@Test
public void testCopyFromLocalFile() throws URISyntaxException, IOException, InterruptedException {
Configuration conf = new Configuration();
//2获取文件系统
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.106.107:9000"), conf, "root");
fs.copyFromLocalFile(new Path("a\\aa.txt"), new Path("/user/a.txt"));
fs.close();
}

下载到本地一个文件

@Test
public void testCopyToLocalFile() throws URISyntaxException,IOException,InterruptedException{
//1.创建配置文件
Configuration conf=new Configuration();
//2.获取文件系统
FileSystem fs=FileSystem.get(new URI("hdfs://192.168.106.107:9000"),conf,"root");
//3.调用API操作
fs.copyToLocalFile(new Path("/hdfs"),new Path("E:\\"));
//4.关闭资源
fs.close();
}

删除文件

@Test
public void testdelete() throws URISyntaxException, IOException, InterruptedException {
Configuration conf = new Configuration();
//2获取文件系统
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.106.107:9000"), conf, "root");
fs.delete(new Path("/hdfs/aa.txt"),true);
fs.close();
}

http://192.168.106.107:50070
查看文件是否操作成功

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: