使用JavaAPI上传下载数据
使用JavaAPI上传下载数据
1.新建一个java文件,将示例代码写入其中。
2.运行Test。
3.在hdfs中查看是否上传成功:hdfs dfs -ls /folder1
4.下载操作类似
文件上传/下载的两种方法:
法1:利用java.io流操作
法2:利用hadoop.io.IOUtils流操作
import org.junit.Test;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.junit.Test;
public class TestUpload { @Test public void testUpload1() throws Exception{ Configuration conf = new Configuration(); conf.set("fs.defaultFS","hdfs://bigdata11:9000"); FileSystem client = FileSystem.get(conf); OutputStream out = client.create(new Path("/folder1/a.txt")); InputStream in = new FileInputStream("F:\\bigdata11\\JasonPeng.txt"); byte[] buffer = new byte[1024]; int len = 0; while((len = in.read(buffer))>0) { out.write(buffer,0,len); } out.flush(); out.close(); in.close(); } @Test public void testUpload2() throws Exception{ Configuration conf = new Configuration(); conf.set("fs.defaultFS","hdfs://bigdata11:9000"); FileSystem client = FileSystem.get(conf); OutputStream out = client.create(new Path("/folder1/b.txt")); InputStream in = new FileInputStream("F:\\bigdata11\\JasonPeng.txt"); IOUtils.copyBytes(in,out,1024); } }
package demo; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IOUtils; import org.junit.Test; public class TestDownload { @Test public void testDownload() throws Exception{ Configuration conf = new Configuration(); conf.set("fs.defaultFS","hdfs://bigdata11:9000"); FileSystem client = FileSystem.get(conf); InputStream in = client.open(new Path("/folder1/a.txt")); OutputStream out = new FileOutputStream("F:\\bigdata11\\JasonPeng1.txt"); byte[] buffer = new byte[1024]; //长度 int len = 0; while((len=in.read(buffer)) > 0){ //表示读入了数据,再输出 out.write(buffer,0,len); } out.flush(); out.close(); in.close(); } @Test public void testDownload1() throws Exception{ Configuration conf = new Configuration(); conf.set("fs.defaultFS","hdfs://bigdata11:9000"); FileSystem client = FileSystem.get(conf); InputStream in = client.open(new Path("/folder1/a.txt")); OutputStream out = new FileOutputStream("F:\\bigdata11\\JasonPeng2.txt"); IOUtils.copyBytes(in,out,1024); } }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步