• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Scala AmazonDynamoDBClient类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Scala中com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient的典型用法代码示例。如果您正苦于以下问题:Scala AmazonDynamoDBClient类的具体用法?Scala AmazonDynamoDBClient怎么用?Scala AmazonDynamoDBClient使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了AmazonDynamoDBClient类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Scala代码示例。

示例1: client

//设置package包名称以及导入依赖的类
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)
  }
} 
开发者ID:zalando-nakadi,项目名称:reactive-nakadi,代码行数:26,代码来源:ClientProvider.scala


示例2: createTableIfNotExist

//设置package包名称以及导入依赖的类
package common

import com.amazonaws.services.dynamodbv2.document.{ DynamoDB, Table }
import com.amazonaws.services.dynamodbv2.model._
import com.amazonaws.services.dynamodbv2.{ AmazonDynamoDB, AmazonDynamoDBClient }

import scala.collection.JavaConversions._


trait DynamoDBTableHelper {

  def createTableIfNotExist(describe: DescribeTableRequest, create: CreateTableRequest): Unit = try {
    dynamoDB.describeTable(describe)
  } catch {
    case e: ResourceNotFoundException => // ???????????????
      dynamoDB.createTable(create)
  }

  def deleteContents(scan: ScanRequest, deleteItem: Map[String, AttributeValue] => DeleteItemRequest): Unit =
    dynamoDB.scan(scan).getItems.toList.foreach { kv => // ??????????????
      dynamoDB.deleteItem(deleteItem(kv.toMap))
    }

  def deleteTable(): Unit = table.delete()

  val db = new DynamoDB(dynamoDB)

  def table: Table

  private def dynamoDB: AmazonDynamoDB = {
    val client = new AmazonDynamoDBClient()
    client.setEndpoint("http://localhost:8000")
    client
  }
} 
开发者ID:cm-wada-yusuke,项目名称:ecn-news,代码行数:36,代码来源:DynamoDBTableHelper.scala


示例3: AwsContextProvider

//设置package包名称以及导入依赖的类
package aws

import com.amazonaws.auth.{
  AWSCredentialsProviderChain,
  AWSStaticCredentialsProvider,
  BasicAWSCredentials,
  ContainerCredentialsProvider
}
import com.amazonaws.auth.profile.ProfileCredentialsProvider
import com.amazonaws.regions.Regions
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient
import com.amazonaws.services.s3.AmazonS3Client
import play.api.Logger

object AwsContextProvider {

  def genContext(isRunningInCompose: Boolean, region: Regions): (AmazonDynamoDBClient, AmazonS3Client) = {
    if (isRunningInCompose) {
      Logger.info("Running in compose")
      System.setProperty("com.amazonaws.sdk.disableCertChecking", "true")
      val awsCreds                      = new AWSStaticCredentialsProvider(new BasicAWSCredentials("key", "secret"))
      val dClient: AmazonDynamoDBClient = new AmazonDynamoDBClient(awsCreds).withRegion(region)
      val s3Client: AmazonS3Client      = new AmazonS3Client(awsCreds).withRegion(region)
      dClient.setEndpoint(sys.env("LOCAL_DYNAMO"))
      (dClient, s3Client)
    } else {
      val awsCreds = new AWSCredentialsProviderChain(
        new ContainerCredentialsProvider(),
        new ProfileCredentialsProvider()
      )
      (new AmazonDynamoDBClient(awsCreds).withRegion(region), new AmazonS3Client(awsCreds).withRegion(region))
    }
  }
} 
开发者ID:ovotech,项目名称:comms-template-manager,代码行数:35,代码来源:AwsContextProvider.scala


示例4: AwsProvider

//设置package包名称以及导入依赖的类
package com.ovoenergy.orchestration.aws

import com.amazonaws.auth._
import com.amazonaws.auth.profile.ProfileCredentialsProvider
import com.amazonaws.regions.Regions
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient
import com.ovoenergy.comms.templates.TemplatesContext
import org.slf4j.LoggerFactory

object AwsProvider {

  private val log = LoggerFactory.getLogger("AwsClientProvider")

