Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Situation Awareness
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive
On this page

Connecting to a GeminiDB Cassandra Instance Using Spark

Updated on 2025-01-26 GMT+08:00

This section describes how to use the Scala to connect to a GeminiDB Cassandra instance.

Prerequisites

  • A GeminiDB Cassandra instance has been created and is running properly. For details about how to create a GeminiDB Cassandra instance, see Buying a GeminiDB Cassandra Instance.
  • For details about how to create an ECS, see Purchasing an ECS in Getting Started with Elastic Cloud Server.
  • The Spark environment has been installed on the ECS.

Procedure

  1. Obtain the private IP address and port number of the GeminiDB Cassandra instance.

    For details about how to obtain the private IP address and port number, see Viewing the IP Address and Port Number of a GeminiDB Cassandra Instance.

  2. Log in to the ECS. For details, see Logging In to an ECS in Getting Started with Elastic Cloud Server.
  3. Edit the code for connecting to the GeminiDB Cassandra instance.

    • If Spark 2.x is used to connect to the GeminiDB Cassandra instance, the recommended versions are as follows:

      Spark: 2.5.1

      Scala: 2.12

      spark-cassandra-connector: 2.5.1

      The following is sample code:
          /**
           * There will be security risks if the username and password used for authentication are directly written into code. Store the username and password in ciphertext in the configuration file or environment variables.
           * In this example, the username and password are stored in the environment variables. Before running this example, set environment variables USERNAME_ENV and PASSWORD_ENV as needed.
           */
          val username: String = System.getenv().asScala.mkString("USERNAME_ENV")
          val password: String = System.getenv().asScala.mkString("PASSWORD_ENV")
          val sparkSession = SparkSession
            .builder()
            .appName("Spark Cassandra basic example")
            .master("local")
            .config("spark.cassandra.connection.host", "26.84.42.111")
            .config("spark.cassandra.connection.port", "9042")
            .config("spark.cassandra.auth.username", username)
            .config("spark.cassandra.auth.password", password)
            .getOrCreate()

      If an error is reported during the connection, fix it by following What Can I Do If Spark Failed to Connect to Cassandra?.

    • If Spark 3.x is used to connect to the GeminiDB Cassandra instance, the recommended versions include:

      Spark: 3.2.4

      Scala: 2.12.15

      Java: 1.8

      spark-cassandra-connector: 3.1.0

      1. You are advised to rewrite a CassandraConnectionFactory (change loadBalancingPolicy to DefaultLoadBalancingPolicy). The following is sample code:
        package sample
        import java.io.IOException
        import java.net.{MalformedURLException, URL}
        import java.nio.file.{Files, Paths}
        import java.time.Duration
        
        import com.datastax.bdp.spark.ContinuousPagingScanner
        import com.datastax.dse.driver.api.core.DseProtocolVersion
        import com.datastax.dse.driver.api.core.config.DseDriverOption
        import com.datastax.oss.driver.api.core.CqlSession
        import com.datastax.oss.driver.api.core.config.DefaultDriverOption._
        import com.datastax.oss.driver.api.core.config.{DriverConfigLoader, ProgrammaticDriverConfigLoaderBuilder => PDCLB}
        import com.datastax.oss.driver.internal.core.connection.ExponentialReconnectionPolicy
        import com.datastax.oss.driver.internal.core.loadbalancing.DefaultLoadBalancingPolicy
        import com.datastax.oss.driver.internal.core.ssl.DefaultSslEngineFactory
        import com.datastax.spark.connector.rdd.ReadConf
        import com.datastax.spark.connector.util.{ConfigParameter, DeprecatedConfigParameter, ReflectionUtil}
        import org.apache.spark.{SparkConf, SparkEnv, SparkFiles}
        import org.slf4j.LoggerFactory
        
        import scala.jdk.CollectionConverters._
        import com.datastax.spark.connector.cql.{CassandraConnectionFactory, CassandraConnector, CassandraConnectorConf, CloudBasedContactInfo, DefaultScanner, IpBasedContactInfo, LocalNodeFirstLoadBalancingPolicy, MultipleRetryPolicy, MultiplexingSchemaListener, ProfileFileBasedContactInfo, Scanner}
        
        class ConnectionFactory extends CassandraConnectionFactory {
          @transient
          lazy private val logger = LoggerFactory.getLogger("com.datastax.spark.connector.cql.CassandraConnectionFactory")
        
          def connectorConfigBuilder(conf: CassandraConnectorConf, initBuilder: PDCLB) = {
        
            def basicProperties(builder: PDCLB): PDCLB = {
              val localCoreThreadCount = Math.max(1, Runtime.getRuntime.availableProcessors() - 1)
              builder
                .withInt(CONNECTION_POOL_LOCAL_SIZE, conf.localConnectionsPerExecutor.getOrElse(localCoreThreadCount)) // moved from CassandraConnector
                .withInt(CONNECTION_POOL_REMOTE_SIZE, conf.remoteConnectionsPerExecutor.getOrElse(1)) // moved from CassandraConnector
                .withInt(CONNECTION_INIT_QUERY_TIMEOUT, conf.connectTimeoutMillis)
                .withDuration(CONTROL_CONNECTION_TIMEOUT, Duration.ofMillis(conf.connectTimeoutMillis))
                .withDuration(METADATA_SCHEMA_REQUEST_TIMEOUT, Duration.ofMillis(conf.connectTimeoutMillis))
                .withInt(REQUEST_TIMEOUT, conf.readTimeoutMillis)
                .withClass(RETRY_POLICY_CLASS, classOf[MultipleRetryPolicy])
                .withClass(RECONNECTION_POLICY_CLASS, classOf[ExponentialReconnectionPolicy])
                .withDuration(RECONNECTION_BASE_DELAY, Duration.ofMillis(conf.minReconnectionDelayMillis))
                .withDuration(RECONNECTION_MAX_DELAY, Duration.ofMillis(conf.maxReconnectionDelayMillis))
                .withInt(NETTY_ADMIN_SHUTDOWN_QUIET_PERIOD, conf.quietPeriodBeforeCloseMillis / 1000)
                .withInt(NETTY_ADMIN_SHUTDOWN_TIMEOUT, conf.timeoutBeforeCloseMillis / 1000)
                .withInt(NETTY_IO_SHUTDOWN_QUIET_PERIOD, conf.quietPeriodBeforeCloseMillis / 1000)
                .withInt(NETTY_IO_SHUTDOWN_TIMEOUT, conf.timeoutBeforeCloseMillis / 1000)
                .withBoolean(NETTY_DAEMON, true)
                .withBoolean(RESOLVE_CONTACT_POINTS, conf.resolveContactPoints)
                .withInt(MultipleRetryPolicy.MaxRetryCount, conf.queryRetryCount)
                .withDuration(DseDriverOption.CONTINUOUS_PAGING_TIMEOUT_FIRST_PAGE, Duration.ofMillis(conf.readTimeoutMillis))
                .withDuration(DseDriverOption.CONTINUOUS_PAGING_TIMEOUT_OTHER_PAGES, Duration.ofMillis(conf.readTimeoutMillis))
            }
        
            // compression option cannot be set to NONE (default)
            def compressionProperties(b: PDCLB): PDCLB =
              Option(conf.compression)
                .filter(_.toLowerCase != "none")
                .fold(b)(c => b.withString(PROTOCOL_COMPRESSION, c.toLowerCase))
        
            def localDCProperty(b: PDCLB): PDCLB =
              conf.localDC.map(b.withString(LOAD_BALANCING_LOCAL_DATACENTER, _)).getOrElse(b)
        
            // add ssl properties if ssl is enabled
            def ipBasedConnectionProperties(ipConf: IpBasedContactInfo) = (builder: PDCLB) => {
              builder
                .withStringList(CONTACT_POINTS, ipConf.hosts.map(h => s"${h.getHostString}:${h.getPort}").toList.asJava)
                .withClass(LOAD_BALANCING_POLICY_CLASS, classOf[DefaultLoadBalancingPolicy])
        
              def clientAuthEnabled(value: Option[String]) =
                if (ipConf.cassandraSSLConf.clientAuthEnabled) value else None
        
              if (ipConf.cassandraSSLConf.enabled) {
                Seq(
                  SSL_TRUSTSTORE_PATH -> ipConf.cassandraSSLConf.trustStorePath,
                  SSL_TRUSTSTORE_PASSWORD -> ipConf.cassandraSSLConf.trustStorePassword,
                  SSL_KEYSTORE_PATH -> clientAuthEnabled(ipConf.cassandraSSLConf.keyStorePath),
                  SSL_KEYSTORE_PASSWORD -> clientAuthEnabled(ipConf.cassandraSSLConf.keyStorePassword))
                  .foldLeft(builder) { case (b, (name, value)) =>
                    value.map(b.withString(name, _)).getOrElse(b)
                  }
                  .withClass(SSL_ENGINE_FACTORY_CLASS, classOf[DefaultSslEngineFactory])
                  .withStringList(SSL_CIPHER_SUITES, ipConf.cassandraSSLConf.enabledAlgorithms.toList.asJava)
                  .withBoolean(SSL_HOSTNAME_VALIDATION, false) // TODO: this needs to be configurable by users. Set to false for our integration tests
              } else {
                builder
              }
            }
        
            val universalProperties: Seq[PDCLB => PDCLB] =
              Seq( basicProperties, compressionProperties, localDCProperty)
        
            val appliedProperties: Seq[PDCLB => PDCLB] = conf.contactInfo match {
              case ipConf: IpBasedContactInfo => universalProperties :+ ipBasedConnectionProperties(ipConf)
              case other => universalProperties
            }
        
            appliedProperties.foldLeft(initBuilder){ case (builder, properties) => properties(builder)}
          }
        
          /** Creates and configures native Cassandra connection */
          override def createSession(conf: CassandraConnectorConf): CqlSession = {
            val configLoaderBuilder = DriverConfigLoader.programmaticBuilder()
            val configLoader = connectorConfigBuilder(conf, configLoaderBuilder).build()
        
            val initialBuilder = CqlSession.builder()
        
            val builderWithContactInfo =  conf.contactInfo match {
              case ipConf: IpBasedContactInfo =>
                ipConf.authConf.authProvider.fold(initialBuilder)(initialBuilder.withAuthProvider)
                  .withConfigLoader(configLoader)
              case CloudBasedContactInfo(path, authConf) =>
                authConf.authProvider.fold(initialBuilder)(initialBuilder.withAuthProvider)
                  .withCloudSecureConnectBundle(maybeGetLocalFile(path))
                  .withConfigLoader(configLoader)
              case ProfileFileBasedContactInfo(path) =>
                //Ignore all programmatic config for now ... //todo maybe allow programmatic config here by changing the profile?
                logger.warn(s"Ignoring all programmatic configuration, only using configuration from $path")
                initialBuilder.withConfigLoader(DriverConfigLoader.fromUrl(maybeGetLocalFile(path)))
            }
        
            val appName = Option(SparkEnv.get).map(env => env.conf.getAppId).getOrElse("NoAppID")
            builderWithContactInfo
              .withApplicationName(s"Spark-Cassandra-Connector-$appName")
              .withSchemaChangeListener(new MultiplexingSchemaListener())
              .build()
          }
        
          /**
           * Checks the Spark Temp work directory for the file in question, returning
           * it if exists, returning a generic URL from the string if not
           */
          def maybeGetLocalFile(path: String): URL = {
            val localPath = Paths.get(SparkFiles.get(path))
            if (Files.exists(localPath)) {
              logger.info(s"Found the $path locally at $localPath, using this local file.")
              localPath.toUri.toURL
            } else {
              try {
                new URL(path)
              } catch {
                case e: MalformedURLException =>
                  throw new IOException(s"The provided path $path is not a valid URL nor an existing locally path. Provide an " +
                    s"URL accessible to all executors or a path existing on all executors (you may use `spark.files` to " +
                    s"distribute a file to each executor).", e)
              }
            }
          }
        
          def continuousPagingEnabled(session: CqlSession): Boolean = {
            val confEnabled = SparkEnv.get.conf.getBoolean(CassandraConnectionFactory.continuousPagingParam.name, CassandraConnectionFactory.continuousPagingParam.default)
            val pv = session.getContext.getProtocolVersion
            if (pv.getCode > DseProtocolVersion.DSE_V1.getCode && confEnabled) {
              logger.debug(s"Scan Method Being Set to Continuous Paging")
              true
            } else {
              logger.debug(s"Scan Mode Disabled or Connecting to Non-DSE Cassandra Cluster")
              false
            }
          }
        
          override def getScanner(
                                   readConf: ReadConf,
                                   connConf: CassandraConnectorConf,
                                   columnNames: scala.IndexedSeq[String]): Scanner = {
        
            val isContinuousPagingEnabled =
              new CassandraConnector(connConf).withSessionDo { continuousPagingEnabled }
        
            if (isContinuousPagingEnabled) {
              logger.debug("Using ContinousPagingScanner")
              ContinuousPagingScanner(readConf, connConf, columnNames)
            } else {
              logger.debug("Not Connected to DSE 5.1 or Greater Falling back to Non-Continuous Paging")
              new DefaultScanner(readConf, connConf, columnNames)
            }
          }
        }
      2. The code for connecting to the GeminiDB Cassandra instance is as follows:
        /**
             * There will be security risks if the username and password used for authentication are directly written into code. Store the username and password in ciphertext in the configuration file or environment variables.
             * In this example, the username and password are stored in the environment variables. Before running this example, set environment variables USERNAME_ENV and PASSWORD_ENV as needed.
         */
        val username: String = System.getenv().asScala.mkString("USERNAME_ENV")
        val password: String = System.getenv().asScala.mkString("PASSWORD_ENV")
        val sparkSession = SparkSession
          .builder()
          .appName("Spark Cassandra basic example")
          .master("local")
          .config("spark.cassandra.connection.host", host)
          .config("spark.cassandra.connection.port", port)
          .config("spark.cassandra.auth.username", username)
          .config("spark.cassandra.auth.password", password)
        Set .config("spark.cassandra.connection.factory", "sample.ConnectionFactory") //Set ConnectionFactory as needed.
          .getOrCreate()

  4. Run the sample code to check whether the instance is connected.

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback