![]() |
VOOZH | about |
AWS Glue is an ETL service from Amazon that allows you to easily prepare and load your data for storage and analytics. Using the PySpark module along with AWS Glue, you can create jobs that work with data over JDBC connectivity, loading the data directly into AWS data stores. In this article, we walk through uploading the CData JDBC Driver for Kafka into an Amazon S3 bucket and creating and running an AWS Glue job to extract Kafka data and store it in S3 as a CSV file.
In order to work with the CData JDBC Driver for Kafka in AWS Glue, you will need to store it (and any relevant license files) in an Amazon S3 bucket.
To connect to Kafka using the CData JDBC driver, you will need to create a JDBC URL, populating the necessary connection properties. Additionally, you will need to set the property in the JDBC URL (unless you are using a Beta driver). You can view the licensing file included in the installation for information on how to set this property.
Set BootstrapServers and the Topic properties to specify the address of your Apache Kafka server, as well as the topic you would like to interact with.
You may be required to trust the server certificate. In such cases, specify the TrustStorePath and the TrustStorePassword if necessary.
For assistance in constructing the JDBC URL, use the connection string designer built into the Kafka JDBC Driver. Either double-click the JAR file or execute the JAR file from the command-line.
java -jar cdata.jdbc.apachekafka.jar
Fill in the connection properties and copy the connection string to the clipboard.
👁 Using the built-in connection string designer to generate a JDBC URL (Salesforce is shown.)To host the JDBC driver in Amazon S3, you will need a license (full or trial) and a Runtime Key (RTK). For more information on obtaining this license (or a trial), contact our sales team.
Below is a sample script that uses the CData JDBC driver with the PySpark and AWSGlue modules to extract Kafka data and write it to an S3 bucket in CSV format. Make any necessary changes to the script to suit your needs and save the job.
import sys
from awsglue.transforms import *
from awsglue.utils import getResolvedOptions
from pyspark.context import SparkContext
from awsglue.context import GlueContext
from awsglue.dynamicframe import DynamicFrame
from awsglue.job import Job
args = getResolvedOptions(sys.argv, ['JOB_NAME'])
sparkContext = SparkContext()
glueContext = GlueContext(sparkContext)
sparkSession = glueContext.spark_session
##Use the CData JDBC driver to read Kafka data from the SampleTable_1 table into a DataFrame
##Note the populated JDBC URL and driver class name
source_df = sparkSession.read.format("jdbc").option("url","jdbc:apachekafka:RTK=5246...;User=admin;Password=pass;BootStrapServers=https://localhost:9091;Topic=MyTopic;").option("dbtable","SampleTable_1").option("driver","cdata.jdbc.apachekafka.ApacheKafkaDriver").load()
glueJob = Job(glueContext)
glueJob.init(args['JOB_NAME'], args)
##Convert DataFrames to AWS Glue's DynamicFrames Object
dynamic_dframe = DynamicFrame.fromDF(source_df, glueContext, "dynamic_df")
##Write the DynamicFrame as a file in CSV format to a folder in an S3 bucket.
##It is possible to write to any Amazon data store (SQL Server, Redshift, etc) by using any previously defined connections.
retDatasink4 = glueContext.write_dynamic_frame.from_options(frame = dynamic_dframe, connection_type = "s3", connection_options = {"path": "s3://mybucket/outfiles"}, format = "csv", transformation_ctx = "datasink4")
glueJob.commit()
With the script written, we are ready to run the Glue job. Click Run Job and wait for the extract/load to complete. You can view the status of the job from the Jobs page in the AWS Glue Console. Once the Job has succeeded, you will have a CSV file in your S3 bucket with data from the Kafka SampleTable_1 table.
Using the CData JDBC Driver for Kafka in AWS Glue, you can easily create ETL jobs for Kafka data, whether writing the data to an S3 bucket or loading it into any other AWS data store.
Download a free trial of the Apache Kafka Driver to get started:
Download NowLearn more:
👁 Apache Kafka IconRapidly create and deploy powerful Java applications that integrate with Apache Kafka.