> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-fix-nav-issues.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> Integrate ClickHouse and Amazon Glue

# Integrating Amazon Glue with ClickHouse and Spark

export const ClickHouseSupportedBadge = () => {
  return <div className="ClickHouseSupportedBadge">
            <div className="ClickHouseSupportedIcon">
                <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
                    <path d="M1.30762 1.39073C1.30762 1.3103 1.37465 1.22986 1.46849 1.22986H2.64824C2.72868 1.22986 2.80912 1.29689 2.80912 1.39073V14.4886C2.80912 14.5691 2.74209 14.6495 2.64824 14.6495H1.46849C1.38805 14.6495 1.30762 14.5825 1.30762 14.4886V1.39073Z" fill="currentColor" />
                    <path d="M4.2832 1.39073C4.2832 1.3103 4.35023 1.22986 4.44408 1.22986H5.62383C5.70427 1.22986 5.7847 1.29689 5.7847 1.39073V14.4886C5.7847 14.5691 5.71767 14.6495 5.62383 14.6495H4.44408C4.36364 14.6495 4.2832 14.5825 4.2832 14.4886V1.39073Z" fill="currentColor" />
                    <path d="M7.25977 1.39073C7.25977 1.3103 7.3268 1.22986 7.42064 1.22986H8.60039C8.68083 1.22986 8.76127 1.29689 8.76127 1.39073V14.4886C8.76127 14.5691 8.69423 14.6495 8.60039 14.6495H7.42064C7.3402 14.6495 7.25977 14.5825 7.25977 14.4886V1.39073Z" fill="currentColor" />
                    <path d="M10.2354 1.39073C10.2354 1.3103 10.3024 1.22986 10.3962 1.22986H11.576C11.6564 1.22986 11.7369 1.29689 11.7369 1.39073V14.4886C11.7369 14.5691 11.6698 14.6495 11.576 14.6495H10.3962C10.3158 14.6495 10.2354 14.5825 10.2354 14.4886V1.39073Z" fill="currentColor" />
                    <path d="M13.2256 6.6057C13.2256 6.52526 13.2926 6.44482 13.3865 6.44482H14.5662C14.6466 6.44482 14.7271 6.51186 14.7271 6.6057V9.27354C14.7271 9.35398 14.6601 9.43442 14.5662 9.43442H13.3865C13.306 9.43442 13.2256 9.36739 13.2256 9.27354V6.6057Z" fill="currentColor" />
                </svg>
            </div>
            ClickHouse Supported
        </div>;
};

export const Image = ({img, alt, size}) => {
  return <Frame>
      <img src={img} alt={alt} />
    </Frame>;
};

