---
title: 执行Spark引擎业务
description: "使用TPC-H 1T数据的非分区表作为测试表，测试SQL为tpch-sql6，示例执行Spark 3.1.1引擎业务。"
url: https://www.hikunpeng.com/document/detail/zh/kunpengboostkithistory/2400/bds/kunpengbds_omniruntime_20_0124.html
sourcePath: /source/zh/kunpengboostkithistory/2400/bds/kunpengbds_omniruntime_20_0124.html
indexId: aea5dc8d40c4ed5b91d2b64443e63c51abc08f8ddbd4a34b921c19f94ccc4e0378
---
# 执行Spark引擎业务

使用TPC-H 1T数据的非分区表作为测试表，测试SQL为tpch-sql6，示例执行Spark 3.1.1引擎业务。

Spark算子下推使用spark-sql命令来执行。

本次任务示例使用TPC-H的1T数据的非分区表作为测试表，测试SQL为tpch-sql6。

相关的表信息如表1所示。


**表1 相关表信息**

| 表名 | 表格式 | 总行数 | 占用空间 |
| --- | --- | --- | --- |
| lineitem | orc | 5999989709 | 169.6G |


#### Spark 3.1.1步骤

由于Spark 3.1.1 Yarn模式下不打印INFO级别的日志信息，所以Spark 3.1.1需要做日志重定向。


1. 在“/usr/local/spark/conf”目录下，定义日志文件log4j.properties。

  a. 新建日志文件log4j.properties。
    1 2 cd /usr/local/spark/conf vi log4j.properties

  b. 按“i”进入编辑模式，在文件中添加以下内容。
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 log4j.rootCategory=INFO, FILE log4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.target=System.err log4j.appender.console.layout=org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{1}: %m%n log4j.logger.org.apache.spark.sql.execution=DEBUG log4j.logger.org.apache.spark.repl.Main=INFO log4j.appender.FILE=org.apache.log4j.FileAppender log4j.appender.FILE.file=/usr/local/spark/logs/file.log log4j.appender.FILE.layout=org.apache.log4j.PatternLayout log4j.appender.FILE.layout.ConversionPattern=%m%n

  c. 按“Esc”键，输入:wq!，按“Enter”保存并退出编辑。
2. 修改log4j.properties中的log4j.appender.FILE.file为自定义的目录和文件名。
3. 可选：运行Spark-SQL命令，对tpch_flat_orc_1000.lineitem表进行ANALYZE操作。若更新数据表之后进行了ANALYZE操作则无需再次进行ANALYZE操作。

  a. 启动Spark交互式命令行界面。
    1 /usr/local/spark/bin/spark-sql --driver-memory 50G --executor-memory 32G --num-executors 30 --executor-cores 18

  b. 执行ANALYZE语句。
    1 analyze table tpch_flat_orc_1000.lineitem compute statistics for all columns

4. 运行spark-sql命令时加日志项--driver-java-options -Dlog4j.configuration=file:/usr/local/spark/conf/log4j.properties，例如。
  1 /usr/local/spark/bin/spark-sql --driver-class-path '/opt/boostkit/*' --jars '/opt/boostkit/*' --conf 'spark.executor.extraClassPath=./*' --name tpch_query6.sql --driver-memory 50G --driver-java-options -Dlog4j.configuration=file:/usr/local/spark/conf/log4j.properties --executor-memory 32G --num-executors 30 --executor-cores 18

5. 执行sql6语句。
  1 2 3 4 5 6 7 8 9 select sum(l_extendedprice * l_discount) as revenue from tpch_flat_orc_1000.lineitem where l_shipdate >= '1993-01-01' and l_shipdate < '1994-01-01' and l_discount between 0.06 - 0.01 and 0.06 + 0.01 and l_quantity < 25;

  执行任务时会打印下推的信息，如下所示。

  1 2 Selectivity: 0.014160436451808448 Push down with [PushDownInfo(ListBuffer(FilterExeInfo((((((((isnotnull(l_shipdate#18) AND isnotnull(l_discount#14)) AND isnotnull(l_quantity#12)) AND (l_shipdate#18 >= 8401)) AND (l_shipdate#18 < 8766)) AND (l_discount#14 >= 0.05)) AND (l_discount#14 <= 0.07)) AND (l_quantity#12 < 25.0)),List(l_quantity#12, l_extendedprice#13, l_discount#14, l_shipdate#18))),ListBuffer(AggExeInfo(List(sum((l_extendedprice#13 * l_discount#14))),List(),List(sum#43))),None,Map(server1 -> server1, agent2 -> agent2, agent1 -> agent1))]

  包含了下推的选择率以及算子信息。

  spark-sql命令参数信息如表2所示。

**表2 OmniData的参数含义**

| 参数 | 推荐值 | 含义 |
| --- | --- | --- |
| \-\-driver\-memory | 50G | Driver可用内存。 |
| \-\-executor\-memory | 32G | 执行器可用内存。 |
| \-\-num\-executors | 30 | 启动的执行器数量，缺省值为2。 |
| \-\-executor\-cores | 18 | 每个执行器使用的CPU核数，缺省值为1。 |
| \-\-driver\-class\-path | "/opt/boostkit/\*" | 传递给驱动程序的额外JAR包的路径。 |
| \-\-jars | "/opt/boostkit/\*" | 驱动程序和执行器类路径中要包含的JAR包。 |
| \-\-conf | "spark.executor.extraClassPath=./\*" | 配置Spark参数。 |
| \-\-driver\-java\-options \-Dlog4j.configuration | file:/usr/local/spark/conf/log4j.properties | log4j日志配置路径。 |
