一、實(shí)驗(yàn)?zāi)康?/h2>
- 掌握在Linux虛擬機(jī)中安裝Hadoop和Spark的方法;
- 熟悉HDFS的基本使用方法;
- 掌握使用Spark訪問本地文件和HDFS文件的方法。
二、實(shí)驗(yàn)具體內(nèi)容
2.1 HDFS常用操作
-
啟動(dòng)Hadoop,在HDFS中創(chuàng)建用戶目錄“/user/hadoop”
cd /usr/local/hadoop/
./bin/hdfs dfs -mkdir -p /user/hadoop

-
在Linux系統(tǒng)的本地文件系統(tǒng)的“/home/hadoop”目錄下新建一個(gè)文本文件test.txt,并在該文件中隨便輸入一些內(nèi)容,然后上傳到HDFS的“/user/hadoop”目錄下;
使用vim命令在本地新建一個(gè)文件,使用hdfs dfs -put將文件上傳到hdfs,使用hdfs dfs -ls命令查看是否上傳成功。
vim /home/hadoop/test.txt
./bin/hdfs dfs -put /home/hadoop/test.txt
./bin/hdfs dfs -ls

-
把HDFS中“/user/hadoop”目錄下的test.txt文件,下載到Linux系統(tǒng)的本地文件系統(tǒng)中的“/home/hadoop/下載”目錄下;
使用hdfs dfs -get
命令下載hdfs文件到本地

-
將HDFS中“/user/hadoop”目錄下的test.txt文件的內(nèi)容輸出到終端中進(jìn)行顯示;
使用hdfs dfs -cat
將文件內(nèi)容輸出到終端顯示

-
在HDFS中的“/user/hadoop”目錄下,創(chuàng)建子目錄input,把HDFS中“/user/hadoop”目錄下的test.txt文件,復(fù)制到“/user/hadoop/input”目錄下;
用hdfs dfs -mkdir /user/hadoop/input
來創(chuàng)建目錄,
用hdfs dfs -cp
來進(jìn)行文件復(fù)制操作

-
刪除HDFS中“/user/hadoop”目錄下的test.txt文件,刪除HDFS中“/user/hadoop”目錄下的input子目錄及其子目錄下的所有內(nèi)容。
hdfs dfs -rm /user/hadoop/test.txt
hdfs dfs -rm -r /user/hadoop/input

2.2 Spark讀取文件系統(tǒng)數(shù)據(jù)(本地和HDFS)
-
下面是我們的test.txt文件的內(nèi)容(該文件已經(jīng)在Linux本地和hdfs中存在了):

-
在pyspark中讀取Linux系統(tǒng)本地文件“/home/hadoop/test.txt”(如果該文件不存在,請先創(chuàng)建),然后統(tǒng)計(jì)出文件的行數(shù);
在shell中依次輸入下面的代碼:
file_path = "file:///home/hadoop/test.txt" # 這是你自己的文件地址
data = sc.textFile(file_path)
print("該文件的行數(shù)為:", data.count())

-
在pyspark中讀取HDFS系統(tǒng)文件“/user/hadoop/test.txt”(如果該文件不存在,請先創(chuàng)建),然后,統(tǒng)計(jì)出文件的行數(shù);
在shell中依次輸入下面代碼:
file_path = "hdfs://localhost:9000/user/hadoop/test.txt"
data = sc.textFile(file_path)
print("該文件的行數(shù)為:", data.count())

