hadoop的HDFS

HDFS(Hadoop Distributed File System)分布式文件管理系统。
容错性:系统中某个节点(即服务器)脱机,整个系统也能正常运行不会有数据丢失(即不同服务器数据存在冗余)。
以流的形式访问文件系统中的数据。
hdfs适合一次写入多次查询(即不能修改,如果要修改文件数据,先删除,重新写添加),不支持并发写,小文件不适合。

FS SHELL命令
Usage: java FsShell
[-ls <path>]
[-lsr <path>]
[-du <path>]
[-dus <path>]
[-count[-q] <path>]
[-mv <src> <dst>]
[-cp <src> <dst>]
[-rm [-skipTrash] <path>]
[-rmr [-skipTrash] <path>]
[-expunge]
[-put <localsrc> ... <dst>]
[-copyFromLocal <localsrc> ... <dst>]
[-moveFromLocal <localsrc> ... <dst>]
[-get [-ignoreCrc] [-crc] <src> <localdst>]
[-getmerge <src> <localdst> [addnl]]
[-cat <src>]
[-text <src>]
[-copyToLocal [-ignoreCrc] [-crc] <src> <localdst>]
[-moveToLocal [-crc] <src> <localdst>]
[-mkdir <path>]
[-setrep [-R] [-w] <rep> <path/file>]
[-touchz <path>]
[-test -[ezd] <path>]
[-stat [format] <path>]
[-tail [-f] <file>]
[-chmod [-R] <MODE[,MODE]... | OCTALMODE> PATH...]
[-chown [-R] [OWNER][:[GROUP]] PATH...]
[-chgrp [-R] GROUP PATH...]
[-help [cmd]]


[color=red]查看文件[/color]
# hadoop fs -ls / (查看hdfs根目录)
# hadoop fs -lsr / (查看hdfs根目录所以文件、文件夹)
# hadoop fs -lsr hdfs://aly(主机名):9000/ (完整路径写法)

默认读取值为:# vi /home/hadoop-1.1.2/conf/core-site.xml 的
<property>
<name>fs.default.name</name>
[color=blue]<value>hdfs://aly:9000</value>
[/color]</property>

权限 冗余数 所有者 组 文件大小 时间 名称
-rw------- 1 root supergroup 4 2014-09-19 11:32 /usr/local/hadoop/tmp/mapred/system/jobtracker.info

[color=red]创建文件夹、上传文件、下载文件[/color]
# hadoop fs -mkdir /test (在hdfs根目录创建一个test文件夹)

# hadoop fs -put /root/m140730636.jpg /test (在linux系统/root/m140730636.jpg 上传到 /test文件夹下)
注意:文件名已经存在,上传相同文件名是不允许的。即不能覆盖文件。

# hadoop fs -put /root/m140730636.jpg /test/abc (再次上传,把文件名改成abc)

# hadoop fs -get /test/abc /root (把hdfs系统中的abc文件下载到linux系统root文件夹下)

[color=red]查看文件、删除文件[/color]
# hadoop fs -text /test/abc (查看)
# hadoop fs -rm /test/abc (删除)
# hadoop fs -rmr /test (删除文件夹及以下文件)


# hadoop (查看hadoop的命令)
# hadoop fs (查看hadoop fs的命令)


Browse the filesystem无法打开,暂时使用下面链接
http://114.215.104.**:50075/browseDirectory.jsp?dir=%2Ftest&namenodeInfoPort=50070


使用java代码操作hdfs

public class AppHDFS {

static String HDFS_PATH="hdfs://114.215.104.**:9000";

public static void main(String[] args) throws Exception {
// URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
// URL url=new URL(HDFS_PATH+"/a.txt");
// InputStream in=url.openStream();
// IOUtils.copyBytes(in, System.out, 1024,true);//读取文件

FileSystem fileSystem=FileSystem.get(new URI(HDFS_PATH),new Configuration());

fileSystem.mkdirs(new Path(HDFS_PATH+"/test"));//创建文件夹

FSDataOutputStream out= fileSystem.create(new Path(HDFS_PATH+"/test/abc.txt"));//本地上传文件
FileInputStream ins=new FileInputStream("c://wxy.sql");
IOUtils.copyBytes(ins, out, 1024,true);

FSDataInputStream fds=fileSystem.open(new Path(HDFS_PATH+"/test/abc.txt"));//读取文件
IOUtils.copyBytes(fds, System.out, 1024,true);

fileSystem.delete(new Path(HDFS_PATH+"/test"),true);//删除文件夹

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值