前提条件:
Linux下安装好Hadoop2.7.3
Windows下安装好Maven
Windows系统下安装好idea
idea配置好Maven
新建好Maven工程
新建一个HDFS包并创建App类
修改pom.xml添加Hadoop依赖
在前一行添加如下语句: 没有 就创建一个
<!-- 添加相关依赖 -->
<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>
如果出现hadoop包不能引入则再加入
<dependency>
<groupId>jdk.tools</groupId>
<artifactId>jdk.tools</artifactId>
<version>1.8</version>
<scope>system</scope>
<!--suppress UnresolvedMavenProperty -->
<systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
</dependency>
App类添加代码
package com.test.HelloWorld;
public class App {
public static void main(String[] args) {
System.out.println("hello world");
}
}
测试App包能不能正常运行
开始HDFS API编程
1.读取HDFS文件
新建ReadFile.java 添加代码
package com.test.HDFS;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
public class ReadFile {
public static void main(String[] args) throws IOException {
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://192.168.91.128:8020");
FileSystem fs = FileSystem.get(conf);
Path src=new Path("hdfs://192.168.91.128:8020/file.txt");
FSDataInputStream dis = fs.open(src);
IOUtils.copyBytes(dis, System.out, conf);
dis.close();
}
}
注意:192.168.134.128修改成自己Linux的IP
运行程序之前,先新建出测试文件:file.txt
$ vim file.txt
内容为:
Hello Hadoop
Hello HDFS:wq
上传file.txt到HDFS /目录下
$ hdfs dfs -put file.txt /
查看是否上传成功
$ hdfs dfs -ls /
运行: 红色的log4j只是警告,不影响我们读取HDFS文件,可以先不处理它。
以上程序完成了读取HDFS文件内容,接下来可以做的是:通过API完成文件的写入、文件的上传、下载等等!
文件创建与写入
新建类 CreateFile.java 代码:
package com.test.HDFS;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.fs.*;
import java.net.URI;
public class CreateFile {
public static void main(String[] args) throws Exception{
Configuration conf=new Configuration();
URI uri=new URI("hdfs://192.168.134.128:8020");
FileSystem fs=FileSystem.get(uri,conf,"hadoop");
Path dfs=new Path("/user/hadoop/1.txt");
FSDataOutputStream fos=fs.create(dfs,true);
fos.writeBytes("hello hdfs\n");
fos.writeUTF("你好啊!\n");
fos.close();
}
}
运行 查看文件
hdfs dfs -ls /
命令查看
hdfs dfs -cat /user/hadoop/1.txt
成功创建与写入
文件删除
新建类 DeleteFile.java 代码:
package com.test.HDFS;
import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class DeleteFile {
public static void main(String[] args) throws Exception {
Configuration conf=new Configuration();
URI uri=new URI("hdfs://192.168.91.128:8020");
FileSystem fs=FileSystem.get(uri,conf,"hadoop");
Path path=new Path("/user/hadoop/1.txt");
fs.delete(path,true);
}
}
运行: 查看是否删除成功
hdfs dfs -ls /user/hadoop/
文件为空
文件上传
新建类 UploadFile.java 代码:
package com.test.HDFS;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.fs.*;
import java.net.URI;
public class UploadFile {
public static void main(String[] args) throws Exception{
Configuration conf=new Configuration();
URI uri=new URI("hdfs://192.168.91.128:8020");
FileSystem fs=FileSystem.get(uri,conf,"hadoop");
Path src=new Path("F:/2.txt");
Path dst=new Path("/user/hadoop");
fs.copyFromLocalFile(src,dst);
}
}
在相应文件位置找到路劲文件 eg: 对应 运行 查看
hdfs dfs -ls /user/hadoop/
文件的下载
新建类 DownloadFile.java 代码:
package com.test.HDFS;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.fs.*;
import java.net.URI;
public class DownloadFile {
public static void main(String[] args) throws Exception{
Configuration conf=new Configuration();
URI uri=new URI("hdfs://192.168.91.128:8020");
FileSystem fs=FileSystem.get(uri,conf,"hadoop");
Path src=new Path("/user/hadoop/2.txt");
Path dst=new Path("F:/test");
fs.copyToLocalFile(false,src,dst,true);
}
}
先创建文件夹 运行结果 查看F:/test下是否有文件/user/hadoop/中的文件2.txt
查看文件属性
新建类 FileAttr.java 代码:
package com.test.HDFS;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.fs.*;
import java.net.URI;
import java.sql.Date;
import java.text.SimpleDateFormat;
public class FileAttr {
public static void main(String[] args) throws Exception {
Configuration conf=new Configuration();
URI uri=new URI("hdfs://192.168.91.128:8020");
FileSystem fs=FileSystem.get(uri,conf,"hadoop");
Path fpath=new Path("/user/hadoop/2.txt");
FileStatus filestatus=fs.getFileLinkStatus(fpath);
long blocksize=filestatus.getBlockSize();
System.out.println("blocksize:"+blocksize);
long filesize=filestatus.getLen();
System.out.println("filesize:"+filesize);
String fileowner=filestatus.getOwner();
System.out.println("fileowner:"+fileowner);
SimpleDateFormat formatter=new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
long accessTime=filestatus.getAccessTime();
System.out.println("access time:"+formatter.format(new Date(accessTime)));
long modifyTime=filestatus.getModificationTime();
System.out.println("modify time:"+formatter.format(new Date(modifyTime)));
}
}
运行结果 完成! enjoy it!
|