PATH를 설정한다.
{{[#!plain
# export SPARK_HOME=/usr/local/spark
# export PATH=$PATH:$SPARK_HOME/sbin:$SPARK_HOME/bin
}}}
Standalone master 서버를 실행해 보자. 이 서버는 8080 포트로 접근 할 수 있다.
1
2
# $SPARK_HOME/sbin/start-master.sh
starting org.apache.spark.deploy.master.Master, logging to /usr/local/spark/logs/spark-root-org.apache.spark.deploy.master.Master-1-yundream.out
Type "help", "copyright", "credits" or "license" for more information.
20/03/11 00:14:04 WARN Utils: Your hostname, yundream resolves to a loopback address: 127.0.1.1; using 192.168.35.163 instead (on interface wlx002666491c41)
20/03/11 00:14:04 WARN Utils: Set SPARK_LOCAL_IP if you need to bind to another address
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.apache.spark.unsafe.Platform (file:/home/yundream/virtualenvironment/spark/lib/python3.7/site-packages/pyspark/jars/spark-unsafe_2.11-2.4.5.jar) to method java.nio.Bits.unaligned()
WARNING: Please consider reporting this to the maintainers of org.apache.spark.unsafe.Platform
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
20/03/11 00:14:04 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Using Spark's default log4j profile: org/apache/spark/log4j-defaults.properties
Setting default log level to "WARN".
To adjust logging level use sc.setLogLevel(newLevel). For SparkR, use setLogLevel(newLevel).
Welcome to
____ __
/ __/__ ___ _____/ /__
_\ \/ _ \/ _ `/ __/ '_/
/__ / .__/\_,_/_/ /_/\_\ version 2.4.5
/_/
Using Python version 3.7.3 (default, Oct 7 2019 12:56:13)
sc.textFile은 org.spach,spark.SparkContext클래스에서 제공하는 메서드로 텍스트 파일을 읽기 위해서 사용한다. 이 메서드는 로컬 파일 시스템, HDFS 등 다양한 저장소에 접근 할 수 있다.
count 메서드는 RDD로 부터 엘리먼트의 갯수를 반환한다. textFile은 파일의 내용을 읽어서 줄단위로 배열로 저장한다. 결과적으로 README.md 파일의 라인수를 리턴한다. wc로 확인을 해봤다.
코드를 만들기 전에 데이터를 준비하기로 했다. movielens.org 라는 영화등급 사이트가 있는데 여기에서 테스트에 사용 할 데이터를 준비해보자. 먼저 grouplens.org 사이트를 방문한다.
GroupLens Research 는 미네소타 대학교의 컴퓨터 학부에 있는 연구실로 인간과 컴퓨터의 상호작용을 연구한다. 또한 GroupLends는 모바일 및 유비쿼터스 기술, 지리정보 시스템 등의 정보 시스템을 제공한다. GroupLens 랩은 유즈넷 기사 추천 엔진인 "GroupLens" 추천엔진을 개발했으며, 인기영화 추천 사이트인 MovieLens를 구축하여 자동 추천 시스템을 연구 한 최초의 실험실 중 하나다. GroupLens Research 홈페이지의 Featured Projects 섹션에 MovieLens를 확인 할 수 있다.
이후 사이트에 가입하면 추천 서비스를 이용 할 수 있다. 처음 로그인하면, 사용자의 취향을 확인하기 위한 좋아햐는 영화 장르를 선택하게 된다. 이 정보를 토대로 아래와 같이 영화를 추천한다.
사용자는 추천된 각 영화에 별점을 줄 수 있다. 이 별점을 데이터를 이용해서 개인화된 영화 추천 서비스를 만든다.
다시 grouplens.org 홈으로 이동한다. 홈페이지 상단 메뉴에서 datasets를 클릭한다. 여기에서 테스트할 데이터셋을 다운로드 한다. 다양한 크기의 데이터셋이 있는데, 학습이 목적이므로 작은 데이터셋을 선택했다.
SparkConf()를 이용해서 애플리케이션에서 필요한 SparkConf 객체를 생성한다.
setMaster(value) : 마스터 URL을 설정한다. 로컬파일 시스템을 사용하기 때문에 "local"를 설정했다.
setAppName(value) : 애플리케이션 이름을 설정한다.
SparkContext는 Spark 기능의 시작점이다. SparkContext는 Py4J를 사용해서 JVM을 시작하고 JavaSparkContext를 생성한다. PySpark에는 기본적으로 "sc"로 사용 가능한 SparkContext가 있으므로 new Sparkcontext는 작동하지 않는다.
textFile로 읽을 텍스트 파일을 설정했다. u.data 파일 형식은 아래와 같다.
코드에 사용된 람다함수는 파라미터로 받은 문자열 "x"를 split() 메서드를 이용해서 나눈 다음 배열의 두번째 값(평점)을 반환한다. map은 가장 보편적인 transformation 함수다. map 함수는 sub 함수를 전달 할 수 있다. 입력된 모든 데이터에 대해서 sub 함수가 적용된다.
Spark는 Transformation 과 Action 두가지 단계로 구성된다.
1 단계 (Transformation) : 수행하고 싶은 operation으로 RDD를 transformation 한다.
2 단계 (Action) : 선언된 RDD의 결과 값을 출력한다. 출력을 하기 위해서 collect, countByValue(), take() 등등의 함수를 사용 할 수 있다.
앞서 map으로 Transformation을 했는데, 그 결과를 countByValue로 수행해서 출력한다. 이 과정은 아래와 같이 묘사 할 수 있다.
pySpark 코드를 실행해 보자. spark-submit 스크립트는 응용 프로그램을 클러스터에서 제출하기 위해서 사용한다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# spark-submit ratings-counter.py
20/03/16 00:43:31 WARN Utils: Your hostname, yundream resolves to a loopback address: 127.0.1.1; using 192.168.35.163 instead (on interface wlx002666491c41)
20/03/16 00:43:31 WARN Utils: Set SPARK_LOCAL_IP if you need to bind to another address
20/03/16 00:43:32 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Using Spark's default log4j profile: org/apache/spark/log4j-defaults.properties
20/03/16 00:43:32 INFO SparkContext: Running Spark version 2.4.5
20/03/16 00:43:32 INFO SparkContext: Submitted application: RatingsHistogram
20/03/16 00:43:32 INFO SecurityManager: Changing view acls to: yundream
20/03/16 00:43:32 INFO SecurityManager: Changing modify acls to: yundream
20/03/16 00:43:32 INFO SecurityManager: Changing view acls groups to:
20/03/16 00:43:32 INFO SecurityManager: Changing modify acls groups to:
20/03/16 00:43:32 INFO SecurityManager: SecurityManager: authentication disabled; ui acls disabled; users with view permissions: Set(yundream); groups with view permissions: Set(); users with modify permissions: Set(yundream); groups with modify permissions: Set()
20/03/16 00:43:32 INFO Utils: Successfully started service 'sparkDriver' on port 45371.
20/03/16 00:43:32 INFO SparkEnv: Registering MapOutputTracker
20/03/16 00:43:32 INFO SparkEnv: Registering BlockManagerMaster
20/03/16 00:43:32 INFO BlockManagerMasterEndpoint: Using org.apache.spark.storage.DefaultTopologyMapper for getting topology information
......
......
20/03/16 00:43:34 INFO DAGScheduler: ResultStage 0 (countByValue at /home/yundream/workspace/spark/ml-100k/ratings-counter.py:9) finished in 0.503 s
20/03/16 00:43:34 INFO DAGScheduler: Job 0 finished: countByValue at /home/yundream/workspace/spark/ml-100k/ratings-counter.py:9, took 0.533288 s
Contents
1. 소개
2. 개발 환경 세팅
2.1. Python 설치
2.2. JAVA 환경 만들기
3. Apache Spark 설치
4. pyspark 설치
5. pyspark course 코드 다운로드
6. 테스트 데이터 셋 준비하기
7. pyspark 코드 실행
1. 소개
2. 개발 환경 세팅
2.1. Python 설치
2.2. JAVA 환경 만들기
3. Apache Spark 설치
4. pyspark 설치
5. pyspark course 코드 다운로드
6. 테스트 데이터 셋 준비하기
7. pyspark 코드 실행
Recent Posts
Archive Posts
Tags