-
編寫?yīng)毩?yīng)用程序,讀取HDFS系統(tǒng)文件“/user/hadoop/test.txt”(如果該文件不存在,請先創(chuàng)建),然后,統(tǒng)計(jì)出文件的行數(shù);通過spark-submit提交到Spark中運(yùn)行程序。
創(chuàng)建~/mycode/LineCount.py文件,其中代碼如下:
from pyspark import SparkConf, SparkContext
conf = SparkConf().setMaster("local").setAppName("line count")
sc = SparkContext(conf = conf)
file_path = "hdfs://localhost:9000/user/hadoop/test.txt"
data = sc.textFile(file_path)
print("該文件的行數(shù)為:", data.count())
使用/usr/local/spark/bin/spark-submit ~/mycode/LineCount.py
提交程序文章來源:http://www.zghlxwxcb.cn/news/detail-855280.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-855280.html
啟動(dòng)Hadoop,在HDFS中創(chuàng)建用戶目錄“/user/hadoop”
cd /usr/local/hadoop/
./bin/hdfs dfs -mkdir -p /user/hadoop
在Linux系統(tǒng)的本地文件系統(tǒng)的“/home/hadoop”目錄下新建一個(gè)文本文件test.txt,并在該文件中隨便輸入一些內(nèi)容,然后上傳到HDFS的“/user/hadoop”目錄下;
使用vim命令在本地新建一個(gè)文件,使用hdfs dfs -put將文件上傳到hdfs,使用hdfs dfs -ls命令查看是否上傳成功。
vim /home/hadoop/test.txt
./bin/hdfs dfs -put /home/hadoop/test.txt
./bin/hdfs dfs -ls
把HDFS中“/user/hadoop”目錄下的test.txt文件,下載到Linux系統(tǒng)的本地文件系統(tǒng)中的“/home/hadoop/下載”目錄下;
使用hdfs dfs -get
命令下載hdfs文件到本地
將HDFS中“/user/hadoop”目錄下的test.txt文件的內(nèi)容輸出到終端中進(jìn)行顯示;
使用hdfs dfs -cat
將文件內(nèi)容輸出到終端顯示
在HDFS中的“/user/hadoop”目錄下,創(chuàng)建子目錄input,把HDFS中“/user/hadoop”目錄下的test.txt文件,復(fù)制到“/user/hadoop/input”目錄下;
用hdfs dfs -mkdir /user/hadoop/input
來創(chuàng)建目錄,
用hdfs dfs -cp
來進(jìn)行文件復(fù)制操作
刪除HDFS中“/user/hadoop”目錄下的test.txt文件,刪除HDFS中“/user/hadoop”目錄下的input子目錄及其子目錄下的所有內(nèi)容。
hdfs dfs -rm /user/hadoop/test.txt
hdfs dfs -rm -r /user/hadoop/input
下面是我們的test.txt文件的內(nèi)容(該文件已經(jīng)在Linux本地和hdfs中存在了):
在pyspark中讀取Linux系統(tǒng)本地文件“/home/hadoop/test.txt”(如果該文件不存在,請先創(chuàng)建),然后統(tǒng)計(jì)出文件的行數(shù);
在shell中依次輸入下面的代碼:
file_path = "file:///home/hadoop/test.txt" # 這是你自己的文件地址
data = sc.textFile(file_path)
print("該文件的行數(shù)為:", data.count())
在pyspark中讀取HDFS系統(tǒng)文件“/user/hadoop/test.txt”(如果該文件不存在,請先創(chuàng)建),然后,統(tǒng)計(jì)出文件的行數(shù);
在shell中依次輸入下面代碼:
file_path = "hdfs://localhost:9000/user/hadoop/test.txt"
data = sc.textFile(file_path)
print("該文件的行數(shù)為:", data.count())
編寫?yīng)毩?yīng)用程序,讀取HDFS系統(tǒng)文件“/user/hadoop/test.txt”(如果該文件不存在,請先創(chuàng)建),然后,統(tǒng)計(jì)出文件的行數(shù);通過spark-submit提交到Spark中運(yùn)行程序。
創(chuàng)建~/mycode/LineCount.py文件,其中代碼如下:
from pyspark import SparkConf, SparkContext
conf = SparkConf().setMaster("local").setAppName("line count")
sc = SparkContext(conf = conf)
file_path = "hdfs://localhost:9000/user/hadoop/test.txt"
data = sc.textFile(file_path)
print("該文件的行數(shù)為:", data.count())
使用/usr/local/spark/bin/spark-submit ~/mycode/LineCount.py
提交程序文章來源:http://www.zghlxwxcb.cn/news/detail-855280.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-855280.html
到了這里,關(guān)于HDFS常用操作以及使用Spark讀取文件系統(tǒng)數(shù)據(jù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!