zookeeper javaapi 的使用测试
2017-11-04 00:50
274 查看
http://search.maven.org/中查询到zookeeper的依赖
在pom.xml中加入
创建测试类并加入代码
在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{ FSDataOutputStre 4000 am 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; } }
相关文章推荐
- ZooKeeper Java Api 使用样例
- 六:ZooKeeper的java客户端api的使用
- zookeeper Java客户端API的使用方法
- Zookeeper使用--Java API
- ZooKeeper 学习 (四) ZooKeeper Java客户端API使用
- Zookeeper(四)Java客户端API使用
- Kafka 0.9+Zookeeper3.4.6集群搭建、配置,新Client API的使用要点,高可用性测试,以及各种坑 (转载)
- Kafka 0.9+Zookeeper3.4.6集群搭建、配置,新Client API的使用要点,高可用性测试,以及各种坑 (转载)
- 【ZooKeeper】Java客户端API使用介绍。
- zookeeper(二) java客户端api的使用
- ZooKeeper之Java客户端API使用—删除节点。
- HDFS中Java的API使用测试
- Zookeeper Java Api的使用
- Zookeeper学习之源生API的使用(java与shell操作zookeeper)。
- Kafka 0.9+Zookeeper3.4.6集群搭建、配置,新Client API的使用要点,高可用性测试,以及各种坑
- ZooKeeper之Java客户端API使用—创建会话。
- Zookeeper实践(三)java api 使用
- Zookeeper使用--Java API
- Zookeeper Java API的使用
- ZooKeeper Java API使用