本文主要描述如何实现在一台Linux机器上搭建一个Pseudo-Distributed的Hadoop,在另外一台机器上通过API访问
服务器端环境搭建:
1.版本信息
java:1.8.0_172
Hadoop: 2.9.1
2.设置JAVA_HOME
在~/.bash_profile中追加如下内容
JAVA_HOME=/usr/java/jdk1.8.0_172-amd64
export JAVA_HOME
执行如下命令使上述设置立即生效
source ~/.bash_profile
3.设置hostname
hostname docker05
4.下载hadoop,
点击下载
5.解压下载的文件
$ tar xzvf hadoop-2.9.1.tar.gz
6.修改配置文件
6.1 修改etc/hadoop/core-site.xml文件,追加如下内容
<configuration>
<property>
<name>fs.defaultFS</name>
<value>hdfs://docker05:9000</value>
</property>
</configuration>
6.2 修改etc/hadoop/hdfs-site.xml文件,追加如下内容
<configuration>
<property>
<name>dfs.replication</name>
<value>1</value>
</property>
<property>
<name>dfs.permissions.enabled</name>
<value>false</value>
</property>
</configuration>
6.3 修改etc/hadoop/hadoop-env.sh,将JAVA_HOME修改成正确值
# The java implementation to use.
export JAVA_HOME=/usr/java/jdk1.8.0_172-amd64
7. 配置无密码登录
$ ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
$ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
$ chmod 0600 ~/.ssh/authorized_keys
8. 格式化name node
$ bin/hdfs namenode -format
9.启动 dfs
sbin/start-dfs.sh
客户端环境
1.配置hostname
在客户端机器上也需要设置hostname,如192.168.0.172 docker05
2.工程目录如下
3. 工程pom.xml文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>falcon.chengf</groupId>
<artifactId>simple-hdfs-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.8.1</version>
</dependency>
</dependencies>
</project>
4.测试代码
/**
*
*/
package simple.hdfs.test;
import java.io.IOException;
import java.net.URI;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
/**
* @author: 作者: chengaofeng
* @date: 创建时间:2018-06-01 15:25:47
* @Description: TODO
* @version V1.0
*/
public class HdfsTest {
public static String uri = "hdfs://docker05:9000";
public static void main(String[] args) throws IOException {
uploadLocalFile2HDFS("/Users/chengaofeng/Downloads/hadoop-test1.data", "/chengf/hadoop-test1.data");
}
public static boolean uploadLocalFile2HDFS(String localFile, String hdfsFile) throws IOException {
if (StringUtils.isBlank(localFile) || StringUtils.isBlank(hdfsFile)) {
return false;
}
hdfsFile = uri + hdfsFile;
Configuration config = new Configuration();
FileSystem hdfs = FileSystem.get(URI.create(uri), config);
Path src = new Path(localFile);
Path dst = new Path(hdfsFile);
hdfs.copyFromLocalFile(src, dst);
hdfs.close();
return true;
}
}
5.执行后到服务器端执行 hadoop fs -ls 可以看到文件正常上传了
遇到错误
1.
starting namenodes on docker05
docker05 Error: JAVA_HOME is not set and could not be found.
localhost: Error:JAVA_HOME is not set and could not be found.
原因 hadoop-env.sh中JAVA_HOME的设置有问题
具体的修改办法如下:
vim etc/hadoop/hadoop-env.sh
将语句 export JAVA_HOME=$JAVA_HOME
修改为 export JAVA_HOME=/usr/java/jdk1.8.0_172-amd64
保存后退出。
再次输入start-dfs.sh启动hadoop,则没有报错。
2.
权限问题
Exception in thread "main" org.apache.hadoop.security.AccessControlException: Permission denied: user=chengaofeng, access=WRITE, inode="/":root:supergroup:drwxr-xr-x at org.apache.hadoop.hdfs.server.namenode.FSPermissionChecker.check(FSPermissionChecker.java:350)
解决方法,可暂时去掉权限检查
在 hdfs-site.xml文件中追加
<property>
<name>dfs.permissions.enabled</name>
<value>false</value>
</property>
另一种连接方式,通过hdfs的配置文件来创建Configuration
1.工程目录
其中core-site.xml、hdfs-site.xml是从服务器上copy下来的文件
2.测试类
/**
*
*/
package simple.hdfs.test;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
/**
* @author: 作者: chengaofeng
* @date: 创建时间:2018-06-01 15:25:47
* @Description: TODO
* @version V1.0
*/
public class HdfsTest {
public static void main(String[] args) throws IOException {
Configuration config = new Configuration();
InputStream input = HdfsTest.class.getResourceAsStream("/core-site.xml");
config.addResource(input);
InputStream input2 = HdfsTest.class.getResourceAsStream("/hdfs-site.xml");
config.addResource(input2);
FileSystem hdfs = FileSystem.get(config);
uploadLocalFile2HDFS("/Users/chengaofeng/Downloads/hadoop-test1.data", "/chengf/hadoop-test2.data", hdfs);
System.out.println("upload ok");
}
public static boolean uploadLocalFile2HDFS(String localFile, String hdfsFile, FileSystem hdfs) throws IOException {
if (StringUtils.isBlank(localFile) || StringUtils.isBlank(hdfsFile)) {
return false;
}
Path src = new Path(localFile);
Path dst = new Path(hdfsFile);
hdfs.copyFromLocalFile(src, dst);
hdfs.close();
return true;
}
}
- 大小: 32.4 KB
- 大小: 19.4 KB
- 大小: 38.2 KB
分享到:
相关推荐
Hadoop分布式环境搭建教程一
hadoop伪分布式视频教程二
前端开源库-pseudo-elements伪元素,所有CSS伪元素的列表。
前端开源库-has-pseudo-class具有伪类,确定选择器是否包含伪类
Pseudo-Mask Matters in Weakly-Supervised Semantic Segmentation
2016_Pseudo-Random High-Frequency Square-Wave Voltage Injection Based Sensorless Control of IPMSM Drives for Audible Noise Reduction 王教授的论文 2016_Pseudo-Random High-Frequency Square-Wave ...
面向信息检索的群体伪相关反馈技术,万小军,,伪相关反馈技术已经成功应用于改善信息检索质量。现有技术基于单一的搜索引擎(模型),并假设该引擎返回的顶端文档与查询相关。
@ shimyshack / tailwindcss-pseudo-element-plugin 提供一个插件before和after的变体以及pseudo-content-{value}实用工具类,顺风CSS。 安装 从npm安装插件: # Using npm npm install @shimyshack/tailwindcss-...
前端开源库-pseudo-classes.zip
前端开源库-pseudo-elements.zip
python库。 资源全名:pseudo-python-0.2.12.tar.gz
前端开源库-has-pseudo-element具有伪元素,确定选择器是否包含伪元素。
前端开源库-pseudo-classes伪类,获取所有CSS伪类的列表。
tailwindcss-pseudo-elements TailwindCSS插件,添加伪元素的变体( ::before , ::after , ::first-letter等)。 用法 安装 NPM npm install tailwindcss-pseudo-elements --save-dev 纱 yarn add tailwindcss-...
是采用伪随机编码结构光照明主动视觉技术,用编码结构光照明被测场景,实现动态三维场景的重建
作者: , , , , , , , 和 引文@inproceedings{qian2020end, title={End-to-End Pseudo-LiDAR for Image-Based 3D Object Detection}, author={Qian, Rui and Garg, Divyansh and Wang, Yan and You, Yurong ...
资源分类:Python库 所属语言:Python 资源全名:pseudo-0.2.4-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
Pseudo-LiDAR from Visual Depth Estimation.pdf
资源分类:Python库 所属语言:Python 资源全名:pseudo-python-0.2.16.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
资源分类:Python库 所属语言:Python 资源全名:pseudo-python-0.2.14.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059