com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient Scala Examples

The following examples show how to use com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: EagerDynamoDBDalek.scala    From aws-training-demo   with Apache License 2.0 5 votes vote down vote up
package aws.daleks.eager

import com.amazonaws.auth.AWSCredentialsProvider
import com.amazonaws.regions.Region
import com.amazonaws.services.s3.AmazonS3Client
import com.amazonaws.regions.ServiceAbbreviations
import scala.collection.JavaConverters._
import com.amazonaws.services.s3.model.{ Region => S3Region }
import com.amazonaws.services.s3.model.S3ObjectSummary
import com.amazonaws.services.s3.model.Bucket
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient
import aws.daleks.util.Humid

class EagerDynamoDBDalek(implicit region: Region, credentials: AWSCredentialsProvider) extends Dalek {
  val dynamo = withRegion(new AmazonDynamoDBClient(credentials), region)

  def exterminate = {
    val tables: Seq[String] = dynamo.listTables.getTableNames asScala

    tables foreach { t =>
      info(this,s"Exterminating DyanmoDB Table ${t}")
      Humid { dynamo.deleteTable(t) }
    }

  }
} 
Example 2
Source File: ClientProvider.scala    From reactive-nakadi   with MIT License 5 votes vote down vote up
package org.zalando.react.nakadi.commit.handlers.aws

import com.amazonaws.regions.Regions
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain
import com.amazonaws.services.dynamodbv2.document.DynamoDB
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient
import org.zalando.react.nakadi.properties.CommitProperties


trait Provider {
  def client: DynamoDB
  def leaseProperties: CommitProperties
}

class ClientProvider(override val leaseProperties: CommitProperties) extends Provider {

  private val credentialsProviderChain = new DefaultAWSCredentialsProviderChain()
  private val region = Regions.fromName(leaseProperties.awsCommitRegion)

  override val client: DynamoDB = {
    val c = new AmazonDynamoDBClient(credentialsProviderChain)
    c.configureRegion(region)
    new DynamoDB(c)
  }
} 
Example 3
Source File: DynamoDBClient.scala    From vinyldns   with Apache License 2.0 5 votes vote down vote up
package vinyldns.dynamodb.repository

import com.amazonaws.auth.{AWSStaticCredentialsProvider, BasicAWSCredentials}
import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration
import com.amazonaws.services.dynamodbv2.{AmazonDynamoDBClient, AmazonDynamoDBClientBuilder}

object DynamoDBClient {

  def apply(dynamoDBDataStoreSettings: DynamoDBDataStoreSettings): AmazonDynamoDBClient = {
    val dynamoAKID = dynamoDBDataStoreSettings.key
    val dynamoSecret = dynamoDBDataStoreSettings.secret
    val dynamoEndpoint = dynamoDBDataStoreSettings.endpoint
    val dynamoRegion = dynamoDBDataStoreSettings.region

    System.getProperties.setProperty("aws.accessKeyId", dynamoAKID)
    System.getProperties.setProperty("aws.secretKey", dynamoSecret)
    val credentials = new BasicAWSCredentials(dynamoAKID, dynamoSecret)
    AmazonDynamoDBClientBuilder
      .standard()
      .withCredentials(new AWSStaticCredentialsProvider(credentials))
      .withEndpointConfiguration(new EndpointConfiguration(dynamoEndpoint, dynamoRegion))
      .build()
      .asInstanceOf[AmazonDynamoDBClient]
  }
} 
Example 4
Source File: DynamoSchema.scala    From orders-aws   with Apache License 2.0 5 votes vote down vote up
package works.weave.socks.spring.aws

import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient
import com.amazonaws.services.dynamodbv2.model._

import org.slf4j.LoggerFactory

import scala.collection.JavaConverters._
import scala.language.reflectiveCalls

import works.weave.socks.spring.Ops._

abstract class DynamoSchema(dynamoConnection : DynamoConfiguration) {

