Bootcamp

[Spark] spark 다루기

K_Hyul 2024. 1. 18. 10:35
728x90

terminal에서 pyspark를 했을 때 주피터 노트북으로 연결이 안되는 이슈가 있어서

 

주피터 노트북에서 pyspark를 실행했다

import findspark
findspark.init()

from pyspark.sql import SparkSession

# SparkSession을 생성하면서 필요한 설정을 지정
spark = SparkSession.builder \
    .appName("example") \
    .master("yarn") \
    .config("spark.executor.instances", 3) \
    .getOrCreate()

 

그 후 어제 보냈던 데이터를 가져왔다.

df = spark.read.csv("/data/bicycle/*", encoding='cp949', inferSchema = True, header=True)

 

df 는 이제 내꺼다

df.select(col("자전거번호"), col("성별")).head(3)

# df2.isnull().sum() 과 아래와 동일한 기능 spark라 명령어 다름
df2 = df.select([count(when(col(c).contains('None') | \
                            col(c).contains('NULL') | \
                            (col(c) == '' ) | \
                            col(c).isNull(), c 
                           )).alias(c)
                    for c in df.columns])


df2.show()

df2.explain()  #동작 계획

df.groupby("성별").agg(count("성별").alias("값")).show()

 

# witColumn : 새로운 열 추가 혹은 기존에 있는걸 수정
# when : 조건
# otherwise : when 함수의 조건이 만족되지 않을 때, 

df = df.withColumn('성별', when(col('성별') == 'm', 'M').otherwise(col('성별')))

df.groupby("성별").agg( count("성별").alias("값")).show()
728x90

'Bootcamp' 카테고리의 다른 글

[AWS] kubectl 연습  (0) 2024.02.05
[Kubernetes] 쿠버네티스 마스터 설정하기  (1) 2024.02.01
[MySQL] 네트워크, mysql  (1) 2023.12.28
[Crawling] - 간단한 크롤링  (1) 2023.12.27
[Linux] Linux & Jupyter Notebook  (1) 2023.12.27