idea操作hdfs-------周东海

1.先配置windows的环境变量

2.创建工程

3.导入lib配置包

4.输入Java语句

import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URI; import java.net.URISyntaxException; 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.Before; import org.junit.Test; windows系统的hadoop可以不配置系统变量,直接配置Path public class JavaToHDFS{   //定义类变量:文件系统,用来连接hdfs   FileSystem fs=null;   //预处理 @Before   public void init() throws Exception{     fs=FileSystem.get(new URI("hdfs://192.168.153.129:9000"),new Configuration(),"root");   }   @Test   public void mkdir(){     System.out.println(fs)   }   @Test   public void testUpload() throws Exception{ //上传文件       InputStream in = new FileInputStream("/root/install.log");     OutputStream out = fs.create(new Path("/log123.log"));     IOUtils.copyBytes(in, out, 1024, true);   }   @Test   public void testMkdir() throws IllegalArgumentException, IOException{  //创建文件     boolean flag = fs.mkdirs(new Path("/a/aa"));System.out.println(flag);   }   @Test   public void testDel() throws IllegalArgumentException, IOException{  //删除文件     boolean flag = fs.delete(new Path("/a"), true);     System.out.println(flag);   }   public static void main(String[] args) throws Exception {       FileSystem fs = FileSystem.get(new URI("hdfs://itcast01:9000"), new Configuration());     InputStream in = fs.open(new Path("/jdk"));     OutputStream out = new FileOutputStream("/home/jdk1.7.tar.gz");     IOUtils.copyBytes(in, out, 4096, true);}   }
经验分享 程序员 微信小程序 职场和发展