打开IntelliJ IDEA 2018.2.2 x64
创建mvven工程
一 、向hdfs文件系统的写入文件
在pom.xml中添加Hadoop相关架包:
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.6.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.6.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-hdfs -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
如下图所示:
package com.kgc.hdfs;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.FileInputStream;
import java.io.IOException;
//向hdfs文件系统的写入文件
public class TestHDFS {
public static void writeToHDFS(String hdfsFile,//hdfs文件
String hdfsURL,//hdfs地址,默认主节点
String fileName//本机文件
) throws IOException {
Configuration configuration = new Configuration();//设置配置对象
configuration.set("fs.defaultFs","hdfsURL");
//获取文件
FileSystem fileSystem = FileSystem.get(configuration);
//判断文件是否存在,存在则写入
if (fileSystem.exists(new Path(hdfsFile))){
//读出来
FSDataOutputStream fsDataOutputStream = fileSystem.create(new Path(hdfsFile));
//将文件装到hdfs中
FileInputStream fis = new FileInputStream(fileName);
byte [] bytes = new byte[1024];
//首次读
int count = fis.read(bytes,0,1024);//最开始读到1024
while (count>0){//读到
fsDataOutputStream.write(bytes,0,1024);//读到的写入
count = fis.read(bytes,0,1024);//前面的流读过,重新对下一个读
}
//关流
fis.close();
fsDataOutputStream.close();
fileSystem.close();
}
}
public static void main(String[] args) throws IOException {
writeToHDFS(args[0],args[1],args[2]);
}
}
注意:选择apach下的Hadoop
打架包
打完之后:可以看到生成的架包
拖到虚拟机目录
虚拟机的操作:
网页验证
读文件完成
虚拟机查看
二、向hdfs写入linux系统本地文件,其余操作和‘一’相同
package HDFS.hdfs;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.FileInputStream;
import java.io.IOException;
public class WriteToHdfs {
public static void writeToHdfs(String hdfsFile,String hdfsUrl,String fileName) throws IOException {
Configuration cfg=new Configuration();
cfg.set("fs.defaultFS",hdfsUrl);
FileSystem fs = FileSystem.get(cfg);
//hdfs存在才可以写入
if (fs.exists(new Path(hdfsFile))) {
FSDataOutputStream fsdos = fs.create(new Path(hdfsFile));
FileInputStream fis = new FileInputStream(fileName);
byte[] bytes=new byte[2048];
//首次读
int count=fis.read(bytes,0,2048);
while (count>0){
fsdos.write(bytes,0,count);
count=fis.read(bytes,0,2048);
}
fis.close();
fsdos.close();
fs.close();
}
}
public static void main(String[] args) throws IOException {
writeToHdfs(args[0],args[1],args[2]);
}
}