[Amazon Glue](https://aws.amazon.com/glue/) is a fully managed, serverless data integration service provided by Amazon Web Services (AWS). It simplifies the process of discovering, preparing, and transforming data for analytics, machine learning, and application development.

<h2 id="installation">
  Installation
</h2>

To integrate your Glue code with ClickHouse, you can use our official Spark connector in Glue via one of the following:

* Installing the ClickHouse Glue connector from the AWS Marketplace (recommended).
* Manually adding the Spark Connector's jars to your Glue job.

<Tabs>
  <Tab title="AWS Marketplace">
    1. <h3 id="subscribe-to-the-connector">Subscribe to the Connector</h3>
       To access the connector in your account, subscribe to the ClickHouse AWS Glue Connector from AWS Marketplace.

    2. <h3 id="grant-required-permissions">Grant Required Permissions</h3>
       Ensure your Glue job’s IAM role has the necessary permissions, as described in the minimum privileges [guide](https://docs.aws.amazon.com/glue/latest/dg/getting-started-min-privs-job.html#getting-started-min-privs-connectors).

    3. <h3 id="activate-the-connector">Activate the Connector & Create a Connection</h3>
       You can activate the connector and create a connection directly by clicking [this link](https://console.aws.amazon.com/gluestudio/home#/connector/add-connection?connectorName="ClickHouse%20AWS%20Glue%20Connector"\&connectorType="Spark"\&connectorUrl=https://709825985650.dkr.ecr.us-east-1.amazonaws.com/clickhouse/clickhouse-glue:1.0.0\&connectorClassName="com.clickhouse.spark.ClickHouseCatalog"), which opens the Glue connection creation page with key fields pre-filled. Give the connection a name, and press create (no need to provide the ClickHouse connection details at this stage).

    4. <h3 id="use-in-glue-job">Use in Glue Job</h3>
       In your Glue job, select the `Job details` tab, and expend the `Advanced properties` window. Under the `Connections` section, select the connection you just created. The connector automatically injects the required JARs into the job runtime.

    <Image img="https://mintcdn.com/private-7c7dfe99-fix-nav-issues/lGskH5qUgz9Vtlav/images/integrations/data-ingestion/aws-glue/notebook-connections-config.png?fit=max&auto=format&n=lGskH5qUgz9Vtlav&q=85&s=7012e5d4c7a7ff392ba916abe9c12da5" size="md" alt="Glue Notebook connections config" force="true" width="877" height="742" data-path="images/integrations/data-ingestion/aws-glue/notebook-connections-config.png" />

    <Note>
      The JARs used in the Glue connector are built for `Spark 3.3`, `Scala 2`, and `Python 3`. Make sure to select these versions when configuring your Glue job.
    </Note>
  </Tab>

  <Tab title="Manual Installation">
    To add the required jars manually, please follow the following:

    1. Upload the following jars to an S3 bucket - `clickhouse-jdbc-0.6.X-all.jar` and `clickhouse-spark-runtime-3.X_2.X-0.8.X.jar`.
    2. Make sure the Glue job has access to this bucket.
    3. Under the `Job details` tab, scroll down and expend the `Advanced properties` drop down, and fill the jars path in `Dependent JARs path`:

    <Image img="https://mintcdn.com/private-7c7dfe99-fix-nav-issues/lGskH5qUgz9Vtlav/images/integrations/data-ingestion/aws-glue/dependent_jars_path_option.png?fit=max&auto=format&n=lGskH5qUgz9Vtlav&q=85&s=1adfb38ede30692eb3659ba0aeac52db" size="md" alt="Glue Notebook JAR path options" force="true" width="954" height="753" data-path="images/integrations/data-ingestion/aws-glue/dependent_jars_path_option.png" />
  </Tab>
</Tabs>

<h2 id="example">
  Examples
</h2>

<Tabs>
  <Tab title="Scala">
    ```java theme={null}
    import com.amazonaws.services.glue.GlueContext
    import com.amazonaws.services.glue.util.GlueArgParser
    import com.amazonaws.services.glue.util.Job
    import com.clickhouseScala.Native.NativeSparkRead.spark
    import org.apache.spark.sql.SparkSession

    import scala.collection.JavaConverters._
    import org.apache.spark.sql.types._
    import org.apache.spark.sql.functions._

    object ClickHouseGlueExample {
      def main(sysArgs: Array[String]) {
        val args = GlueArgParser.getResolvedOptions(sysArgs, Seq("JOB_NAME").toArray)

        val sparkSession: SparkSession = SparkSession.builder
          .config("spark.sql.catalog.clickhouse", "com.clickhouse.spark.ClickHouseCatalog")
          .config("spark.sql.catalog.clickhouse.host", "<your-clickhouse-host>")
          .config("spark.sql.catalog.clickhouse.protocol", "https")
          .config("spark.sql.catalog.clickhouse.http_port", "<your-clickhouse-port>")
          .config("spark.sql.catalog.clickhouse.user", "default")
          .config("spark.sql.catalog.clickhouse.password", "<your-password>")
          .config("spark.sql.catalog.clickhouse.database", "default")
          // for ClickHouse cloud
          .config("spark.sql.catalog.clickhouse.option.ssl", "true")
          .config("spark.sql.catalog.clickhouse.option.ssl_mode", "NONE")
          .getOrCreate

        val glueContext = new GlueContext(sparkSession.sparkContext)
        Job.init(args("JOB_NAME"), glueContext, args.asJava)
        import sparkSession.implicits._

        val url = "s3://{path_to_cell_tower_data}/cell_towers.csv.gz"

        val schema = StructType(Seq(
          StructField("radio", StringType, nullable = false),
          StructField("mcc", IntegerType, nullable = false),
          StructField("net", IntegerType, nullable = false),
          StructField("area", IntegerType, nullable = false),
          StructField("cell", LongType, nullable = false),
          StructField("unit", IntegerType, nullable = false),
          StructField("lon", DoubleType, nullable = false),
          StructField("lat", DoubleType, nullable = false),
          StructField("range", IntegerType, nullable = false),
          StructField("samples", IntegerType, nullable = false),
          StructField("changeable", IntegerType, nullable = false),
          StructField("created", TimestampType, nullable = false),
          StructField("updated", TimestampType, nullable = false),
          StructField("averageSignal", IntegerType, nullable = false)
        ))

        val df = sparkSession.read
          .option("header", "true")
          .schema(schema)
          .csv(url)

        // Write to ClickHouse
        df.writeTo("clickhouse.default.cell_towers").append()

        // Read from ClickHouse
        val dfRead = spark.sql("select * from clickhouse.default.cell_towers")
        Job.commit()
      }
    }
    ```
  </Tab>

  <Tab title="Python">
    ```python theme={null}
    import sys
    from awsglue.transforms import *
    from awsglue.utils import getResolvedOptions
    from pyspark.context import SparkContext
    from awsglue.context import GlueContext
    from awsglue.job import Job
    from pyspark.sql import Row

    ## @params: [JOB_NAME]
    args = getResolvedOptions(sys.argv, ['JOB_NAME'])

    sc = SparkContext()
    glueContext = GlueContext(sc)
    logger = glueContext.get_logger()
    spark = glueContext.spark_session
    job = Job(glueContext)
    job.init(args['JOB_NAME'], args)

    spark.conf.set("spark.sql.catalog.clickhouse", "com.clickhouse.spark.ClickHouseCatalog")
    spark.conf.set("spark.sql.catalog.clickhouse.host", "<your-clickhouse-host>")
    spark.conf.set("spark.sql.catalog.clickhouse.protocol", "https")
    spark.conf.set("spark.sql.catalog.clickhouse.http_port", "<your-clickhouse-port>")
    spark.conf.set("spark.sql.catalog.clickhouse.user", "default")
    spark.conf.set("spark.sql.catalog.clickhouse.password", "<your-password>")
    spark.conf.set("spark.sql.catalog.clickhouse.database", "default")
    spark.conf.set("spark.clickhouse.write.format", "json")
    spark.conf.set("spark.clickhouse.read.format", "arrow")
    # for ClickHouse cloud
    spark.conf.set("spark.sql.catalog.clickhouse.option.ssl", "true")
    spark.conf.set("spark.sql.catalog.clickhouse.option.ssl_mode", "NONE")

    # Create DataFrame
    data = [Row(id=11, name="John"), Row(id=12, name="Doe")]
    df = spark.createDataFrame(data)

    # Write DataFrame to ClickHouse
    df.writeTo("clickhouse.default.example_table").append()

    # Read DataFrame from ClickHouse
    df_read = spark.sql("select * from clickhouse.default.example_table")
    logger.info(str(df.take(10)))

    job.commit()
    ```
  </Tab>
</Tabs>

For more details, please visit our [Spark documentation](/integrations/connectors/data-ingestion/apache-spark/index).
