在此就不详细介绍HDFS是啥东西了,你只要问问google大神或度娘就一清二楚了。
在此我主要用java代码实现对HDFS的增、删、查操作。
由于本工程是用Maven管理的,则pom文件如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cloud.hdfs</groupId> <artifactId>java-hdfs</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>java-hdfs</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-core</artifactId> <version>1.2.1</version> </dependency> </dependencies> </project>
HDFSClient.java
package com.cloud.hdfs; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataInputStream; import org.apache.hadoop.fs.FSDataOutputStream; import org.apache.hadoop.fs.FileStatus; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IOUtils; public class HDFSClient { private FileSystem fileSystem; /** * 在创建对象时,把fileSystem实例化。 * @param conf * @throws IOException */ public HDFSClient(Configuration conf) throws IOException { fileSystem = FileSystem.get(conf); } public void close() throws IOException { fileSystem.close(); } /** * 实现的命令: * hadoop fs -ls /chris * @param folder * @throws IOException */ public void ls(String folder) throws IOException { Path path = new Path(folder); FileStatus[] fileStatus = fileSystem.listStatus(path); System.out.println("===================================================="); for (FileStatus fs : fileStatus) { System.out.println("name: " + fs.getPath() +" folder: " + fs.isDir() + " size: " + fs.getLen() + " permission: " + fs.getPermission()); } System.out.println("===================================================="); } /** * 实现的命令: * hadoop fs -mkdir /chris/client * @param folder * @throws IOException */ public void mkdir(String folder) throws IOException { Path path = new Path(folder); if (!fileSystem.exists(path)) { fileSystem.mkdirs(path); System.out.println("Created " + folder); } } /** * 实现的命令: * haoop fs -rmr /chris/client * @param folder * @throws IOException */ public void rmr(String folder) throws IOException { Path path = new Path(folder); fileSystem.deleteOnExit(path); System.out.println("Delete the " + folder); } /** * 实现的命令: * hadoop fs -copyFromLocal /home/chris/test /chris/ * 注意:此处由于ubuntu操作系统是安装在win7的虚拟机上的,而这段程序是在win7下run的 * 所以此处的本地路径就是win7的。 * @param local * @param remote * @throws IOException */ public void copyFile(String local, String remote) throws IOException { fileSystem.copyFromLocalFile(new Path(local), new Path(remote)); System.out.println("Copy from " + local +" to " + remote); } /** * 实现命令: * hadoop fs -cat /chris/test * @param file * @throws IOException */ public void cat(String file) throws IOException { Path path = new Path(file); FSDataInputStream in = fileSystem.open(path); IOUtils.copyBytes(in, System.out, 4096, false); IOUtils.closeStream(in); } /** * 实现的命令: * hadoop fs -copyToLocal /tmp/core-site.xml /home/chris * 注意:此处的本地也是win7的路径,理由同上。 * @param remote * @param local * @throws IOException */ public void download(String remote, String local) throws IOException { fileSystem.copyToLocalFile(new Path(remote), new Path(local)); System.out.println("Download from " + remote + " to " + local); } /** * 这个是没法通过一条命令来实现的。 * 但是创建文件是有命令的:hadoop fs -touchz /chris/hehe * 只不过里面的内容是空的。 * @param file * @param content * @throws IOException */ public void createFile(String file, String content) throws IOException { byte[] buff = content.getBytes(); FSDataOutputStream out = fileSystem.create(new Path(file)); out.write(buff, 0, buff.length); out.close(); } public static void main(String[] args) throws Exception { //在实现这个config时,它会自动去加载resources下的这几个配置文件 Configuration config = new Configuration(); HDFSClient client = new HDFSClient(config); // client.mkdir("/chris/client"); // client.ls("/chris"); // client.rmr("/chris/client"); // client.ls("/chris"); // client.cat("/chris/test"); // client.copyFile("src/main/resources/core-site.xml", "/tmp/"); // client.download("/chris/test", "src/main/resources/"); // client.createFile("/chris/client.txt", "ddddddddd"); client.close(); } }
相关推荐
深度剖析Hadoop HDFS-高清-完整目录-2017年3月。。。。
eclipse连接hadoop的详细配置教程指南。适合hadoop学习新手。
win10下安装haoop-2.6,包含winunits.exe,亲测有效!
大数据haoop基础实验指导书.docx
2.1.2 第二步:修改zoo.cfg 7 2.1.3 第三步:创建myid 9 2.1.4 第四步:配置环境变量 9 2.1.5 第五步:测试 9 2.2 hadoop 10 2.2.1 第一步:解压 10 2.2.2 第二步:配置core-site.xml 11 2.2.3 第三步:配置hdfs-...
文章目录一、MapReduce跑得慢的原因二、MapReduce优化方法(一)数据输入(二)Map阶段(三)Reduce阶段(四)I/O阶段(五)数据倾斜问题(六)常用的调优参数三、HDFS小文件优化方法 一、MapReduce跑得慢的原因 ...
hadoop开发平台必备安装包,除share部分在我的另一个上传资料中
Hadoop实现了一个分布式文件系统(Hadoop Distributed File System),简称HDFS。HDFS有着高容错性的特点,并且设计用来部署在低廉的(low-cost)硬件上。而且它提供高传输率(high throughput)来访问应用程序的...
htrace-core-3.0.4.jar\hadoop-hdfs-nfs-2.6.0 (1).jar hadoop-nfs\hadoop-hdfs\hadoop-common\hadoop-auth\hadoop-clientcommons-daemon-1.0.13.jar commons-configuration-1.6.jar
a)SecondaryNameNode b)DataNode c)TaskTracker d)Jobtracker 注:haoop1.X 分析: hadoop 的集群是基于 master/slave 模式,namenode 和 jobtracker 属于 master,datanode 和 tasktracker属于 slave,master 只 ...
涵盖hadoop基础知识,MapReduce框架。编写和运行hadoop数据处理程序所需的实践技能。
在Centos7.0中搭建Haoop伪分布式环境,并用Java接口上传文件进行测试。
用来做一个推荐系统。hadoop框架用的,搭建环境,再windows下,导入到eclipse中。
sqoop安装详解以及sqoop内容介绍使用介绍 ...2、导入数据:从结构化数据(Mysql,oracle,db2,)导入到半结构化或非结构化hadoop中(HDFS、HIVE、HBASE) 3、导出数据:从haoop中将数据导出到关系数据库中MySql
haoop权威指南中文版,这是一本很好的大数据方面书籍值得学习,内容包括hdfs、mapredurce、pig等
大数据离线分析项目(Hadoop).pdf
linux-结构化成行成列-小文件循环合并成大文件--方便上传hadoop: 文件到达门限后上传,或者文件时间戳超过门限时间,上传HDFS,防止小文件过多上传。
分布式框架 Hadoop实战 chuck lamz著 韩翼中译 pdf
1.Haoop 稳定版本 hadoop-1.1.2-1 2.集成 JDK 6 3.集成LZO,hadoop-gpl-packaging-0.6.1-1 4.界面化一键安装部署管理hadoop集群。 5.支持64位 linux 操作系统. 特别是 redhat centos 5 6 6.五种秒级监控图表,...
hadoop-dist-2.7.0.jar