Hadoop(5)--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、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());
}

7、思考百度网盘的实现方式?

8、core-site做什么用?hdfs-site做什么用?

posted @ 2018-01-22 18:10  xu_shuyi  阅读(181)  评论(0)    收藏  举报