在tumbleweed中准备hadoop和mongodb环境

背景:因为学习分布式爬虫而需要在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 <namenode
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:

<configuration>
    <property>
        <name>fs.defaultFS</name>
        <value>hdfs://localhost:9000</value>
    </property>
</configuration>

etc/hadoop/hdfs-site.xml:

<configuration>
    <property>
        <name>dfs.replication</name>
        <value>1</value>
    </property>
</configuration>

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/<username>
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> 
    Enter passphrase (empty for no passphrase): <Enter> 
    Enter same passphrase again: <Enter> 
    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> 
        Enter same passphrase again: <Enter> 
        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> 
    Enter same passphrase again: <Enter> 
    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<tt> command still imports to <tt>%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即可。

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

<configuration>
    <property>
        <name>dfs.replication</name>
        <value>1</value>
    </property>
    <!--    -->
    <property>
        <name>dfs.permissions</name>
        <value>false</value>
    </property>

</configuration>

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'<h1>hello</h1>')

三、准备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://segmentfault.com/a/1190000014767387
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