java编写hdfs程序

1.hdfs系统会把用到的数据存储在core-site.xml中由hadoop.tmp.dir指定,而这个值默认位于/tmp/hadoop-${user.name}下面, 由于/tmp目录在系统重启时候会被删除,所以应该修改目录位置。 修改core-site.xml(在所有站点上都修改)

<property>
    <name>hadoop.tmp.dir</name>
    <value>/var/hadoop</value>
</property>

2.第一次启动时要讲namenode format一下 hdfs namenode format

3.通过java访问hdfs程序,就把hdfs集群当成一个大的系统磁盘就行了

4.windows上的权限系统和linux上的权限系统,测试期间为了简单起见可以关闭权限检查 在namenode的hdfs-site.xml上,添加配置:

< property >
     < name >dfs.permissions.enabled</ name >
     < value >false</ value >
</ property >
重新启动namenode hadoop-daemon.sh stop namenode hadoop-daemon.sh start namenode


5.从hdfs中读取文件

URL.setURLStreamHandlerFactory( new FsUrlStreamHandlerFactory());
InputStream in = new URL( "hdfs://192.168.56.100:9000/test.data" ).openStream();
IOUtils.copyBytes(in, System.out, 4096 , true );

6.使用fileSystem类
Configuration conf = new Configuration();
conf.set( "fs.defaultFS" , "hdfs://192.168.56.100:9000" );
FileSystem fileSystem = FileSystem.get(conf);
 
boolean b = fileSystem.exists( new Path( "/hello" ));
System.out.println(b);
 
boolean success = fileSystem.mkdirs( new Path( "/mashibing" ));
System.out.println(success);
 
success = fileSystem.delete( new Path( "/mashibing" ), true );
System.out.println(success);
 
FSDataOutputStream out = fileSystem.create( new Path( "/test.data" ), true );
FileInputStream fis = new FileInputStream( "c:/test/core-site.xml" );
IOUtils.copyBytes(fis, out, 4096 , true );
 
FileStatus[] statuses = fileSystem.listStatus( new Path( "/" ));
//System.out.println(statuses.length);
for (FileStatus status : statuses) {
     System.out.println(status.getPath());
     System.out.println(status.getPermission());
     System.out.println(status.getReplication());
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值