hdfs权限
类似Unix Linux
有用户的概念,没有API创建用户的脚本 默认信任客户端,使用操作系统的用户。
有超级用户 ,超级用户是启动NameNode的用户
hdfs 有自己的权限控制,由超级用户控制,当启动NameNode的用户改变,超级用户也会改变。。
添加用户
useradd benxiong
passwd benxiong
修改node1 -node4资源拥有者
chown -R benxiong hadoop-2.6.5/
chown -R benxiong hadoop/
切换到benxiong 做一下账号的ssh免密
.ssh文件还没有 登录一下 ssh node1 生成
//生成.ssh文件
ssh node1
exit
切换到benxiong账号 node1 对node1 node2 node3 node4 免密 在node1执行
ssh-keygen -t rsa -m PEM
ssh-copy-id node1
ssh-copy-id node2
ssh-copy-id node3
ssh-copy-id node4
切换到benxiong账号 node4对node1免密
ssh-keygen -t rsa -m PEM
ssh-copy-id node1
使用benxiong账号启动hdfs
start-dfs.sh
idea实际操作
新建springboot 工程,修改pom文件依赖
这里的版本和hdfs要一致
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.6.5</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.6.5</version>
</dependency>
复制core-site.xml hdfs-site.xml 到资源文件下
![](https://img-blog.csdnimg.cn/img_convert/5bf40784b692db139e82e1319b8dfd9b.png)
java 类
package com.example.hadoophdfs;
import org.apache.hadoop.conf.Configurable;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.URI;
@SpringBootApplication
public class HadoopHdfsApplication implements CommandLineRunner {
public Configuration conf = null;
public FileSystem fs = null;
public static void main(String[] args) {
SpringApplication.run(HadoopHdfsApplication.class, args);
}
@Override
public void run(String... strings) throws Exception {
conf = new Configuration(true);
//直接使用配置文件中的集群HDFS
// fs = FileSystem.get(conf);
fs = FileSystem.get(URI.create("hdfs://mycluster"), conf, "benxiong");
Path path = new Path("/csg");
if(fs.exists(path)){
fs.delete(path, true);
}
FSDataOutputStream fsos = fs.create(path);
//获得文件的
// fs.getFileBlockLocations()
BufferedInputStream input = new BufferedInputStream(new FileInputStream(new File("/Users/benxiong/work/hadoop/hadoop-hdfs/src/main/resources/testdata")));
Path out = new Path("/csg01/testdata");
FSDataOutputStream fsDataOutputStream = fs.create(out);
IOUtils.copyBytes(input,fsDataOutputStream,conf,true);
fs.close();
}
}
上传后查看效果
![](https://img-blog.csdnimg.cn/img_convert/f18e25ddf48bf81af0855c7b248ccc8e.png)