  def dynamoClient(isRunningInLocalDocker: Boolean, region: Regions): AmazonDynamoDBClient = {
    if (isRunningInLocalDocker) {
      log.warn("Running in local docker")
      System.setProperty("com.amazonaws.sdk.disableCertChecking", "true")
      val awsCreds                           = getCreds(isRunningInLocalDocker, region)
      val dynamoClient: AmazonDynamoDBClient = new AmazonDynamoDBClient(awsCreds).withRegion(region)
      dynamoClient.setEndpoint(sys.env("LOCAL_DYNAMO"))
      dynamoClient
    } else {
      val awsCreds = getCreds(isRunningInLocalDocker, region)
      new AmazonDynamoDBClient(awsCreds).withRegion(region)
    }
  }

  def templatesContext(isRunningInLocalDocker: Boolean, region: Regions) = {
    val awsCreds = getCreds(isRunningInLocalDocker, region)
    TemplatesContext.cachingContext(awsCreds)
  }

  private def getCreds(isRunningInLocalDocker: Boolean, region: Regions): AWSCredentialsProvider = {
    if (isRunningInLocalDocker)
      new AWSStaticCredentialsProvider(new BasicAWSCredentials("key", "secret"))
    else
      new AWSCredentialsProviderChain(
        new ContainerCredentialsProvider(),
        new ProfileCredentialsProvider()
      )
  }
} 
开发者ID:ovotech,项目名称:comms-orchestration,代码行数:43,代码来源:AwsProvider.scala


示例5: AwsClientProvider

//设置package包名称以及导入依赖的类
package aws

import com.amazonaws.auth.{
  AWSCredentialsProviderChain,
  AWSStaticCredentialsProvider,
  BasicAWSCredentials,
  ContainerCredentialsProvider
}
import com.amazonaws.auth.profile.ProfileCredentialsProvider
import com.amazonaws.regions.Regions
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient
import com.amazonaws.services.s3.AmazonS3Client
import play.api.Logger

object AwsClientProvider {

  def genClients(runningInDockerOnDevMachine: Boolean, region: Regions): (AmazonDynamoDBClient, AmazonS3Client) = {
    if (runningInDockerOnDevMachine) {
      Logger.info("Running in compose")
      System.setProperty("com.amazonaws.sdk.disableCertChecking", "true")
      val awsCreds = new AWSStaticCredentialsProvider(new BasicAWSCredentials("key", "secret"))
      val dClient: AmazonDynamoDBClient = new AmazonDynamoDBClient(awsCreds).withRegion(region)
      val s3Client: AmazonS3Client = new AmazonS3Client(awsCreds).withRegion(region)
      dClient.setEndpoint(sys.env("LOCAL_DYNAMO"))
      (dClient, s3Client)
    } else {
      val awsCreds = new AWSCredentialsProviderChain(
        new ContainerCredentialsProvider(),
        new ProfileCredentialsProvider("default")
      )
      (new AmazonDynamoDBClient(awsCreds).withRegion(region), new AmazonS3Client(awsCreds).withRegion(region))
    }
  }
} 
开发者ID:ovotech,项目名称:comms-audit-log,代码行数:35,代码来源:AwsClientProvider.scala


示例6: LastLoadedRepositoryImpl

//设置package包名称以及导入依赖的类
package org.nisshiee.chatwork_slack_relay.infra.chatwork

import scala.concurrent.{ ExecutionContext, Future }
import scala.util.Try

import com.amazonaws.auth.EnvironmentVariableCredentialsProvider
import com.amazonaws.regions.Regions
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient
import com.amazonaws.services.dynamodbv2.document._
import com.github.nscala_time.time.Imports._
import com.typesafe.config.ConfigFactory
import net.ceedubs.ficus.Ficus._
import org.nisshiee.chatwork_slack_relay.domain.chatwork._

object LastLoadedRepositoryImpl extends LastLoadedRepository {
  lazy val config = ConfigFactory.load
  lazy val region = Regions.fromName(config.as[String]("aws.region"))
  lazy val tableName = config.as[String]("aws.dynamodb.lastLoaded.table")
  lazy val table = {
    val client: AmazonDynamoDBClient = new AmazonDynamoDBClient(
      new EnvironmentVariableCredentialsProvider()
    ).withRegion(region)
    val dynamoDB = new DynamoDB(client)
    dynamoDB.getTable(tableName)
  }

  lazy val roomIdKey = "roomId"
  lazy val sendTimeKey = "sendTime"
  lazy val updateTimeKey = "updateTime"

  implicit class RichItem(val self: Item) extends AnyVal {
    def getDateTime(name: String): Option[DateTime] =
      Try(self.getLong(name)).toOption.map { l: Long => new DateTime(l) }
  }

  override def get(room: Room)(implicit ec: ExecutionContext) = Future {
    (for {
      item <- Option(table.getItem(new KeyAttribute(roomIdKey, room.id.value)))
      sendTime <- item.getDateTime(sendTimeKey)
      updateTime <- item.getDateTime(updateTimeKey)
    } yield LastLoaded(sendTime, updateTime)).getOrElse(LastLoaded.default)
  }

  override def set(room: Room, lastLoaded: LastLoaded)(implicit ec: ExecutionContext) = Future {
    val item = new Item().
      withPrimaryKey(roomIdKey, room.id.value).
      withLong(sendTimeKey, lastLoaded.sendTime.getMillis).
      withLong(updateTimeKey, lastLoaded.updateTime.getMillis)
    table.putItem(item)
    ()
  }
}

trait MixinLastLoadedRepository extends UsesLastLoadedRepository {
  override val lastLoadedRepository = LastLoadedRepositoryImpl
} 
开发者ID:nisshiee,项目名称:chatwork-slack-relay,代码行数:57,代码来源:LastLoadedRepositoryImpl.scala


示例7: DynamoDbConfigurationSource

//设置package包名称以及导入依赖的类
package com.gu.cm

import com.amazonaws.auth.DefaultAWSCredentialsProviderChain
import com.amazonaws.regions.RegionUtils
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient
import com.amazonaws.services.dynamodbv2.document.{PrimaryKey, DynamoDB}
import com.typesafe.config.{ConfigFactory, Config}
import scala.util.{Failure, Success, Try}

class DynamoDbConfigurationSource(dynamoDb: DynamoDB, identity: Identity, prefix: String) extends ConfigurationSource {

  val tableName = s"$prefix${identity.stack}"

  override def load: Config = {
    val config = for {
      table <- Try(dynamoDb.getTable(tableName))
      item <- Try(table.getItem(new PrimaryKey("App", identity.app, "Stage", identity.stage)))
    } yield {
      ConfigFactory.parseMap(item.getMap("Config"), s"Dynamo DB table $tableName [App=${identity.app}, Stage=${identity.stage}]")
    }

    config match {
      case Success(theConfig) => theConfig
      case Failure(theFailure) => ConfigFactory.empty(s"no DynamoDB config (or failed to load) for $tableName [App=${identity.app}, Stage=${identity.stage}], exception=[$theFailure]")
    }
  }
}

object DynamoDbConfigurationSource {
  def apply(identity: Identity, prefix: String = "config-"): DynamoDbConfigurationSource = {
    val dynamoDb = {
      val client = new AmazonDynamoDBClient(new DefaultAWSCredentialsProviderChain())
      client.setRegion(RegionUtils.getRegion(identity.region))
      new DynamoDB(client)
    }
    new DynamoDbConfigurationSource(dynamoDb, identity, prefix)
  }
} 
开发者ID:guardian,项目名称:configuration-magic,代码行数:39,代码来源:DynamoDbConfigurationSource.scala


示例8: DynamoDbConfigurationSourceSpec

//设置package包名称以及导入依赖的类
package com.gu.cm

import com.amazonaws.auth.DefaultAWSCredentialsProviderChain
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient
import com.amazonaws.services.dynamodbv2.document.{Item, DynamoDB}
import com.amazonaws.services.dynamodbv2.model._
import com.typesafe.config.ConfigFactory
import org.specs2.mutable.Specification
import org.specs2.specification.Scope

import scala.collection.JavaConverters._

class DynamoDbConfigurationSourceSpec extends Specification {
  "a dynamo DB configuration Source" should {
    "load a config from dynamodb" in new DynamoDbScope {
      val conf = dynamoDbConfigurationSource.load

      conf.getInt("foo.a") shouldEqual 42
    }
  }

