马士兵hadoop第三课:java开发hdfs(转)

马士兵hadoop第一课:虚拟机搭建和安装hadoop及启动

马士兵hadoop第二课:hdfs集群集中管理和hadoop文件操作

马士兵hadoop第三课:java开发hdfs

马士兵hadoop第四课:Yarn和Map/Reduce配置启动和原理讲解

马士兵hadoop第五课:java开发Map/Reduce

(1)关于hdfs小结

hadoop由hdfs + yarn + map/reduce组成,

hdfs是数据库存储模块,主要由1台namenode和n台datanode组成的一个集群系统,

datanode可以动态扩展,文件根据固定大小分块(默认为128M),

每一块数据默认存储到3台datanode,故意冗余存储,防止某一台datanode挂掉,数据不会丢失。

HDFS = NameNode + SecondaryNameNode + journalNode + DataNode

hdfs的典型应用就是:百度云盘

(2)修改hadoop.tmp.dir默认值

hadoop.tmp.dir默认值为/tmp/hadoop-${user.name},由于/tmp目录是系统重启时候会被删除,所以应该修改目录位置。
修改core-site.xml(在所有节点上都修改)

[root@master ~]#  vim core-site.xml

修改完namenode和datanode上的hadoop.tmp.dir参数后,需要格式化namenode,在master上执行:

[root@master ~]# hdfs namenode -format

(4)测试期间关闭权限检查

为了简单起见,需要关闭权限检查,需要在namenode的hdfs-site.xml上,添加配置:

<property>    <name>dfs.permissions.enabled</name>    <value>false</value></property>

重新启动namenode:

[root@master ~]# hadoop-daemon.sh stop namenode[root@master ~]# hadoop-daemon.sh start namenode

(5) 使用FileSyste类来读写hdfs

复制代码
package com.hadoop.hdfs;import java.io.FileInputStream;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.FSDataOutputStream;import org.apache.hadoop.fs.FileStatus;import org.apache.hadoop.fs.FileSystem;import org.apache.hadoop.fs.Path;public class HelloHDFS {    public static Log log =  LogFactory.getLog(HelloHDFS.class);        public static void main(String[] args) throws Exception {        Configuration conf = new Configuration();        conf.set("fs.defaultFS", "hdfs://192.168.56.100:9000");        conf.set("dfs.replication", "2");//默认为3        FileSystem fileSystem = FileSystem.get(conf);                boolean success = fileSystem.mkdirs(new Path("/yucong"));        log.info("创建文件是否成功:" + success);                success = fileSystem.exists(new Path("/yucong"));        log.info("文件是否存在:" + success);                success = fileSystem.delete(new Path("/yucong"), true);        log.info("删除文件是否成功:" + success);                /*FSDataOutputStream out = fileSystem.create(new Path("/test.data"), true);        FileInputStream fis = new FileInputStream("c:/test.txt");        IOUtils.copyBytes(fis, out, 4096, true);*/                FSDataOutputStream out = fileSystem.create(new Path("/test2.data"));        FileInputStream in = new FileInputStream("c:/test.txt");        byte[] buf = new byte[4096];        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("/"));        log.info(statuses.length);        for(FileStatus status : statuses) {            log.info(status.getPath());            log.info(status.getPermission());            log.info(status.getReplication());        }    } }
复制代码

 这是一个maven项目,pom.xml文件为:

复制代码
  <dependencies>    <dependency>      <groupId>org.apache.hadoop</groupId>      <artifactId>hadoop-common</artifactId>      <version>2.7.3</version>    </dependency>        <dependency>        <groupId>org.apache.hadoop</groupId>        <artifactId>hadoop-hdfs</artifactId>       <version>2.7.3</version>    </dependency>      </dependencies>
复制代码

马士兵视频课程百度云盘下载:http://pan.baidu.com/s/1kVSbxS7

原文地址:http://www.cnblogs.com/yucongblog/p/6650839.html

马士兵hadoop第一课:虚拟机搭建和安装hadoop及启动

马士兵hadoop第二课:hdfs集群集中管理和hadoop文件操作

马士兵hadoop第三课:java开发hdfs

马士兵hadoop第四课:Yarn和Map/Reduce配置启动和原理讲解

马士兵hadoop第五课:java开发Map/Reduce

(1)关于hdfs小结

hadoop由hdfs + yarn + map/reduce组成,

hdfs是数据库存储模块,主要由1台namenode和n台datanode组成的一个集群系统,

datanode可以动态扩展,文件根据固定大小分块(默认为128M),

每一块数据默认存储到3台datanode,故意冗余存储,防止某一台datanode挂掉,数据不会丢失。

HDFS = NameNode + SecondaryNameNode + journalNode + DataNode

hdfs的典型应用就是:百度云盘

(2)修改hadoop.tmp.dir默认值

hadoop.tmp.dir默认值为/tmp/hadoop-${user.name},由于/tmp目录是系统重启时候会被删除,所以应该修改目录位置。
修改core-site.xml(在所有节点上都修改)

[root@master ~]#  vim core-site.xml

修改完namenode和datanode上的hadoop.tmp.dir参数后,需要格式化namenode,在master上执行:

[root@master ~]# hdfs namenode -format

(4)测试期间关闭权限检查

为了简单起见,需要关闭权限检查,需要在namenode的hdfs-site.xml上,添加配置:

<property>    <name>dfs.permissions.enabled</name>    <value>false</value></property>

重新启动namenode:

[root@master ~]# hadoop-daemon.sh stop namenode[root@master ~]# hadoop-daemon.sh start namenode

(5) 使用FileSyste类来读写hdfs

复制代码
package com.hadoop.hdfs;import java.io.FileInputStream;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.FSDataOutputStream;import org.apache.hadoop.fs.FileStatus;import org.apache.hadoop.fs.FileSystem;import org.apache.hadoop.fs.Path;public class HelloHDFS {    public static Log log =  LogFactory.getLog(HelloHDFS.class);        public static void main(String[] args) throws Exception {        Configuration conf = new Configuration();        conf.set("fs.defaultFS", "hdfs://192.168.56.100:9000");        conf.set("dfs.replication", "2");//默认为3        FileSystem fileSystem = FileSystem.get(conf);                boolean success = fileSystem.mkdirs(new Path("/yucong"));        log.info("创建文件是否成功:" + success);                success = fileSystem.exists(new Path("/yucong"));        log.info("文件是否存在:" + success);                success = fileSystem.delete(new Path("/yucong"), true);        log.info("删除文件是否成功:" + success);                /*FSDataOutputStream out = fileSystem.create(new Path("/test.data"), true);        FileInputStream fis = new FileInputStream("c:/test.txt");        IOUtils.copyBytes(fis, out, 4096, true);*/                FSDataOutputStream out = fileSystem.create(new Path("/test2.data"));        FileInputStream in = new FileInputStream("c:/test.txt");        byte[] buf = new byte[4096];        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("/"));        log.info(statuses.length);        for(FileStatus status : statuses) {            log.info(status.getPath());            log.info(status.getPermission());            log.info(status.getReplication());        }    } }
复制代码

 这是一个maven项目,pom.xml文件为:

复制代码
  <dependencies>    <dependency>      <groupId>org.apache.hadoop</groupId>      <artifactId>hadoop-common</artifactId>      <version>2.7.3</version>    </dependency>        <dependency>        <groupId>org.apache.hadoop</groupId>        <artifactId>hadoop-hdfs</artifactId>       <version>2.7.3</version>    </dependency>      </dependencies>
复制代码

马士兵视频课程百度云盘下载:http://pan.baidu.com/s/1kVSbxS7

猜你喜欢

转载自www.cnblogs.com/jpfss/p/9034788.html