资讯专栏INFORMATION COLUMN

在tumbleweed中准备hadoop和mongodb环境

DevTalking / 2129人阅读

摘要:背景因为学习分布式爬虫而需要在和上进行数据存取。操作系统需要为爬虫准备存放的的以及用于存放爬取任务的。三准备参考的是通过安装的,系统本身的并没有包含,所以需要额外指定安装源或者使用安装。按照官方文档进行用户的创建并开启服务。

背景:因为学习分布式爬虫而需要在hadoop和mongodb上进行数据存取。
操作系统:tumbleweed update to 20180420+

需要为爬虫准备html存放的hadoop的dfs以及用于存放爬取任务的mongodb。虽然课件已经包含redis部分,但实际课程的进度代码并没有跟上,所以先解决这两个问题。


一、准备hadoop环境

首先,按照官方要求进行单机版的安装
http://hadoop.apache.org/docs...


Hadoop: Setting up a Single Node Cluster.

Purpose
Prerequisites
    Supported Platforms
    Required Software
    Installing Software
Download
Prepare to Start the Hadoop Cluster
Standalone Operation
Pseudo-Distributed Operation
    Configuration
    Setup passphraseless ssh
    Execution
    YARN on a Single Node
Fully-Distributed Operation

Purpose

This document describes how to set up and configure a single-node Hadoop installation so that you can quickly perform simple operations using Hadoop MapReduce and the Hadoop Distributed File System (HDFS).
Prerequisites
Supported Platforms

GNU/Linux is supported as a development and production platform.
Hadoop has been demonstrated on GNU/Linux clusters with 2000 nodes.

Windows is also a supported platform but the followings steps are for Linux only. To set up Hadoop on Windows, see wiki page.

Required Software

Required software for Linux include:

Java™ must be installed. Recommended Java versions are described at
HadoopJavaVersions.

ssh must be installed and sshd must be running to use the Hadoop scripts that manage remote Hadoop daemons if the optional start and
stop scripts are to be used. Additionally, it is recommmended that
pdsh also be installed for better ssh resource management.

Installing Software

If your cluster doesn’t have the requisite software you will need to install it.

For example on Ubuntu Linux:

  $ sudo apt-get install ssh
  $ sudo apt-get install pdsh

Download

To get a Hadoop distribution, download a recent stable release from one of the Apache Download Mirrors.
Prepare to Start the Hadoop Cluster

Unpack the downloaded Hadoop distribution. In the distribution, edit the file etc/hadoop/hadoop-env.sh to define some parameters as follows:

# set to the root of your Java installation

export JAVA_HOME=/usr/java/latest

Try the following command:

$ bin/hadoop

This will display the usage documentation for the hadoop script.

Now you are ready to start your Hadoop cluster in one of the three supported modes:

Local (Standalone) Mode
Pseudo-Distributed Mode
Fully-Distributed Mode

Standalone Operation

By default, Hadoop is configured to run in a non-distributed mode, as a single Java process. This is useful for debugging.

