1.创建一个maven项目,导入jar包
<dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>2.6.5</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-hdfs</artifactId> <version>2.6.5</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>2.6.5</version> </dependency> <dependency> <groupId>jdk.tools</groupId> <artifactId>jdk.tools</artifactId> <version>1.8</version> <scope>system</scope> <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath> </dependency>
2.上传文件代码
import java.io.FileInputStream; import java.io.FileOutputStream; 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 org.apache.hadoop.io.IOUtils; public class HdfsText01 { public static void main(String[] args) throws Exception { //1 创建连接 Configuration conf = new Configuration(); //2 连接端口 conf.set("fs.defaultFS", "hdfs://hadoop01:9000"); //3 获取连接对象 FileSystem fs = FileSystem.get(conf); //本地文件上传到 hdfs fs.copyFromLocalFile(new Path("E://haha.txt"), new Path("/output1")); fs.close(); } }
3.使用流上传文件
//流上传文件 FileInputStream in=new FileInputStream("E://haha.txt");//读取本地文件 FSDataOutputStream out = fs.create(new Path("/output2"));//在hdfs上创建路径 byte[] b = new byte[1024*1024]; int read = 0; while((read = in.read(b)) > 0){ out.write(b, 0, read); }
4.将hdfs文件下载到本地
//hdfs文件复制到本地(流) FSDataInputStream in = fs.open(new Path("/output")); FileOutputStream out = new FileOutputStream("E:/mm.txt"); IOUtils.copyBytes(in, out, conf);
注:在最后要关闭流哦