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;
}
}