时间:2019年11月25日 22:56:02
集群搭建
参考地址1伪分布式hadoop集群搭建
参考地址2完全分布式hadoop集群搭建
JAVA API 调用
hdfs依赖如下,可直接从maven空间下载
compile group: 'org.apache.hadoop', name: 'hadoop-client', version: '3.2.0'
主代码如下
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class DataBase_Hdfs {
private FileSystem fs = null;
private String ip = "192.168.40.198"; // 要访问的hdfs的URI(集群中的任何一个IP地址都可以)
public FileSystem init() {
// 构造配置参数对象,设置一个参数:我们要访问的hdfs的URI
// FileSystem.get()方法去构造一个访问hdfs文件系统的客户端,以及hdfs的访问地址
// new Configuration()时,会去加载jar包中的hdfs-default.xml,然后加载classpath下的hdfs-site.xml
Configuration conf = new Configuration();
conf.set("bcgis", "hdfs://" + ip + ":9000");
conf.set("dfs.replication", "3");
// 获取hdfs的访问客户端
try {
fs = FileSystem.get(new URI("hdfs://" + ip + ":9000"), conf, "java");
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
return fs;
}
}
测试代码
import org.apache.commons.io.IOUtils;
import org.apache.hadoop.fs.*;
import org.junit.Test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Test_HDFS {
FileSystem fs = new DataBase_Hdfs().init();
/**
* 上传文件
*/
@Test
public void upload() throws Exception {
// 本地路径和上传路径----------(需要将数据变为流的形式进行上传)
String localpath = "E:\\DemoRecording\\testFileStorage\\JerseyTest\\test1.jpg";
String destpath = "hdfs://192.168.40.198:9000/user/bcgis/test2.jpg";
Path dst = new Path(destpath);
FSDataOutputStream os = fs.create(dst);
FileInputStream is = new FileInputStream(localpath);
IOUtils.copy(is, os);
}
// 下载文件
@Test
public void testDownloadFileToLocal() throws IllegalArgumentException, IOException {
String localpath = "E:\\DemoRecording\\testFileStorage\\JerseyTest\\HDFSdownloadtest.jpg";
fs.copyToLocalFile(new Path("/user/bcgis/test.jpg"), new Path(localpath));
fs.close();
}
// 操作目录/文件
@Test
public void testMkdirAndDeleteAndRename() throws IllegalArgumentException, IOException {
// 创建目录
fs.mkdirs(new Path("/user/bcgis/testbcgis"));
fs.mkdirs(new Path("/user/bcgis/testbcgis1"));
// 删除文件夹 ,如果是非空文件夹,参数2必须给值true
fs.delete(new Path("/user/bcgis/testbcgis"), true);
fs.deleteOnExit(new Path("/user/bcgis/test1.jpg"));
// 重命名文件或文件夹
fs.rename(new Path("/user/bcgis/testbcgis1"), new Path("/user/bcgis/testbcgis_rename"));
}
// 查看目录信息,只显示文件
@Test
public void testListFiles() throws FileNotFoundException, IllegalArgumentException, IOException {
RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
while (listFiles.hasNext()) {
LocatedFileStatus fileStatus = listFiles.next();
System.out.println(fileStatus.getPath().getName());
System.out.println(fileStatus.getBlockSize());
System.out.println(fileStatus.getPermission());
System.out.println(fileStatus.getLen());
BlockLocation[] blockLocations = fileStatus.getBlockLocations();
for (BlockLocation bl : blockLocations) {
System.out.println("block-length:" + bl.getLength() + "--" + "block-offset:" + bl.getOffset());
String[] hosts = bl.getHosts();
for (String host : hosts) {
System.out.println(host);
}
}
}
}
//查看文件及文件夹信息
@Test
public void testListAll() throws FileNotFoundException, IllegalArgumentException, IOException {
FileStatus[] listStatus = fs.listStatus(new Path("/"));
String flag = "d-- ";
for (FileStatus fstatus : listStatus) {
if (fstatus.isFile()) flag = "f-- ";
System.out.println(flag + fstatus.getPath().getName());
}
}
}
注意事项
1、首先需要在本机的host里面加上部署的IP地址 192.168.40.156 hadoop
2、上传文件时,可能会出现以下权限问题
Permission denied: user=java, access=WRITE, inode="/":lqs:supergroup:drwxr-xr-x
解决如下:在集群文件bin
目录建立文件夹如下,然后设置权限为777
hdfs dfs -mkdir /user/bcgis
hadoop fs -chmod 777 /user/bcgis
3、下载文件(复制)的路径问题,需要下载一个hadoop包将里面的bin目录配置到windows系统目录下,设置变量如下
以下是编辑环境变量path路径下添加
4、配置好之后,会遇到配置信息不对的问题,描述如下
Could not locate Hadoop executable: D:\HDFS\hadoop-2.8.5\bin\winutils.exe
解决办法:在以下GitHub地址下载
将其中的hadoop.dll文件和winutils.exe放到本地hadoop文件下的bin目录里面
注意这里面可能会出现版本问题,我当前用的是hadoop-2.8.5
以上为博客全部内容,如有问题请提出共同探讨,谢谢!!!