Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
189 changes: 189 additions & 0 deletions scala/datastax-v4/aws-glue/count-table-rows/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
## Using Glue Count Example
This example provides scala script for counting the number of rows in a Amazon Keyspaces table using AWS Glue. This is common utility in verifying data in tables during migration or after bulk import.

## Prerequisites
* Amazon Keyspaces table to count
* Amazon S3 bucket to store required jars
* Amazon S3 bucket to store job configuration and script
* Amazon S3 bucket to store glue shuffle data

### Counting the number of rows in Amazon Keyspaces
The following example uses the spark-cassandra-connector. The script takes three parameters KEYSPACE_NAME, KEYSPACE_TABLE, and DRIVER_CONF. DRIVER_CONF is the java driver external application.conf where all connection information is maintained.


```
import com.amazonaws.services.glue.GlueContext
import com.amazonaws.services.glue.log.GlueLogger
import com.amazonaws.services.glue.util.GlueArgParser
import com.amazonaws.services.glue.util.Job
import org.apache.spark.SparkContext
import org.apache.spark.SparkConf
import org.apache.spark.sql.Dataset
import org.apache.spark.sql.Row
import org.apache.spark.sql.SaveMode
import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.functions.from_json
import org.apache.spark.sql.streaming.Trigger
import scala.collection.JavaConverters._
import com.datastax.spark.connector._
import org.apache.spark.sql.cassandra._
import org.apache.spark.sql.SaveMode._

object GlueApp {

def main(sysArgs: Array[String]) {

val args = GlueArgParser.getResolvedOptions(sysArgs, Seq("JOB_NAME", "KEYSPACE_NAME", "TABLE_NAME", "DRIVER_CONF").toArray)

val driverConfFileName = args("DRIVER_CONF")

val conf = new SparkConf()
.setAll(
Seq(
("spark.cassandra.connection.config.profile.path", driverConfFileName),
("spark.cassandra.query.retry.count", "100"),

("spark.cassandra.sql.inClauseToJoinConversionThreshold", "0"),
("spark.cassandra.sql.inClauseToFullScanConversionThreshold", "0"),
("spark.cassandra.concurrent.reads", "512"),

("spark.cassandra.output.concurrent.writes", "5"),
("spark.cassandra.output.batch.grouping.key", "none"),
("spark.cassandra.output.batch.size.rows", "1")
))


val spark: SparkContext = new SparkContext(conf)
val glueContext: GlueContext = new GlueContext(spark)
val sparkSession: SparkSession = glueContext.getSparkSession

import com.datastax.spark.connector._
import org.apache.spark.sql.cassandra._
import sparkSession.implicits._

Job.init(args("JOB_NAME"), glueContext, args.asJava)

val tableName = args("TABLE_NAME")
val keyspaceName = args("KEYSPACE_NAME")

val tableDf = sparkSession.read
.format("org.apache.spark.sql.cassandra")
.options(Map( "table" -> tableName, "keyspace" -> keyspaceName))
.load()

val total = tableDf.toJavaRDD.count()

val logger = new GlueLogger

logger.info("Total number of rows in table:" + total)

Job.commit()
}
}

```


## Create IAM ROLE for AWS Glue
Create a new AWS service role named 'GlueKeyspacesRole' with AWS Glue as a trusted entity.

Included is a sample permissions-policy for executing Glue job. You can use managed policies AWSGlueServiceRole, AmazonKeyspacesReadOnlyAccess, read access to S3 bucket containing spack-cassandra-connector jar and configuration.


## Cassandra driver configuration to connect to Amazon Keyspaces
The following configuration for connecting to Amazon Keyspaces with the spark-cassandra connector.

Using the RateLimitingRequestThrottler we can ensure that request do not exceed configured Keyspaces capacity. The G1.X DPU creates one executor per worker. The RateLimitingRequestThrottler in this example is set for 1000 request per second. With this configuration and G.1X DPU you will achieve 1000 request per Glue worker. Adjust the max-requests-per-second accordingly to fit your workload. Increase the number of workers to scale throughput to a table.

```

datastax-java-driver {
basic.request.consistency = "LOCAL_ONE"
basic.request.default-idempotence = true
basic.contact-points = [ "cassandra.us-east-1.amazonaws.com:9142"]
advanced.reconnect-on-init = true

basic.load-balancing-policy {
local-datacenter = "us-east-1"
}

advanced.auth-provider = {
class = PlainTextAuthProvider
username = "user-at-sample"
password = "SAMPLE#PASSWORD"
}

advanced.throttler = {
class = RateLimitingRequestThrottler
max-requests-per-second = 1000
max-queue-size = 50000
drain-interval = 1 millisecond
}

advanced.ssl-engine-factory {
class = DefaultSslEngineFactory
hostname-validation = false
}

advanced.connection.pool.local.size = 2
advanced.resolve-contact-points = false

}

```

## Create S3 bucket to store job artifacts
The AWS Glue ETL job will need to access jar dependencies, driver configuration, and scala script. You can use the same bucket to store backups.
```
aws s3 mb s3://amazon-keyspaces-artifacts
```

## Create S3 bucket for shuffle space
With NoSQL its common to shuffle large sets of data. This can overflow local disk. With AWS Glue, you can use Amazon S3 to store Spark shuffle and spill data. This solution disaggregates compute and storage for your Spark jobs, and gives complete elasticity and low-cost shuffle storage, allowing you to run your most shuffle-intensive workloads reliably.

```
aws s3 mb s3://amazon-keyspaces-glue-shuffle
```

