Create a PySpark script and upload it to your object storage bucket. Choose the script that
matches your table type.
For Hive tables:
from pyspark.sql import SparkSession
from pyspark import SparkConf
import subprocess
# Kerberos and HDFS configuration
KEYTAB_PATH = "<mount_path>/<keytab_file>"
PRINCIPAL = "<kerberos_principal>"
HIVE_METASTORE = "thrift://<metastore_host>:<metastore_port>"
NAMENODE_URI = "hdfs://<namenode_host>:<namenode_port>"
KRB5_CONF = "<mount_path>/krb5.conf"
CORE_SITE = "<mount_path>/core-site.xml"
HDFS_SITE = "<mount_path>/hdfs-site.xml"
HIVE_SITE = "<mount_path>/hive-site.xml"
DATABASE_NAME = "<database_name>"
TABLE_NAME = "<table_name>"
# Obtain a Kerberos ticket
subprocess.run(
["kinit", "-kt", KEYTAB_PATH, PRINCIPAL],
check=True
)
print(f"Kerberos ticket obtained for: {PRINCIPAL}")
conf = SparkConf()
conf.set("spark.hadoop.fs.defaultFS", NAMENODE_URI)
conf.set("spark.hadoop.hadoop.security.authentication", "kerberos")
conf.set("spark.hadoop.hadoop.security.authorization", "true")
conf.set("spark.hadoop.dfs.namenode.kerberos.principal", PRINCIPAL)
conf.set("spark.kerberos.keytab", KEYTAB_PATH)
conf.set("spark.kerberos.principal", PRINCIPAL)
conf.set("spark.hadoop.hadoop.kerberos.kinit.command", "kinit")
conf.set("spark.hadoop.java.security.krb5.conf", KRB5_CONF)
spark = SparkSession.builder \
.appName("ClouDeraHiveQuery") \
.config(conf=conf) \
.enableHiveSupport() \
.getOrCreate()
spark.sparkContext.addFile(CORE_SITE)
spark.sparkContext.addFile(HDFS_SITE)
spark.sparkContext.addFile(HIVE_SITE)
spark.sql(f"USE {DATABASE_NAME}")
df = spark.sql(f"SELECT * FROM {TABLE_NAME} LIMIT 20")
df.printSchema()
df.show(20, truncate=False)
print(f"Total rows: {df.count()}")
spark.stop()
For Iceberg tables:
from pyspark.sql import SparkSession
from pyspark import SparkConf
import subprocess
# Kerberos and HDFS configuration
KEYTAB_PATH = "<mount_path>/<keytab_file>"
PRINCIPAL = "<kerberos_principal>"
HIVE_METASTORE = "thrift://<metastore_host>:<metastore_port>"
NAMENODE_URI = "hdfs://<namenode_host>:<namenode_port>"
KRB5_CONF = "<mount_path>/krb5.conf"
CORE_SITE = "<mount_path>/core-site.xml"
HDFS_SITE = "<mount_path>/hdfs-site.xml"
HIVE_SITE = "<mount_path>/hive-site.xml"
CATALOG_NAME = "<iceberg_catalog_name>"
DATABASE_NAME = "<database_name>"
TABLE_NAME = "<table_name>"
# Obtain a Kerberos ticket
subprocess.run(
["kinit", "-kt", KEYTAB_PATH, PRINCIPAL],
check=True
)
print(f"Kerberos ticket obtained for: {PRINCIPAL}")
conf = SparkConf()
conf.set("spark.hadoop.fs.defaultFS", NAMENODE_URI)
conf.set("spark.hadoop.hadoop.security.authentication", "kerberos")
conf.set("spark.hadoop.hadoop.security.authorization", "true")
conf.set("spark.hadoop.dfs.namenode.kerberos.principal", PRINCIPAL)
conf.set("spark.kerberos.keytab", KEYTAB_PATH)
conf.set("spark.kerberos.principal", PRINCIPAL)
conf.set("spark.hadoop.hadoop.kerberos.kinit.command", "kinit")
conf.set("spark.hadoop.java.security.krb5.conf", KRB5_CONF)
conf.set(f"spark.sql.catalog.{CATALOG_NAME}",
"org.apache.iceberg.spark.SparkCatalog")
conf.set(f"spark.sql.catalog.{CATALOG_NAME}.type", "hive")
conf.set(f"spark.sql.catalog.{CATALOG_NAME}.uri", HIVE_METASTORE)
conf.set("spark.sql.extensions",
"org.apache.iceberg.spark.extensions.IcebergSparkSessionExtensions")
spark = SparkSession.builder \
.appName("ClouderaIcebergQuery") \
.config(conf=conf) \
.getOrCreate()
spark.sparkContext.addFile(CORE_SITE)
spark.sparkContext.addFile(HDFS_SITE)
spark.sparkContext.addFile(HIVE_SITE)
df = spark.sql(
f"SELECT * FROM {CATALOG_NAME}.{DATABASE_NAME}.{TABLE_NAME} LIMIT 20"
)
df.printSchema()
df.show(20, truncate=False)
print(f"Total rows: {df.count()}")
# Example: Iceberg time travel
df_snapshot = spark.sql(
f"SELECT * FROM {CATALOG_NAME}.{DATABASE_NAME}.{TABLE_NAME} "
f"VERSION AS OF 1"
)
df_snapshot.show(truncate=False)
spark.stop()
Replace the following placeholders in the script:
| Placeholder |
Description |
<mount_path> |
Mount path of the storage volume configured in step 1 (for example,
/mnts/cloudera). |
<keytab_file> |
File name of the Kerberos keytab (for example, hive.keytab). |
<kerberos_principal> |
Kerberos principal in the format <service>/<hostname>@<REALM>
(for example, hive/cloudera-host.example.com@EXAMPLE.COM). |
<metastore_host> |
Hostname or IP address of the Hive Metastore server. |
<metastore_port> |
Port of the Hive Metastore (default: 9083). |
<namenode_host> |
Hostname or IP address of the HDFS NameNode. |
<namenode_port> |
Port of the HDFS NameNode (default: 8020). |
<database_name> |
Name of the Hive database containing the target table. |
<table_name> |
Name of the Hive or Iceberg table to query. |
<iceberg_catalog_name> |
Name for the Iceberg catalog configured in Spark (Iceberg script only). This is a
logical name used in Spark SQL; for example, cloudera_iceberg. |
After completing the script, upload it to your object storage bucket (for example,
s3a://<bucket_name>/cloudera_query.py).