目录
//通过工具类来操作hdfs hdfs dfs -put d:user_info.txt /user_info.txt
// 将文件放入到hdfs中
@Test
public void test1() throws IOException { // 构建方法的时候,不要加 static 加了之后@Test就不能用了
//操作hdfs
//1. 初始化配置对象 需要new出来
Configuration conf = new Configuration();
//2. 添加配置 (其实就是提供一个key - value)
conf.set("fs.defaultFS","hdfs://hadoop10:8020");
//3. 构建操作hdfs的具体工具类
FileSystem fileSystem = FileSystem.get(conf);
//通过工具类来操作hdfs hdfs dfs -put d:user_info.txt /user_info.txt
// 将文件放入到hdfs中
fileSystem.copyFromLocalFile(new Path("d:\\user_info.txt"),new Path("/"));
// Permission denied: 看到这个就一定要想到权限问题
// hdfs dfs -chmod -R 777 /
/*
运行结果中有如下的信息,可以忽略
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
*/
//关闭hdfs的连接工具
if (fileSystem != null) {
fileSystem.close();
}