  trait DynamoDbScope extends Scope {
    val dynamoDb = {
      val client = new AmazonDynamoDBClient(new DefaultAWSCredentialsProviderChain())
      client.setEndpoint("http://localhost:8000")
      new DynamoDB(client)
    }

    val identity = AwsApplication(
      stack = "test-stack",
      app = "configuration-magic",
      stage = "test",
      region = "eu-west-1"
    )

    val dynamoDbConfigurationSource = new DynamoDbConfigurationSource(dynamoDb, identity, "prefix-")

    def initTable() = {
      val req = new CreateTableRequest(
        "prefix-test-stack",
        List(
          new KeySchemaElement("App", KeyType.HASH),
          new KeySchemaElement("Stage", KeyType.RANGE)
        ).asJava).withAttributeDefinitions(
        new AttributeDefinition("App", "S"),
        new AttributeDefinition("Stage", "S")
      ).withProvisionedThroughput(new ProvisionedThroughput(1000L, 1000L))

      val table = dynamoDb.createTable(req)

      val confBlob = ConfigFactory.parseResourcesAnySyntax("sample").root.unwrapped()

      val item = new Item()
        .withString("App", "configuration-magic")
        .withString("Stage", "test")
        .withMap("Config", confBlob)
      table.putItem(item)
    }
    initTable()
  }
} 
开发者ID:guardian,项目名称:configuration-magic,代码行数:62,代码来源:DynamoDbConfigurationSourceSpec.scala


示例9: StockTradeAPI

//设置package包名称以及导入依赖的类
import java.io.{InputStream, OutputStream}

import com.amazonaws.regions.Regions
import com.amazonaws.services.dynamodbv2.model.{DescribeStreamRequest, GetRecordsRequest, GetShardIteratorRequest, ShardIteratorType}
import com.amazonaws.services.dynamodbv2.{AmazonDynamoDB, AmazonDynamoDBClient, AmazonDynamoDBStreams, AmazonDynamoDBStreamsClient}
import com.amazonaws.services.lambda.runtime.{Context, RequestStreamHandler}
import com.google.gson.Gson

import scala.collection.JavaConversions._
import scala.collection.JavaConverters._
import scala.io.Source


class StockTradeAPI extends RequestStreamHandler {

  private val STATS_SOURCE_TABLE: String = "StockTradeStats"

  def handleRequest(input: InputStream, output: OutputStream, context: Context) = {

    Source.fromInputStream(input, "utf-8").getLines().foreach(println)

    lazy val db: AmazonDynamoDB = new AmazonDynamoDBClient().withRegion(Regions.EU_WEST_1)
    lazy val dbs: AmazonDynamoDBStreams = new AmazonDynamoDBStreamsClient().withRegion(Regions.EU_WEST_1)

    lazy val tab = db.describeTable(STATS_SOURCE_TABLE).getTable

    val records = dbs.describeStream(
      new DescribeStreamRequest().withStreamArn(tab.getLatestStreamArn)
    ).getStreamDescription.getShards.toList.flatMap { shard =>
      println(s"processing ${shard.getShardId}")
      dbs.getRecords(
        new GetRecordsRequest()
          .withShardIterator(
            dbs.getShardIterator(
              new GetShardIteratorRequest()
                .withStreamArn(tab.getLatestStreamArn)
                .withShardIteratorType(ShardIteratorType.TRIM_HORIZON)
                .withShardId(shard.getShardId)
            ).getShardIterator
          ).withLimit(100)
      ).getRecords.toList.map { record =>
        println(s"adding record ${record.getDynamodb.getSequenceNumber}")
        record.getDynamodb.getNewImage.values().toSeq.map{v =>
          println(s"extracting value $v")
          Option(v.getN).getOrElse(v.getS)
        }.asJavaCollection
      }.asJavaCollection
    }.asJavaCollection

    println(s"compute $records")

    output.write(new Gson().toJson(records).getBytes())
  }

} 
开发者ID:mugglmenzel,项目名称:StockTradeFetcherLambda,代码行数:56,代码来源:StockTradeAPI.scala


