hadoop之旅(二)

前面我们已经搭建好hadoop,hdfs,一台master两台slaves。
1。hadoop-daemon.sh start/stop namenode/datanode启动/关闭hdfs
2。hdfs dfsadmin -report|more 查看集群情况
3.start-dfs 直接在master启动集群
4.ssh’免密码登录ssh_keygen -t rsa ,生成一个私钥一个公钥文件,把公钥拷贝到slaves。ssh-copy-id master /slave1/slave2
5.start-dfs 直接在master启动集群
6.修改hdfs-site.xml, 增加property dfs.replication 修改默认备份数
7.core-site.xml

fs.defaultFS
hdfs://master:9000

配置master
hadoop.tmp.dir
/var/hadoop
默认存在tmp下容易出问题就修改
8.java操作HDFS
URL url=new URL(“http://www“);//远程操作http协议
inputstream in =url.openStream();
IOUtils.copyBytes(in,System.out,4096,true)
1.方式
URL.setURLStreamHandlerfactory(new FsUrlStreamHandlerFactory());
URL url=new URL(“hdfs:192.168.233.131:9000/hello.txt”);//远程操作hdfs
inputstream in =url.openStream();
IOUtils.copyBytes(in,System.out,4096,true)
2.方式filesystem
Configuration conf=new Configuration();
conf.set(“fs.defaultFS”,”hdfs://192.168.233.131:9000”);
conf.set(“dfs.replication”,2)//默认备份数
FileSystem fileSystem=FileSystem.get(conf); //文件系统抽象类
boolean success=fieSystem.mkdir(new Path(“/msb”));//创建目录(会覆盖,用之前要判断是否存在)

success=fileSystem.exists(new Path(“/hello.txt”))
success=fileSystem.delete(new Path(“/hello.txt”),true) //删除
FsDataOutputStream out=create(new Path(“/test.data”),true)
FileInputStream fis=new FileInputStream(“f:/hello.txt”)
IOUtils.copyBytes(fis,out,4096,true)//从windows拷贝文件放在集群上(传数据)
/第二种传数据,用java的io/
FsDataOutputStream out=create(new Path(“/test.data”),true)
FileInputStream in=new FileInputStream(“f:/hello.txt”)
byte []buf=new byte[4096]; //buffer
int len = in.read(buf);
while(len !=-1){
out.write(buf,0,len)
len=in.read(buf)
}
in.close();
out.close();

/列举目录下所有文件或者子目录的信息/
FileStatus[] statuses=fileSystem.listStatus(new Path(“/”)); // 子目录下所有信息

for(FileStatus status:statuses){
status.getPath()//目录下所有文件路径
status.getPermission()//权限
status.getReplication//分了多少份
}
9.
windows与linux权限系统不一样,测试期间可以关闭权限检查,在namenode-site。xml添加配置
dfs.permissions.enabled
false

hadoop dfsadmin -safemode leave #解除hadoop的安全模式

hadoop fs -copyFromLocal URI#拷贝本地文件到hdfs
hadoop fs -cat file:///file3 /user/hadoop/file4#将路径指定文件的内容输出到stdout
hadoop fs -chgrp [-R] GROUP URI#改变文件的所属组
hadoop fs -chmod [-R] 755 URI#改变用户访问权限
hadoop fs -chown [-R] [OWNER][:[GROUP]] URI [URI ]#修改文件的所有者
hadoop fs -copyToLocal URI localdst#拷贝hdfs文件到本地
hadoop fs -cp URI [URI …] #拷贝hdfs文件到其它目录
hadoop fs -du URI [URI …]#显示目录中所有文件的大小
hadoop fs -getmerge [addnl]#合并文件到本地目录

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值