---
title: Spark-SQL行式数据源
description: "使用OmniShield机密大数据特性执行Spark引擎业务，需通过启动Spark提交任务执行。"
url: https://www.hikunpeng.com/document/detail/zh/kunpengboostkithistory/240RC5/bds/kunpengbds_omniruntime_20_0298.html
sourcePath: /source/zh/kunpengboostkithistory/240RC5/bds/kunpengbds_omniruntime_20_0298.html
indexId: ac7b690496caa3ab9349ab0ea88ea138e6942a3043fff866980ac6c2e970a5a980
---
# Spark-SQL行式数据源

使用OmniShield机密大数据特性执行Spark引擎业务，需通过启动Spark提交任务执行。

1. 部署相应KMS服务，创建好所需的primary key。例如可以通过以下指令在Hadoop KMS中创建primary key。
  1 hadoop key create key1

2. 在“/opt/omnishield”目录下执行如下命令启动Spark-SQL。
  1 spark-sql --master local --conf spark.sql.extensions=com.huawei.analytics.shield.sql.DataSourceEncryptPlugin --conf spark.hadoop.io.compression.codecs=com.huawei.analytics.shield.crypto.CryptoCodec --jars omnishield-1.0-SNAPSHOT.jar,kms.jar --conf spark.executor.extraClassPath=omnishield-1.0-SNAPSHOT.jar:kms.jar --driver-class-path omnishield-1.0-SNAPSHOT.jar:kms.jar

3. 在Spark-SQL命令行执行如下SQL创建加密数据表。
  1 2 drop table if exists test1; create table test1 (id int) using csv options (`encrypt` 'true',`keyname` 'key1',`kmstype` 'test.HadoopKeyManagementService',`cryptomode` 'aes/gcm/nopadding',`keylength` '128');

  encrypt是指定是否使用加密表，该项的值为true时表示使用加密表。 keyname是指定当前使用的primary key, 该key需要提前在KMS中创建生成。 kmstype是指定primary key的KMS类型，KMS需要使用者自行按照OmniShield提供的接口实现，该项的值需要指定KMS在kms.jar中的具体类路径。 cryptomode是指定加密数据时使用的加密算法，支持AES/GCM/NOPadding和SM4/GCM/NoPadding。 keylength是指定加密的密钥长度，当使用AES/GCM/NOPadding加密算法时该项的值可以是128或256，当使用SM4/GCM/NoPadding加密算法时该项的值是128。 可以通过指定数据源格式创建不同类型的数据表，如创建JSON格式数据表需将3中的using csv替换using json。目前支持CSV/JSON/TXT格式。

4. 在Spark-SQL命令行执行如下SQL检测数据表是否为加密数据表。
  1 show create table test1;

  在SQL运行结果的OPTIONS中如果包含encryptdatakey项则为加密表。
