在写增删改查前,我想先写一下如何在idea中直接连接hdfs

首先我们需要在File->Settings->Plugins 中下载Big Data Tools

然后我们发现idea右边多了一个Big Data Tools

 

选择Explicit uri 

 

 

最后我们看到成功连接.(在此之前肯定要启动虚拟机上的Hadoop集群的) 

然后进入正题 ,使用hdfs进行增删改查 

先导入新的Hadoop依

org.apache.hadoop

hadoop-hdfs

2.6.0

org.apache.hadoop

hadoop-client

2.6.0

org.apache.hadoop

hadoop-common

2.6.0

 既然是连接hdfs 那么URI 和 User 用户是肯定要获取的 我这里提供两种获取方法

1,直接进行输入 获取

// 创造连接

Configuration conf = new Configuration();

// 连接hdfs

FileSystem fs = FileSystem.get(new URI("hdfs://192.xxx:8020"),conf,"root");

2 创建properties文件,寻址获得

在resources下创建Hadoop.properties文件  里面存放URI 与 root 用户名称即可

 

                           

Configuration conf = new Configuration(); // 创造连接

Properties properties = new Properties();

properties.load(new FileInputStream("src/main/resources/Hadoop.properties"));

String URI = properties.getProperty("URI");

String user = properties.getProperty("user");

FileSystem fs = FileSystem.get(new URI(URI),conf,user); // 配置连接hdfs

两种方法都行,看自己进行选择

最后附上增删改查的代码

package hdfs;

import jdk.nashorn.internal.ir.Block;

import org.apache.hadoop.fs.*;

import org.apache.slider.server.avro.LoadedRoleHistory;

import org.testng.annotations.Test;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.io.IOUtils;

import java.io.FileInputStream;

import java.net.URI;

import java.util.Properties;

public class testCopyToLocalFile {

@Test

public void testCopyToLocalFile() throws Exception {

// 1 获取文件系统

Configuration conf = new Configuration();

// 连接到hdfs

Properties properties = new Properties();

properties.load(new FileInputStream("src/main/resources/Hadoop.properties"));

String URI = properties.getProperty("URI");

String user = properties.getProperty("user");

FileSystem fs = FileSystem.get(new URI(URI),conf,user); // 配置连接hdfs

// 2 执行下载操作

// boolean delSrc 指是否将原文件删除

// Path src 指要下载的文件路径

// Path dst 指将文件下载到的路径

// boolean useRawLocalFileSystem 是否开启文件校验

RemoteIterator listFiles = fs.listFiles(new Path("/banhua.txt"),true);

while(listFiles.hasNext()){

LocatedFileStatus status = listFiles.next();

// 文件名称

System.out.println(status.getPath().getName());

// 长度

System.out.println(status.getLen());

// 权限

System.out.println(status.getPermission());

// 分组

System.out.println(status.getGroup());

// 用户

System.out.println(status.getOwner());

// 获取存储的块信息

BlockLocation[] blockLocations = status.getBlockLocations();

for (BlockLocation blockLocation:blockLocations){

String [] hosts = blockLocation.getHosts();

for(String host:hosts){

System.out.println(host);

}

}

}

// fs.create(new Path("/a"));

// 删除文件

boolean status = fs.delete(new Path("/zzzz"),true);

// 删除成功 输出true 否则 输出 false

System.out.println(status);

// 新建文件

fs.create(new Path("/a.txt"));

// 重命名文件

fs.rename(new Path("/a.txt"),new Path("/xiaose.txt"));

// 将hdfs 上的文件拷贝到本地 命令

fs.copyToLocalFile(false, new Path("/banhua.txt"), new Path("D:/banhua.txt"), true);

// 3 关闭资源

fs.close();

}

}

 

如上面三个图所示,通过测试没有问题。

参考阅读

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: