3. ● spark on kubernetes 의 장점 - 요청 간 resource 분리
● Batch 작업은 spark operator를 사용
● Query data set을 활용하고 싶음
● 일반적인 조회는 presto 에서 처리가능하지만
a. query 별로 간섭이 없음
b. resource 를 지정 or dynamicAllocation 사용가능
● Jupyter notebook에서 spark 사용
a. notebook server as driver
- 만들기는 쉬우나 notebook server 에 spark 실행시 driver역할을 해야하는 부하 발생
b. Notebook과 Driver를 분리하고 싶다
spark on kubernetes query service
4. Pyspark을 이용한 방법
local에 해당하는 Jupyter Notebook 서버에서
JVM위 Spark Driver와 Python간 Socket 채
널을 통한 데이터 송수신
- Spark Context를 python에서 직접 이용
하기 때문에 SQL 레벨에서 테스팅에
적합
- 다만 python process에서 Dataframe
및 RDD에 접근 시, socket으로 데이터
를 가져오므로 느림
☑ python code로 정의한 UDF의 경우 python process에서만 처리될 수 있으므로, number
of rows * 데이터전송 처리 부하가 걸려 큰 병목으로 작용 될 수 있음(최대한 지양)
5. ● spark context에서 jvm 설정
a. https://spark.apache.org/docs/3.1.1/api/python/reference/api/pyspark.
SparkContext.html
b. class pyspark.SparkContext(master=None, appName=None, sparkHome=None, pyFiles=None,
environment=None, batchSize=0, serializer=PickleSerializer(), conf=None, gateway=None, jsc=None,
profiler_cls=<class 'pyspark.profiler.BasicProfiler'>)
c. gateway: py4j.java_gateway.JavaGateway, optional
i. Use an existing gateway and JVM, otherwise a new JVM will be instantiated. This is only used internally.
remote spark 구성
6. remote spark 구성
● spark server에서 spark driver 를 위한 statefulset 실행
○ 관련 library :
● spark image 생성시 javagateway 연결을 위한 daemon실행
○ 관련 library : https://github.com/cce199/PysparkGateway.git
○ spark docker build시 entrypoint.sh에 driver 실행 부분에
pyspark-gateway start
추가
9. spark database server 개발 이유?
● 현상 : 사용자가 팀에서 서비스하는 Presto를 안씀
○ 왜? presto 는 worker수가 지정되어있어(autoscale 지정할수 있기는 한데 오래걸림. 물론 spark
server도 pod띄우는데 오래 걸리기는 함)
○ 왜? tableau에서 조회하는 데이터가 큰 query를 실행시키면 presto실패
○ 결과 : 그래서, Tableau에서 무거운 쿼리는 Athena를 사용
● 해결방안
○ 요청에 따라 resource가 Load에 맞춰 요청되고, 요청간 Resource가 분리된 Query Service 가
DB형태(Athena connector가 있는) 로 제공되면 Athena를 안쓰고 팀에서 Heavy 한 쿼리를 서비
스하는 플랫폼 제공가능
● 해결방안을 찾는 과정
○ Spark on K8s가 resource가 서로 독립된 형태로 실행되고, resource도 요청에 따라 지정할수
있음
○ Spark Server는 서로 독립적인 실행이 아니라서 제외
○ Apache Kyuubi project 가 비슷한 컨셉인데 spark image를 서버에 설치해야해서, 생각한 개념
이랑 달라, drop
10. ● spark on kubernetes query service는 ch1에서 개발
● db 형태의 서비스 개발
○ java 로 개발은 어려움
○ python개발이 선호
○ JDBC가 범용적이기는 하나, 이름부터가 java 로 시작하는데 python으로 만들기는 어려워 보이
기도 하고 java로 만드려고 해도 protocol 에 대하 reference 부족
○ 결론 : thrift protocol이 multi 언어를 위해 나온 protocol인데 db 형태의 reference(Hive)가 있어서
python을 이용한 Hive/Spark thrift Server 를 가져다 쓰기로
spark database server 개발 이유?
11. Apache thrift?
- The Apache Thrift software framework, for scalable cross-language services development, combines a software
stack with a code generation engine to build services that work efficiently and seamlessly between C++, Java,
Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, OCaml and Delphi and other
languages.
- Writing a .thrift file
After the Thrift compiler is installed you will need to create a thrift file. This file is an interface definition made up of thrift types and Services. The
services you define in this file are implemented by the server and are called by any clients. The Thrift compiler is used to generate your Thrift File into
source code which is used by the different client libraries and the server you write. To generate the source from a thrift file run
thrift --gen <language> <Thrift filename>
13. - Hive github
- thrift 에서 JDBC 통신/함수에 대한 정의. spark 과 동일하게 사용가능
- https://github.com/cce199/hive.git
- service-rpc/src/gen/thrift/gen-py/TCLIService/TCLIService.py
- thrift 도 hive thrift에 적용하려면 수정필요
- 실제 hiveserver2 와 packet 전송format이 약간 다름
- https://github.com/cce199/thrift.git
- ForkServer
- default 로 설정된 multi thread 방식에서는 multi spark context 가 불가능하기 때문에 ForkServer로
multi process 방식으로 서비스
- 접속
- query tool - DBeaber Hadoop / Apache Spark)
- python library - PyHive
JDBC Hive server using thrift
15. ● spark on k8s 를 database server형태로 제공 시 장점/차이점
a. athena spark, glue spark 대체 가능
i. 비용/성능은 비교해보지 않음
ii. 비용/성능을 떠나서 대안은 될수 있음
b. batch에서 활용되는 spark operator 대체 가능
● Database를 만들었을때 할수 있는것들
a. presto on request
b. ML model serving
i. tableau 등 BI tool에서 db 형태로 ML데이터 제공
ii. ex) from model where parameter1 = ? and parameter2 = ?
spark database server
16. spark driver/executor 실행 방안
1. connection에서
a. 설정마다 resource 정하면 resource 종류별로 별도의 connection name 을 사용해야함. ex)
dbeaver같은 db tool에서 connection설정에 resource정보를 넣는다면 resource별로 다른
connection이름이 필요
2. query 마다
a. query마다 설정을 추가해야하기도 해야하고 spark resource 가 뜨는데 걸리는 시간이 있는데
기존에 생성된 resource를 안쓰는건 비효율적일듯? 필요한 경우도 있겠지만. tableau 같은경우
대부분 쿼리 1개니까 별 차이는 없음
3. 첫번째 쿼리
a. 첫번째 쿼리 = resource(서버) 요청과 같은 개념으로 생각
b. dynamic allocation사용하면 driver 설정만 해주면 됨
17. spark db server 실행 demo
- dbeaver
- dbeaver에서 thrift hive/spark server connection으로 테스트함
- query example)
- 처음실행시에 spark option 설정 기능추가
/* { "spark_driver_cores":"5", "spark_driver_memory":"40G"
, "memoryOverheadFactor" : "0.1"
, "spark_executor_instances": "5" }
*/
select base_date, count(*) from common.tbl where base_date = '2023-08-20'
group by 1 order by 1