The following example copies the unpacked conf directory to use as input and then finds and displays every match of the given regular expression. Output is written to the given output directory.

  $ mkdir input
  $ cp etc/hadoop/*.xml input
  $ bin/hadoop jar share/hadoop/mapreduce/hadoop-mapreduce-examples-3.1.0.jar grep input output "dfs[a-z.]+"
  $ cat output/*

至此单机版安装完成,尝试连接失败,在小象问答中得到的答复:

配置文件就是在etc下,你可以通过执行hadoop
classpath命令,判断当前hadoop能识别的classpath都有哪些,如果已经包含了你的配置文件所在的目录,配置文件就是可以被识别的。

3.0版本的HDFS的默认监听端口不再是50070,而是9870。 你可以先执行jps看下namenode进程的进程号,然后执行netstat -antp | fgrep pid>查看下namenode在监听什么端口。 如果连namenode服务都没启动起来,需要查看日志排查未启动成功的原因。


第一个问题:按照参考配置,etc下hdfs-site.xml和core-site.xml打开后内容都是空的。

第二个问题:jps命令不存在,需要安装openjdk-devel包才可以。

第三个问题:netstat命令也不存在,替换命令是ss

解决这些问题后发现namenode根本没有启动,尝试使用伪分布式部署,继续官方文档如下:


Pseudo-Distributed Operation

Hadoop can also be run on a single-node in a pseudo-distributed mode where each Hadoop daemon runs in a separate Java process.
Configuration

Use the following:

etc/hadoop/core-site.xml:


    
        fs.defaultFS
        hdfs://localhost:9000
    


etc/hadoop/hdfs-site.xml:


    
        dfs.replication
        1
    

Setup passphraseless ssh

Now check that you can ssh to the localhost without a passphrase:

$ ssh localhost

If you cannot ssh to localhost witho$ ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa
$ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
$ chmod 0600 ~/.ssh/authorized_keys
ut a passphrase, execute the following commands:

$ ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa
$ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
$ chmod 0600 ~/.ssh/authorized_keys

Execution

The following instructions are to run a MapReduce job locally. If you want to execute a job on YARN, see YARN on Single Node.

Format the filesystem:

$ bin/hdfs namenode -format

Start NameNode daemon and DataNode daemon:

$ sbin/start-dfs.sh

The hadoop daemon log output is written to the $HADOOP_LOG_DIR directory (defaults to $HADOOP_HOME/logs).

Browse the web interface for the NameNode; by default it is available at:
NameNode - http://localhost:9870/
Make the HDFS directories required to execute MapReduce jobs:

$ bin/hdfs dfs -mkdir /user
$ bin/hdfs dfs -mkdir /user/
Copy the input files into the distributed filesystem:

  $ bin/hdfs dfs -mkdir input
  $ bin/hdfs dfs -put etc/hadoop/*.xml input

Run some of the examples provided:

$ bin/hadoop jar share/hadoop/mapreduce/hadoop-mapreduce-examples-3.1.0.jar grep input output "dfs[a-z.]+"

Examine the output files: Copy the output files from the distributed filesystem to the local filesystem and examine them:

$ bin/hdfs dfs -get output output
$ cat output/*

or
View the output files on the distributed filesystem:

$ bin/hdfs dfs -cat output/*

When you’re done, stop the daemons with:

$ sbin/stop-dfs.sh

首先要解决的是免密登录问题:

第一个问题:默认下sshd是不启动的,手工启动起来。

第二个问题:官方文档的免密登录是无效的,正确方法是:https://en.opensuse.org/SDB:OpenSSH_public_key_authentication

如下:


Key generation
If ssh-keygen is used without any arguments, a 2048 bit RSA key will be generated. The private key will be stored under ~/.ssh/id_rsa and the public key under ~/.ssh/id_rsa.pub. Based upon your needs, you can choose to set a password. Leaving the lines blank will cause no password to be set.

$ ssh-keygen 
    Enter file in which to save the key (/home/your_user/.ssh/id_rsa):  
    Enter passphrase (empty for no passphrase):  
    Enter same passphrase again:  
    Your identification has been saved...

Custom key pairs
You might want to use a different size of key (bigger or smaller), DSA or save your key under a different name.
To create a RSA key pair with size 1152 under name /home/your_user/.ssh/my_id:

 $ ssh-keygen -t rsa -b 1152 -f ~/.ssh/my_id 
        Generating public/private rsa key pair.
        Enter passphrase (empty for no passphrase):  
        Enter same passphrase again:  
        Your identification has been saved in /home/your_user/.ssh/my_id.
        Your public key has been saved in /home/your_user/.ssh/my_id.pub.
        ...

If you want to use DSA, you are limited to a 1024 bits key size.

To create a DSA key pair with size 1024 under name /home/your_user/.ssh/id_dsa:

$ ssh-keygen -t dsa -f ~/.ssh/id_dsa 
    Generating public/private dsa key pair.
    Enter passphrase (empty for no passphrase):  
    Enter same passphrase again:  
    Your identification has been saved in /home/your_user/.ssh/id_dsa.
    Your public key has been saved in /home/your_user/.ssh/id_dsa.pub.
    ...

Upload your key
In order to use your generated key for authentication, your public key is to be uploaded. If you did not create a custom key pair, as mentioned above, issue:

$ ssh-copy-id user@ssh.yourserver.org

Password:
Now try logging into the machine, with "ssh "user@ssh.yourserver.org"", and check in:

    ~/.ssh/authorized_keys

to make sure we haven"t added extra keys that you weren"t expecting.

The "user@" part can be omitted if you want to use the same username as you are currently logged in to.

Custom key pairs
If you used one of the above examples to create a custom key, you should use the -i ~./ssh/my_id.pub option, where my_id.pub is to be replaced by your custom key name (Eg. id_dsa.pub).

$ ssh-copy-id -i ~/.ssh/my_id.pub user@ssh.yourserver.org

Custom settings
OpenSSH accepts public key authentication by default. You only need to alter the following settings if you are using a custom keypair or if you want to disable Password authentication completely.

SSHD settings (server)
Warning Since openSSH 5.4 (which comes with openSUSE 11.3), relative paths in configuration are no longer allowed. When pointing to the authorized_keys file make sure you use %h/ in front of the path. Older versions still can do without.
Edit /etc/ssh/sshd_config (as root) on the server and un-comment options you would like to change. If you like to make sure Public key authentication is used, or change it to no here if you would like to disable this completly:

PubkeyAuthentication yes
To change the path and file name to look for authorized keys. Note that the ssh-copy-id command still imports to %h/.ssh/authorized_keys file, even is this settings is changed. (Client software can"t read sshd_config) :

AuthorizedKeysFile %h/.ssh/authorized_keys

Disable Password authentication
If you are going for that extra bit of security, you can choose to disable Password authentication completely.

Warning Make sure a public key is already uploaded to your server and tested, before using the following settings, you may lock yourself out!
The following settings should be already in place. Because openSSH uses PAM in openSUSE, PasswordAuthentication should always be disabled.

PasswordAuthentication no
UsePAM yes

The ChallangeResponseAuthentication option is used instead. (You are being challenged to supply a PAM worthy password.) Set this option to no to disable password authentication.

ChallengeResponseAuthentication no

Save the file and restart the SSHD.

$ /etc/init.d/sshd restart

Read the Renew keys section if you want to upload a new key while Password Authentication is disabled.

Setting SSH
If you want to use DSA or any other custom key name (or path) edit the system wide configuration file /etc/ssh/ssh_config as root on the client. Uncomment and change the following option to your needs:

IdentityFile ~/.ssh/my_id

即便如此,因为之前生成了需要口令的密钥,所以成功配置后仍需输入密钥口令。经测试如果证书需要口令仍不行,重新生成公私钥对问题解决。

起停服务
因为只用到了dfs,所以起停服务只用需要用hadoop目录中sbin下的start-dfs和stop-dsf即可。

用户权限
按照官方配置后仍然在权限上出错,没办法只能先通过不安全的方法绕过了,如下:


    
        dfs.replication
        1
    
    
    
        dfs.permissions
        false
    


web管理
网上资料给的端口清一色的都是2.x的,3.x的web管理访问入口是:http://127.0.0.1:9870/


二、测试hadoop写入
参考:https://hdfscli.readthedocs.i...
在python中使用hadoop本身并不困难,使用的第三方包是hdfs,最困难的是上一步环境准备,特别是现在用的新版本除官方文档外可以参考的内容不多。

from hdfs import *

client = TokenClient("http://127.0.0.1:9870","thanatos",root="/user/thanatos")
client.write("hello.md","Hello, word!")
with client.write("myfile.html") as writer:
    writer.write(b"

hello

")

三、准备mongodb
参考:https://en.opensuse.org/MongoDB
suse的mongodb是通过zypper安装的,系统本身的repo并没有包含,所以需要额外指定安装源或者使用one click安装。
按照官方文档进行用户的创建并开启服务。


四、测试mongodb
首先需要能使用mongo客户端来查看数据库,参考:http://www.runoob.com/mongodb...
几个常用动作:
注:mfw是一个collection。

表操作
show collections
db.mfw.drop()
查询
db.mfw.find({"stauts":"done"}
db.mfw.find().pretty()
修改
db.mfw.update({"_id" : "http://www.mafengwo.cn"},{$set:{"status":"new"}})
删除
db.mfw.remove({"_id":"http://www.mafengwo.cn"})

然后配合pymongo进行开发操作,参考:http://api.mongodb.com/python...

from pymongo import MongoClient

client = MongoClient("127.0.0.1", 27017)
db = client.spider
db.mfw.insert({"_id":"http://www.baidu.com"}, {"$set":"something"})

实际使用中插入一般使用insert_one方法,课程中使用的"$set":record方法总是会出错,不能直接把一个dict放进insert_one方法中,也是奇怪。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/19251.html

相关文章

  • 虚拟机安装OpenSuse Tumbleweed

    摘要:首先下载安装镜像。这里我用的虚拟机软件是。对于初学者来说实在是福音。这时候还可以注意到鼠标焦点在虚拟机内外切换时不再需要按,已经可以自动切换了。系统检测之后会检测系统。由于是虚拟机安装,所以体验非常差,系统卡的几乎什么也不能干。 一说起滚动发行版Linux,大家想到的常常是ArchLinux和Gentoo。滚动发行版的优点是不存在固定版本号,所有软件都可以独立更新,所以整个系统都是最新...

    beita 评论0 收藏0
  • openSUSE升级问题记录

    摘要:安装过程中出现系统盘空间不足,中断了,重启之后系统只能启动到命令行。恢复思路及步骤把的包缓存目录换到其它硬盘。 这几年用MBP比较多,openSUSE没怎么升级,跟最新版隔了几个版本,升级到最新版需要升级多次,准备直接用Tumbleweed,一次到位。 在一篇博文上发现国内有Tumbleweed源了,下载应该会稳定很多,博文上用的是清华的,后来看了中科大也有,选了这个。下载安装包的过程...

    OnlyLing 评论0 收藏0
  • MongoDB Hadoop Connector发布

    摘要:目前已经与生态系统中的一些组件进行了整合,后续还会根据反馈进行更全方便的整合。目前支持以上的版本版本也基本上能够支持。 10gen刚刚发布了MongoDB Hadoop Connector的1.0版本,它是一个中间件产品,用于将MongoDB和Hadoop连接起来,让MongoDB可以方便的使用Hadoop的分布式计算能力。MongoDB Hadoop Connector的主要流程是让Had...

    Hydrogen 评论0 收藏0
  • SegmentFault 即将亮相 MongoDB 北京2014大会 倾力支持 MongoDB

    摘要:北京大会一年一度的程序员,架构师和运维工程师技术盛会北京将于月日在北京举行。在这里你将深入了解到的技术精湛以及许多的成功案例。作为主办方中文社区的官方合作伙伴,即将亮相北京大会。现在技术问答子站已经开通,有相关问题,欢迎到该子站下提问。 MongoDB 北京2014大会 一年一度的程序员,架构师和运维工程师技术盛会——MongoDB 北京 将于11月22日在北京举行。在这里你将深入了...

    Tonny 评论0 收藏0
  • MongoDB、Hbase、Redis等NoSQL优劣势、应用场景

    摘要:它在许多场景下可用于替代统的关系型数据库或键值存储方式。对性能的关注超过对功能的要求。目前由主持开发工作。应用场景最佳应用场景适用于数据变化快且数据库大小可遇见适合内存容量的应用程序。 NoSQL的四大种类 NoSQL数据库在整个数据库领域的江湖地位已经不言而喻。在大数据时代,虽然RDBMS很优秀,但是面对快速增长的数据规模和日渐复杂的数据模型,RDBMS渐渐力不从心,无法应对很多数据...

    Baaaan 评论0 收藏0

发表评论

0条评论

DevTalking

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<