zookeeper javaapi 的使用测试

http://search.maven.org/中查询到zookeeper的依赖

在pom.xml中加入

  <dependency>
      <groupId>org.apache.zookeeper</groupId>
      <artifactId>zookeeper</artifactId>
      <version>3.4.5</version>
  </dependency>

创建测试类并加入代码

package com.unique.hadoop;

import junit.framework.TestCase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.hdfs.DistributedFileSystem;
import org.apache.hadoop.hdfs.server.common.JspHelper;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.util.Options;
import org.apache.hadoop.util.Progressable;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;

public class HdfsApp {

    public  static final String HDFS_PATH = "hdfs://hadoop0:8020";

    FileSystem fileSystem = null;
    Configuration configuration = null;



    @Test
    public void urlCat() throws Exception{
        URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());
        URL url = new URL("hdfs://hadoop0:8020/tmp/urltest.txt");
        InputStream in = url.openStream();
        IOUtils.copyBytes(in,System.out,4096,false);
        IOUtils.closeStream(in);
    }


    public void mkdir() throws Exception{
        fileSystem.mkdirs(new Path("/hdfsapi/test"));
    }

    @Test
    public void createFile()throws Exception{
        FSDataOutputStream outputStream = fileSystem.create(new Path("/hdfsapi/test/aaa.txt"));
        outputStream.write("hello hadoop".getBytes());
        outputStream.flush();
        outputStream.close();
    }

    @Test
    public void cat()throws Exception{
        FSDataInputStream inputStream = fileSystem.open(new Path("/tmp/urltest.txt"));
        IOUtils.copyBytes(inputStream,System.out,1024);
        inputStream.close();
    }

    /**
     *
     * 重命名
     */

    @Test
    public void copyFromLocalFile()throws Exception{
        Path localPath = new Path("D:/c.txt");
        Path hdfsPath = new Path("/hdfsapi/test/");
        fileSystem.copyFromLocalFile(localPath,hdfsPath);
    }


    @Test
    public void copyFromLocalFileWithProgress()throws Exception{
        InputStream in = new BufferedInputStream(
                new FileInputStream("D:/programmer/java/maven/apache-maven-3.5.0-bin.zip"));
        FSDataOutputStream outputStream =  fileSystem.create(new Path("/hdfsapi/test/maven.zip"), new Progressable() {
            public void progress() {
                System.out.println(".");
            }
        });
        IOUtils.copyBytes(in,outputStream,4096);
    }
    @Test
    public void copyToLocalFile() throws Exception{
        Path localPath = new Path("D:/c.txt");
        Path hdfsPath = new Path("/hdfsapi/test/cc.txt");
        fileSystem.copyToLocalFile(false,hdfsPath,localPath,true);
    }

    @Test
    public void delete()throws  Exception{
        fileSystem.delete(new Path("/tmp/10000_access.log"),true);
    }
    @Test
    public void listFile()throws Exception{

        FileStatus[] fileStatuses = fileSystem.listStatus(new Path("/hdfsapi/"));
        for(FileStatus fileStatus : fileStatuses){
            String isDir = fileStatus.isDirectory() ? "directory":"file";
            short replication  = fileStatus.getReplication();
            long len = fileStatus.getLen();
            String path = fileStatus.getPath().getName();
            System.out.println(isDir+"\t"+replication+"\t"+len+"\t"+path);

        }
    }
    @Test
    public void rename() throws Exception{
        Path oldPath = new Path("/hdfsapi/test/aaa.txt");
        Path newPath = new Path("/hdfsapi/test/bbbb.txt");
        fileSystem.rename(oldPath,newPath);
    }

    @Before
    public void setUp() throws Exception{
        System.out.println("setUp");
        configuration = new Configuration();
        configuration.addResource("core-site.xml");
        configuration.addResource("hdfs-site.xml");
        fileSystem = FileSystem.get(new URI(HDFS_PATH),configuration,"hadoop");
    }

    @After
    public void tearDown()throws Exception{
        System.out.println("teatDown");
        configuration = null;

        fileSystem = null;

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值