  val LOG = LoggerFactory.getLogger(getClass)

  def createMissing(client : AmazonDynamoDBClient) : Unit = {
    val tableNames = client.listTables().getTableNames.asScala.toSet

    schema { table =>
      val name = table.getTableName
      if (tableNames contains name) {
        LOG.info("Table '{}' present", name)
      } else {
        LOG.info("Table '{}' missing, creating...", name)
        client.createTable(table)
        LOG.info("Table '{}' created", name)
      }
    }
  }

  
  def pollWithTimeout(timeout : Int) = new {
    def until(f : => Boolean) : Boolean = {
      def loop(timeoutBudget : Int)(delayMillis : Int) : Boolean = {
        if (f) {
          true
        } else if (timeoutBudget <= 0) {
          false
        } else {
          Thread.sleep(Math.min(timeoutBudget, delayMillis))
          loop(timeoutBudget - delayMillis)(delayMillis * 2)
        }
      }
      loop(timeout)(10)
    }
  }

  def resetDestructively(client : AmazonDynamoDBClient) : Unit = {
    val tableNames = client.listTables().getTableNames.asScala.toSet

    schema { table =>
      val name = table.getTableName
      if (tableNames contains name) {
        LOG.info("Table '{}' present, destroying...", name)

        client.deleteTable(name)
        LOG.info("Awaiting deletion")
        pollWithTimeout(60000) until {
          try {
            client.describeTable(name)
            false
          } catch {
            case e : ResourceNotFoundException =>
              true
          }
        }
        //client.describeTable(name).table.tableStatus.

      }

      LOG.info("Table '{}' creating...", name)
      client.createTable(table)
      LOG.info("Table '{}' created", name)

    }
  }

  protected def schema(declare : CreateTableRequest => Any) : Unit

  val hash = KeyType.HASH
  val range = KeyType.RANGE

  final protected def keySchemaElement(name : String, keyType : KeyType) =
    new KeySchemaElement(name, keyType)

  final protected def attributeDefinition(name : String, scalarAttributeType : ScalarAttributeType) =
    new AttributeDefinition(name, scalarAttributeType)

  final protected def table(name : String,
    attributeDefinitions : Seq[AttributeDefinition],
    keySchema : Seq[KeySchemaElement],
    provisionedThrougput : ProvisionedThroughput) : CreateTableRequest = (new CreateTableRequest()
    after (_.setTableName(name))
    after (_.setAttributeDefinitions(attributeDefinitions.asJava))
    after (_.setKeySchema(keySchema.asJava))
    after (_.setProvisionedThroughput(provisionedThrougput)))

} 
Example 5
Source File: DynamoService.scala    From iep-apps   with Apache License 2.0 5 votes vote down vote up
package com.netflix.iep.archaius

import java.util.concurrent.Executors
import java.util.concurrent.atomic.AtomicLong
import javax.inject.Inject
import javax.inject.Singleton

import com.amazonaws.services.dynamodbv2.AmazonDynamoDB
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient
import com.netflix.iep.service.AbstractService
import com.typesafe.config.Config

import scala.concurrent.ExecutionContext
import scala.concurrent.Future


@Singleton
class DynamoService @Inject()(client: AmazonDynamoDB, config: Config) extends AbstractService {

  private val nextId = new AtomicLong()
  private val pool = Executors.newFixedThreadPool(
    Runtime.getRuntime.availableProcessors(),
    (r: Runnable) => {
      new Thread(r, s"dynamo-db-${nextId.getAndIncrement()}")
    }
  )
  private val ec = ExecutionContext.fromExecutorService(pool)

  override def startImpl(): Unit = ()

  override def stopImpl(): Unit = {
    client match {
      case c: AmazonDynamoDBClient => c.shutdown()
      case _                       =>
    }
  }

  def execute[T](task: AmazonDynamoDB => T): Future[T] = Future(task(client))(ec)
}