## Upload job artifacts to S3
The job will require
* The spark-cassandra-connector to allow reads from Amazon Keyspaces. Amazon Keyspaces recommends version 2.5.2 or above of the spark-cassandra-connector.
* creation of S3 buckets
* cassandra-application.conf containing the cassandra driver configuration for Keyspaces access
* count-example.scala script containing the count code.

```
curl -L -O https://repo1.maven.org/maven2/com/datastax/spark/spark-cassandra-connector-assembly_2.11/2.5.2/spark-cassandra-connector-assembly_2.11-2.5.2.jar

aws s3api put-object --bucket amazon-keyspaces-artifacts --key jars/spark-cassandra-connector-assembly_2.11-2.5.2.jar --body spark-cassandra-connector-assembly_2.11-2.5.2.jar

aws s3api put-object --bucket amazon-keyspaces-artifacts --key conf/cassandra-application.conf --body cassandra-application.conf

aws s3api put-object --bucket amazon-keyspaces-artifacts --key scripts/count-example.scala --body count-example.scala

```
### Create AWS Glue ETL Job
You can use the following command to create a glue job using the script provided in this example. You can also take the parameters and enter them into the AWS Console.
```
aws glue create-job \
--name "AmazonKeyspacesCount" \
--role "GlueKeyspacesRestore" \
--description "Count Rows in Amazon Keyspaces table" \
--glue-version "2.0" \
--number-of-workers 5 \
--worker-type "G.1X" \
--command "Name=glueetl,ScriptLocation=s3://amazon-keyspaces-artifacts/scripts/count-example.scala" \
--default-arguments '{
"--job-language":"scala",
"--KEYSPACE_NAME":"my_keyspace",
"--TABLE_NAME":"my_table",
"--DRIVER_CONF":"cassandra-application.conf",
"--extra-jars":"s3://amazon-keyspaces-artifacts/jars/spark-cassandra-connector-assembly_2.11-2.5.2.jar",
"--extra-files":"s3://amazon-keyspaces-artifacts/conf/cassandra-application.conf",
"--enable-continuous-cloudwatch-log":"true",
"--write-shuffle-files-to-s3":"true",
"--write-shuffle-spills-to-s3":"true",
"--TempDir":"s3://amazon-keyspaces-glue-shuffle",
"--class":"GlueApp"
}'
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

datastax-java-driver {
basic.request.consistency = "LOCAL_ONE"
basic.request.default-idempotence = true
basic.contact-points = [ "cassandra.us-east-1.amazonaws.com:9142"]
advanced.reconnect-on-init = true

basic.load-balancing-policy {
local-datacenter = "us-east-1"
}

advanced.auth-provider = {
class = PlainTextAuthProvider
username = "user-at-sample"
password = "SAMPLE#PASSWORD"
}

advanced.throttler = {
class = RateLimitingRequestThrottler
max-requests-per-second = 1000
max-queue-size = 50000
drain-interval = 1 millisecond
}

advanced.ssl-engine-factory {
class = DefaultSslEngineFactory
hostname-validation = false
}

advanced.connection.pool.local.size = 2
advanced.resolve-contact-points = false

}
69 changes: 69 additions & 0 deletions scala/datastax-v4/aws-glue/count-table-rows/count-example.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import com.amazonaws.services.glue.GlueContext
import com.amazonaws.services.glue.log.GlueLogger
import com.amazonaws.services.glue.util.GlueArgParser
import com.amazonaws.services.glue.util.Job
import org.apache.spark.SparkContext
import org.apache.spark.SparkConf
import org.apache.spark.sql.Dataset
import org.apache.spark.sql.Row
import org.apache.spark.sql.SaveMode
import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.functions.from_json
import org.apache.spark.sql.streaming.Trigger
import scala.collection.JavaConverters._
import com.datastax.spark.connector._
import org.apache.spark.sql.cassandra._
import org.apache.spark.sql.SaveMode._

object GlueApp {

def main(sysArgs: Array[String]) {

val args = GlueArgParser.getResolvedOptions(sysArgs, Seq("JOB_NAME", "KEYSPACE_NAME", "TABLE_NAME", "DRIVER_CONF").toArray)

val driverConfFileName = args("DRIVER_CONF")

val conf = new SparkConf()
.setAll(
Seq(
("spark.cassandra.connection.config.profile.path", driverConfFileName),
("spark.cassandra.query.retry.count", "100"),

("spark.cassandra.sql.inClauseToJoinConversionThreshold", "0"),
("spark.cassandra.sql.inClauseToFullScanConversionThreshold", "0"),
("spark.cassandra.concurrent.reads", "512"),

("spark.cassandra.output.concurrent.writes", "5"),
("spark.cassandra.output.batch.grouping.key", "none"),
("spark.cassandra.output.batch.size.rows", "1")
))


val spark: SparkContext = new SparkContext(conf)
val glueContext: GlueContext = new GlueContext(spark)
val sparkSession: SparkSession = glueContext.getSparkSession

import com.datastax.spark.connector._
import org.apache.spark.sql.cassandra._
import sparkSession.implicits._

Job.init(args("JOB_NAME"), glueContext, args.asJava)

val tableName = args("TABLE_NAME")
val keyspaceName = args("KEYSPACE_NAME")


val tableDf = sparkSession.read
.format("org.apache.spark.sql.cassandra")
.options(Map( "table" -> tableName, "keyspace" -> keyspaceName))
.load()

val total = tableDf.toJavaRDD.count()

val logger = new GlueLogger

logger.info("Total number of rows in table:" + total)

Job.commit()
}
}
Loading