示例10: DynamoDBTableLoader

//设置package包名称以及导入依赖的类
import com.amazonaws.auth.{AWSCredentials, AWSCredentialsProvider, BasicAWSCredentials}
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient
import com.amazonaws.services.dynamodbv2.document.DynamoDB
import com.amazonaws.services.dynamodbv2.model._

object DynamoDBTableLoader {

  val awsCredProvider = new AWSCredentialsProvider {
    override def refresh(): Unit = ()

    override def getCredentials: AWSCredentials = new BasicAWSCredentials("accessKey", "secretKey")
  }

  val dynamoDBClient = new AmazonDynamoDBClient(awsCredProvider.getCredentials)
  dynamoDBClient.setEndpoint("http://localhost:8000")
  val dynamoDB = new DynamoDB(dynamoDBClient)
  dynamoDB.shutdown()


  //Helper functions
  private val keySchemaType = (name: String, keyType: KeyType) => new KeySchemaElement().withAttributeName(name).withKeyType(keyType)
  private val keySchema = (name: String) => keySchemaType(name, KeyType.HASH)
  private val attributeDefType = (name: String, attrType: ScalarAttributeType) => new AttributeDefinition().withAttributeName(name).withAttributeType(attrType)
  private val attributeDef = (name: String) => attributeDefType(name, ScalarAttributeType.S)
  private val provisionedThroughput = (throughput: Long) => new ProvisionedThroughput().withReadCapacityUnits(throughput).withWriteCapacityUnits(throughput)
  private val globalSecondaryIndex = (indexName: String, keySchema: Seq[KeySchemaElement], throughput: ProvisionedThroughput) =>
    new GlobalSecondaryIndex()
      .withIndexName(indexName)
      .withKeySchema(keySchema: _*)
      .withProjection(new Projection().withProjectionType(ProjectionType.ALL))
      .withProvisionedThroughput(throughput)

  def initTestTable() = {
    new CreateTableRequest()
        .withTableName("test_table")
        .withKeySchema(keySchema("my_key"))

  }
} 
开发者ID:lifeway,项目名称:play-dynamodb,代码行数:40,代码来源:DynamoDBTableLoader.scala


示例11: DataLoaderSpec

//设置package包名称以及导入依赖的类
import com.amazonaws.services.dynamodbv2.document.{Item, Table, DynamoDB}
import com.amazonaws.services.dynamodbv2.{AmazonDynamoDB, AmazonDynamoDBClient}
import org.junit.runner.RunWith
import org.specs2.mutable.Specification
import org.specs2.runner.JUnitRunner
import play.api.{Logger, Play}
import play.api.test.WithApplication

import scala.xml.XML

@RunWith(classOf[JUnitRunner])
class DataLoaderSpec extends Specification{

  "DataLoader" should {
    "load data" in new WithApplication {
      val client = new AmazonDynamoDBClient()
        .withEndpoint("http://localhost:8000").asInstanceOf[AmazonDynamoDB]

      val dynamoDB = new DynamoDB(client)

      val table = (dynamoDB.getTable("SegmentEvents")).asInstanceOf[Table]


      val pid = "p1234564"
      val version_pid = "b1234564"
      val segment_pid = "p5432104"


      val doc = Play.getFile("public/segment_event/segment_event_test.xml")

      val xml = XML.loadFile(doc)
      val document = xml.toString()
      //val document = "<segment_event/>"
      Logger.info(document)
      val item = new Item()
        .withPrimaryKey("version_pid", version_pid, "pid", pid)
        .withString("segment_id", segment_pid)
        .withString("document", document)

      try {
        Logger.info("Attempting to load data; please wait...")
        table.putItem(item)

        Logger.info("Success.... ")

      } catch {
        case e: Exception => {
          Logger.info("Unable to add document: ")
          Logger.info(e.getMessage())
        }

      }

    }
  }

} 
开发者ID:KarenPudner,项目名称:store_innovation,代码行数:58,代码来源:DataLoaderSpec.scala



注:本文中的com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Scala TaskMetrics类代码示例发布时间:2022-05-23
下一篇:
Scala ReadableByteChannel类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap