用eclipse在HADOOP中的一些系统指令操作

来源:互联网 发布:美工自学多久能自己做 编辑:程序博客网 时间:2024/06/05 16:47
import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.net.URI;import java.net.URISyntaxException;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.FSDataInputStream;import org.apache.hadoop.fs.FSDataOutputStream;import org.apache.hadoop.fs.FileStatus;import org.apache.hadoop.fs.FileSystem;import org.apache.hadoop.fs.Path;import org.apache.hadoop.fs.permission.FsPermission;import org.apache.hadoop.io.IOUtils;import org.apache.hadoop.util.Progressable;import org.junit.Test;public class HDFS_Hadoop_Api {    public static void main(String[] args) {    }    //从hdfs中读取文件    @Test    public void readFileByAPI2() throws IOException{        Configuration conf = new Configuration();        conf.set("fs.defaultFS", "hdfs://192.168.43.201/");        FileSystem fs = FileSystem.get(conf);        Path path = new Path("/user/centos/1.log");        FSDataInputStream fis = fs.open(path);        ByteArrayOutputStream baos = new ByteArrayOutputStream();        IOUtils.copyBytes(fis, baos, 1024);        System.out.println(new String(baos.toByteArray()));        fis.close();        baos.close();    }//  @Test//  public void readFileByAPI2() throws IOException, InterruptedException, URISyntaxException{//      Configuration conf = new Configuration();//      设置主节点IP//      conf.set("fs.defaultFS", "hdfs://192.168.43.201/");//      "centos是hadoop的操作用户"//      FileSystem fs = FileSystem.get(new URI("hdfs://192.168.43.201/"), conf, "centos");//      fs.mkdirs(new Path("/user/centos/use"));//      fs.delete(new Path("/user/centos/use"),true);//      //  }}
原创粉丝点击