摘要:背景因?yàn)閷W(xué)習(xí)分布式爬蟲而需要在和上進(jìn)行數(shù)據(jù)存取。操作系統(tǒng)需要為爬蟲準(zhǔn)備存放的的以及用于存放爬取任務(wù)的。三準(zhǔn)備參考的是通過安裝的,系統(tǒng)本身的并沒有包含,所以需要額外指定安裝源或者使用安裝。按照官方文檔進(jìn)行用戶的創(chuàng)建并開啟服務(wù)。
背景:因?yàn)閷W(xué)習(xí)分布式爬蟲而需要在hadoop和mongodb上進(jìn)行數(shù)據(jù)存取。
操作系統(tǒng):tumbleweed update to 20180420+
需要為爬蟲準(zhǔn)備html存放的hadoop的dfs以及用于存放爬取任務(wù)的mongodb。雖然課件已經(jīng)包含redis部分,但實(shí)際課程的進(jìn)度代碼并沒有跟上,所以先解決這兩個問題。
一、準(zhǔn)備hadoop環(huán)境
首先,按照官方要求進(jìn)行單機(jī)版的安裝
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/*
至此單機(jī)版安裝完成,嘗試連接失敗,在小象問答中得到的答復(fù):
配置文件就是在etc下,你可以通過執(zhí)行hadoop
classpath命令,判斷當(dāng)前hadoop能識別的classpath都有哪些,如果已經(jīng)包含了你的配置文件所在的目錄,配置文件就是可以被識別的。3.0版本的HDFS的默認(rèn)監(jiān)聽端口不再是50070,而是9870。 你可以先執(zhí)行jps看下namenode進(jìn)程的進(jìn)程號,然后執(zhí)行netstat -antp | fgrep
pid>查看下namenode在監(jiān)聽什么端口。 如果連namenode服務(wù)都沒啟動起來,需要查看日志排查未啟動成功的原因。
第一個問題:按照參考配置,etc下hdfs-site.xml和core-site.xml打開后內(nèi)容都是空的。
第二個問題:jps命令不存在,需要安裝openjdk-devel包才可以。
第三個問題:netstat命令也不存在,替換命令是ss
解決這些問題后發(fā)現(xiàn)namenode根本沒有啟動,嘗試使用偽分布式部署,繼續(xù)官方文檔如下:
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:etc/hadoop/hdfs-site.xml: fs.defaultFS hdfs://localhost:9000 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
首先要解決的是免密登錄問題:
第一個問題:默認(rèn)下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
即便如此,因?yàn)橹吧闪诵枰诹畹拿荑€,所以成功配置后仍需輸入密鑰口令。經(jīng)測試如果證書需要口令仍不行,重新生成公私鑰對問題解決。
起停服務(wù)
因?yàn)橹挥玫搅薲fs,所以起停服務(wù)只用需要用hadoop目錄中sbin下的start-dfs和stop-dsf即可。
用戶權(quán)限
按照官方配置后仍然在權(quán)限上出錯,沒辦法只能先通過不安全的方法繞過了,如下:
dfs.replication 1 dfs.permissions false
web管理
網(wǎng)上資料給的端口清一色的都是2.x的,3.x的web管理訪問入口是:http://127.0.0.1:9870/
二、測試hadoop寫入
參考:https://hdfscli.readthedocs.i...
在python中使用hadoop本身并不困難,使用的第三方包是hdfs,最困難的是上一步環(huán)境準(zhǔn)備,特別是現(xiàn)在用的新版本除官方文檔外可以參考的內(nèi)容不多。
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
")
三、準(zhǔn)備mongodb
參考:https://en.opensuse.org/MongoDB
suse的mongodb是通過zypper安裝的,系統(tǒng)本身的repo并沒有包含,所以需要額外指定安裝源或者使用one click安裝。
按照官方文檔進(jìn)行用戶的創(chuàng)建并開啟服務(wù)。
四、測試mongodb
首先需要能使用mongo客戶端來查看數(shù)據(jù)庫,參考: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進(jìn)行開發(fā)操作,參考: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"})
實(shí)際使用中插入一般使用insert_one方法,課程中使用的"$set":record方法總是會出錯,不能直接把一個dict放進(jìn)insert_one方法中,也是奇怪。
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/19251.html
摘要:北京大會一年一度的程序員,架構(gòu)師和運(yùn)維工程師技術(shù)盛會北京將于月日在北京舉行。在這里你將深入了解到的技術(shù)精湛以及許多的成功案例。作為主辦方中文社區(qū)的官方合作伙伴,即將亮相北京大會。現(xiàn)在技術(shù)問答子站已經(jīng)開通,有相關(guān)問題,歡迎到該子站下提問。 MongoDB 北京2014大會 一年一度的程序員,架構(gòu)師和運(yùn)維工程師技術(shù)盛會——MongoDB 北京 將于11月22日在北京舉行。在這里你將深入了...
閱讀 1017·2023-04-25 22:27
閱讀 872·2021-11-22 14:56
閱讀 984·2021-11-11 16:54
閱讀 1678·2019-08-30 15:54
閱讀 3500·2019-08-30 13:20
閱讀 1213·2019-08-30 10:55
閱讀 2080·2019-08-26 13:34
閱讀 3281·2019-08-26 11:53