score:1

your implementation must be something like this, and hope it helps.

package.scala

package com.app.akka

package object connector {
  // do some codes here..
}

cassandraservice.scala

package com.app.akka
import com.app.akka.connector._

object cassandraservice {
  def selectfromcassandra() = {
    // do some codes here..
  }
}

score:1

you have two issue with your current code.

  1. when you compile your package object connector it is throwing below error

    error:(14, 35) recursive value executioncontext needs type implicit val executioncontext = executioncontext issue is with implicit val executioncontext = executioncontext line solution for this issue would be as below.

    implicit val executioncontext = executioncontext

  2. when we compile cassandraservice then it is throwing error as below

    error:(17, 13) cannot find an implicit executioncontext. you might pass an (implicit ec: executioncontext) parameter to your method or import scala.concurrent.executioncontext.implicits.global. rows.map{item =>

error clearly say that either we need to pass executioncontext as implicit parameter or import scala.concurrent.executioncontext.implicits.global. in my system both issues are resolved and its compiled successfully. i have attached code for your reference.

package com.apache.scala

import akka.actor.actorsystem
import akka.stream.actormaterializer
import com.datastax.driver.core.cluster
import scala.concurrent.executioncontext


package object connector {

  implicit val system = actorsystem()
  implicit val mat = actormaterializer()

  implicit val executioncontext = executioncontext

  implicit val session = cluster
    .builder
    .addcontactpoints("localhost")
    .withport(9042)
    .build()
    .connect()

}

package com.apache.scala.connector

import akka.stream.alpakka.cassandra.scaladsl.cassandrasource
import akka.stream.scaladsl.sink
import com.datastax.driver.core.{row, simplestatement}

import scala.collection.immutable
import scala.concurrent.executioncontext.implicits.global
import scala.concurrent.future

object cassandraservice {

  def selectfromcassandra() = {

    val statement = new simplestatement(s"select * from animals.alpakka").setfetchsize(20)
    val rows: future[immutable.seq[row]] = cassandrasource(statement).runwith(sink.seq)

    rows.map{item =>
      print(item)

    }
  }

}

Related Query

More Query from same tag