hadoop入门(Java访问hdfs)

hadoop默认把文件放在/tmp中,而该目录是一个临时目录用于传放临时文件,所有如果hadoop把文件放在这里会被不定时的删除。
修改配置文件:

# cd /usr/local/hadoop/etc/hadoop
# vi core-site.xml

在倒数第二行插入:
<property>
<name>hadoop.tmp.dir</name>
<value>/var/hadoop</value>
</property>

# hdfs namenode -format    //格式化namenode
# stop-dfs.sh
# start-dfs.sh

打开eclipse–>file–>new–>Java Project。输入project name:(HelloHDFS)点击“finish”.
链入Jar包,HelloHDfs–>Build Path–>Add External Archives…
Java包在hadoop的安装目录中这里用到的有(hadoop-2.7.3–>share–>hadoop–>common–>hadoop-common-2.7.3.jar和hadoop-2.7.3–>share–>hadoop–>common–>lib中所有的包,还有hadoop-2.7.3–>share–>hadoop–>hdfs–>hadoop-hdfs-2.7.3.jar)。

file–>new–>class.
输入Name:HelloHDFS ,将public static void main(String[] args)打钩,点击finish。
出现的内容如下图:

《hadoop入门(Java访问hdfs)》

tisp:throws Exception

在HDFS中存文件用于让使用Java程序读出来。
在master中输入:

# cd
# vi hello.txt    //任意输入一些东西。上传到HDFS的根目录下。
# hadoop fs -put ./hello.txt /
# hadoop fs -ls /
-rw-r--r--   3 root supergroup         24 2017-10-07 08:55 /hello.txt

用Java访问HTTP协议(访问http://www.baidu.com)例子程序:
URL url = new URL(“http://www.baidu.com“);
InputStream in = url.openStream();
IOUtils.copyBytes(in, System.out, 4096, true);

运行程序结果如下:
《hadoop入门(Java访问hdfs)》

用Java访问HDFS协议,例子程序:
URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
URL url = new URL(“hdfs://192.168.56.100:9000/hello.txt“);
InputStream in = url.openStream();
IOUtils.copyBytes(in, System.out, 4096, true);

运行程序结果如下:
《hadoop入门(Java访问hdfs)》

另一种访问hdfs的方法,例子程序:
Configuration conf = new Configuration();
conf.set(“fs.defaultFS”,”hdfs://192.168.56.100:9000″);
FileSystem fileSystem = FileSystem.get(conf);

创建目录的例子程序(在创建目录前首先确认这个目录是否存在):
boolean success = fileSystem.mkdirs(new Path(“/msb”));
System.out.println(success);

运行结果出现如下报错(是权限问题):
《hadoop入门(Java访问hdfs)》

最简单的修改权限的方法(关闭权限检查):
进入namenode的配置文件,修改hdfs-site.xml。

# cd /usr/local/hadoop/etc/hadoop
# vi hdfs-site.xml

插入配置文件:
<property>
<name>dfs.permissions.enabled</name>
<value>false</value>
</property>
重启namenode

# hadoop-daemon.sh stop namenode
# hadoop-daemon.sh start namenode

判断一个文件是否存在,例子程序:
success = fileSystem.exists(new Path(“/hello.txt”));
System.out.println(success);

删除一个目录的例子程序:
success = fileSystem.delete(new Path(“/msb”),true);
System.out.println(success);

运行综上程序结果如下:
《hadoop入门(Java访问hdfs)》

将宿主机中的文件上传到hdfs中,例子程序:
FSDataOutputStream out = fileSystem.create(new Path(“/test.data”),true);
FileInputStream fis = new FileInputStream(“d:/test/linux-xxwd”);
IOUtils.copyBytes(fis, out, 4096, true);

运行程序(无返回值,不贴图了)。在namenode下查看hdfs根目录中是否有这个文件。

# hadoop fs -ls /
-rw-r--r--   3 root supergroup         24 2017-10-07 08:55 /hello.txt
-rw-r--r--   3 Kate supergroup          0 2017-10-07 10:08 /test.data  //文件存在。

另一种上传文件了例子程序:
FSDataOutputStream out = fileSystem.create(new Path(“/test.data”),true);
FileInputStream in = new FileInputStream(“d:/test/linux-xxwd”);
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();

在namenode中将之前上传的文件删除。

# hadoop fs -rm /test.data
# hadoop fs -ls /
-rw-r--r--   3 root supergroup         24 2017-10-07 08:55 /hello.txt

运行上面的例子程序(无返回值),在namenode下查看hdfs根目录下是否有这个文件。

# hadoop fs -ls /
-rw-r--r--   3 root supergroup         24 2017-10-07 08:55 /hello.txt
-rw-r--r--   3 Kate supergroup          0 2017-10-07 10:25 /test.data//文件存在。

列举目录下的所有子目录或者文件的信息,例子程序:
FileStatus[] statuses = fileSystem.listStatus(new Path(“/”));
for(FileStatus status : statuses) {
System.out.println(status.getPath());
System.out.println(status.getPermission());
System.out.println(status.getReplication());
}

运行程序结果如下:
《hadoop入门(Java访问hdfs)》

    原文作者:ZqianLOYALTY
    原文地址: https://www.jianshu.com/p/ed9d1e800d49
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