如何将PySpark数据帧写入DynamoDB表?

时间:2018-10-29 10:57:59

标签: pyspark amazon-dynamodb

如何将PySpark数据帧写入DynamoDB表?找不到太多有关此的信息。根据我的要求,我必须将PySpark数据帧写入Dynamo数据库表。总的来说,我需要从我的PySpark代码读取/写入发电机。

谢谢。

3 个答案:

答案 0 :(得分:1)

Ram,无法直接从pyspark做到这一点。如果您正在运行管道软件,则可以通过一系列步骤来完成。这是可以完成的方法:

  1. 创建一个临时配置单元表,例如

    CREATE TABLE TEMP( column1 type, column2 type...) STORED AS ORC;

  2. 运行pySpark作业并将数据写入其中

    dataframe.createOrReplaceTempView("df") spark.sql("INSERT OVERWRITE TABLE temp SELECT * FROM df")

  3. 创建发电机连接器表

    CREATE TABLE TEMPTODYNAMO( column1 type, column2 type...) STORED BY 'org.apache.hadoop.hive.dynamodb.DynamoDBStorageHandler' TBLPROPERTIES ("dynamodb.table.name" = "temp-to-dynamo", "dynamodb.column.mapping" = "column1:column1,column2:column2...";

  4. 用临时表覆盖该表

    INSERT OVERWRITE TABLE TEMPTODYNAMO SELECT * FROM TEMP;

更多信息在这里: https://docs.aws.amazon.com/emr/latest/ReleaseGuide/EMR_Hive_Commands.html

答案 1 :(得分:1)

您可以使用 spark-dynamodb

来自他们的回购:

# Load a DataFrame from a Dynamo table. Only incurs the cost of a single scan for schema inference.
dynamoDf = spark.read.option("tableName", "SomeTableName") \
                     .format("dynamodb") \
                     .load() # <-- DataFrame of Row objects with inferred schema.

# Scan the table for the first 100 items (the order is arbitrary) and print them.
dynamoDf.show(100)

# write to some other table overwriting existing item with same keys
dynamoDf.write.option("tableName", "SomeOtherTable") \
              .format("dynamodb") \
              .save()

答案 2 :(得分:0)

此 AWS 博客介绍了如何使用 AWS Glue 创建唯一键、分区和将 S3 数据 (csv) 写入 DynamoDB 表。

How realtor.com® maximized data upload from Amazon S3 into Amazon DynamoDB