zookeeper javaapi 的使用测试
来源:互联网 发布:mac终端是什么意思 编辑:程序博客网 时间:2024/06/18 18:12
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; }}
阅读全文
0 0
- zookeeper javaapi 的使用测试
- zookeeper的安装与JavaAPI的使用
- zookeeper-05-javaAPI使用
- zookeeper javaAPI
- HBase的JavaAPI使用
- zookeeper JavaAPI入门操作
- ZooKeeper级联删除javaAPI
- Zookeeper中javaAPI初试
- zookeeper javaApi/zkclient
- Zookeeper JavaApi 增删改查
- zookeeper javaapi简单小例子
- ES2.x版本的javaAPI使用实例
- zookeeper学习记录-06 Zookeeper JavaAPI操作
- 测试使用zookeeper的c客户端的情况
- 使用zk-smoketest测试zookeeper
- 使用JavaAPI操作HDFS
- 使用JAVAAPI验证XML是否符合DTD的规定
- Java安全_使用JavaAPI完成条目的管理
- 【Linux】常用的文本操作(排序、选取、计数、应用正则表达式查找等)
- 面向接口编程
- android studio 2.x ndk开发二
- osg之osg环境配置
- E. Pig and Palindromes
- zookeeper javaapi 的使用测试
- 量化交易----编程实例:爬取标普500指数股票数据
- 自定义类封装request配合filter解决参数乱码
- 安装numpy
- 需要改变的其实是一种习惯
- War Chess
- LeetCode week 10 : Edit Distance
- 如何配置Bitnami Redmine中的邮件通知服务
- Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated