1.hadoop环境搭建可以扫描下面二维码,里面有手测有效的步骤
2.FileSystem使用建议
[不要调用Hadoop的FileSystem#close](https://zhuanlan.zhihu.com/p/144552484)
[Hdfs FileSystem 使用姿势不对导致的内存泄露](https://blog.csdn.net/u013332124/article/details/89302271)
3.HDFS Java API demo
application.yml
hdfs:
hdfsPath: hdfs://192.168.225.133:9000
hdfsName: root
bufferSize: 67108864
HDFSConfig
@org.springframework.context.annotation.Configuration
public class HDFSConfig {
@Autowired
private HDFSProperties hdfsProperties;
/**
* 获取HDFS配置信息
* @return
*/
public Configuration getConfiguration() {
Configuration configuration = new Configuration();
configuration.set("fs.defaultFS", hdfsProperties.getHdfsPath());
return configuration;
}
@Bean
public FileSystem getFileSystem () throws Exception {
FileSystem fileSystem = FileSystem.get(new URI(hdfsProperties.getHdfsPath()), getConfiguration(), hdfsProperties.getHdfsName());
return fileSystem;
}
}
service
public interface HDFSService {
/**
* 在HDFS创建文件夹
* @param path
* @return
* @throws Exception
*/
boolean mkdir(String path) throws Exception;
/**
* 判断HDFS文件是否存在
* @param path
* @return
* @throws Exception
*/
boolean existFile(String path) throws Exception;
/**
* 读取HDFS目录信息
* @param path
* @return
* @throws Exception
*/
List<Map<String, Object>> readPathInfo(String path) throws Exception;
/**
* HDFS创建文件
* @param path
* @param file
* @throws Exception
*/
void createFile(String path, MultipartFile file) throws Exception;
/**
* 读取HDFS文件内容
* @param path
* @return
* @throws Exception
*/
String readFile(String path) throws Exception;
/**
* 读取HDFS文件列表
* @param path
* @return
* @throws Exception
*/
List<Map<String, String>> listFile(String path) throws Exception;
/**
* HDFS重命名文件
* @param oldName
* @param newName
* @return
* @throws Exception
*/
boolean renameFile(String oldName, String newName) throws Exception;
/**
* 删除HDFS文件
* @param path
* @return
* @throws Exception
*/
boolean deleteFile(String path) throws Exception;
/**
* 上传HDFS文件
* @param path
* @param uploadPath
* @throws Exception
*/
void uploadFile(String path, String uploadPath) throws Exception;
/**
* 下载HDFS文件
* @param path
* @param downloadPath
* @throws Exception
*/
void downloadFile(String path, String downloadPath) throws Exception;
/**
* HDFS文件复制
* @param sourcePath
* @param targetPath
* @throws Exception
*/
void copyFile(String sourcePath, String targetPath) throws Exception;
/**
* 打开HDFS上的文件并返回byte数组
* @param path
* @return
* @throws Exception
*/
byte[] openFileToBytes(String path) throws Exception;
/**
* 打开HDFS上的文件并返回java对象
* @param path
* @return
* @throws Exception
*/
<T extends Object> T openFileToObject(String path, Class<T> clazz) throws Exception;
/**
* 获取某个文件在HDFS的集群位置
* @param path
* @return
* @throws Exception
*/
BlockLocation[] getFileBlockLocations(String path) throws Exception;
}
完整项目地址在微信公众中,谢谢大家支持
手把手教你Hadoop的搭建
网友评论