以下是关于如何设置内容的一些要点:
通过重新运行作业,我在redshift中获得了重复的行(正如预期的那样)。但是,有没有办法在插入新数据之前替换或删除行,使用密钥或胶水中的分区设置?
import sys
from awsglue.transforms import *
from awsglue.utils import getResolvedOptions
from pyspark.context import SparkContext
from awsglue.context import GlueContext
from awsglue.job import Job
from awsglue.dynamicframe import DynamicFrame
from awsglue.transforms import SelectFields
from pyspark.sql.functions import lit
## @params: [TempDir, JOB_NAME]
args = getResolvedOptions(sys.argv, ['TempDir','JOB_NAME'])
sc = SparkContext()
glueContext = GlueContext(sc)
spark = glueContext.spark_session
job = Job(glueContext)
job.init(args['JOB_NAME'], args)
columnMapping = [
("id", "int", "id", "int"),
("name", "string", "name", "string"),
]
datasource1 = glueContext.create_dynamic_frame.from_catalog(database = "db01", table_name = "table01", transformation_ctx = "datasource0")
applymapping1 = ApplyMapping.apply(frame = datasource1, mappings = columnMapping, transformation_ctx = "applymapping1")
resolvechoice1 = ResolveChoice.apply(frame = applymapping1, choice = "make_cols", transformation_ctx = "resolvechoice1")
dropnullfields1 = DropNullFields.apply(frame = resolvechoice1, transformation_ctx = "dropnullfields1")
df1 = dropnullfields1.toDF()
data1 = df1.withColumn('platform', lit('test'))
data1 = DynamicFrame.fromDF(data1, glueContext, "data_tmp1")
## Write data to redshift
datasink1 = glueContext.write_dynamic_frame.from_jdbc_conf(frame = data1, catalog_connection = "Test Connection", connection_options = {"dbtable": "table01", "database": "db01"}, redshift_tmp_dir = args["TempDir"], transformation_ctx = "datasink1")
job.commit()
答案 0 :(得分:12)
工作书签是关键。只需编辑作业并启用"作业书签"它不会处理已处理的数据。 请注意,作业必须重新运行一次才能检测到它不必再次重新处理旧数据。
有关详细信息,请参阅: http://docs.aws.amazon.com/glue/latest/dg/monitor-continuations.html
名称"书签"在我看来有点牵强。如果我在搜索期间没有巧合地偶然发现它,我将永远不会看到它。
答案 1 :(得分:4)
这是我从AWS Glue Support获得的解决方案:
您可能知道,虽然您可以创建主键,但Redshift不会强制实现唯一性。因此,如果您正在重新运行Glue作业,则可以插入重复的行。保持唯一性的一些方法是:
使用临时表插入所有行,然后在主表中执行upsert / merge [1],这必须在胶水之外完成。
在redshift表[1]中添加另一列,如插入时间戳,以允许重复,但要知道哪一个是第一个或最后一个,然后在需要时删除副本。
< / LI>将先前插入的数据加载到数据框中,然后比较要插入的数据以避免插入重复项[3]
[1] - http://docs.aws.amazon.com/redshift/latest/dg/c_best-practices-upsert.html和http://www.silota.com/blog/amazon-redshift-upsert-support-staging-table-replace-rows/
[2] - https://github.com/databricks/spark-redshift/issues/238
[3] - https://docs.databricks.com/spark/latest/faq/join-two-dataframes-duplicated-column.html
答案 2 :(得分:2)
今天,我已经测试并找到了一种解决方法,可以使用JDBC连接从目标表中进行更新/删除。
我使用了以下
import sys
from awsglue.transforms import *
from awsglue.utils import getResolvedOptions
from pyspark.context import SparkContext
from awsglue.context import GlueContext
from awsglue.job import Job
import pg8000
args = getResolvedOptions(sys.argv, [
'JOB_NAME',
'PW',
'HOST',
'USER',
'DB'
])
# ...
# Create Spark & Glue context
sc = SparkContext()
glueContext = GlueContext(sc)
spark = glueContext.spark_session
job = Job(glueContext)
job.init(args['JOB_NAME'], args)
# ...
config_port = ****
conn = pg8000.connect(
database=args['DB'],
user=args['USER'],
password=args['PW'],
host=args['HOST'],
port=config_port
)
query = "UPDATE table .....;"
cur = conn.cursor()
cur.execute(query)
conn.commit()
cur.close()
query1 = "DELETE AAA FROM AAA A, BBB B WHERE A.id = B.id"
cur1 = conn.cursor()
cur1.execute(query1)
conn.commit()
cur1.close()
conn.close()
答案 3 :(得分:0)
Glue中的作业书签选项应该可以解决问题,如上所述。当我的源码是S3时,我一直在成功使用它。 http://docs.aws.amazon.com/glue/latest/dg/monitor-continuations.html
答案 4 :(得分:0)
根据我的测试(在相同的情况下),BOOKMARK功能不起作用。多次运行作业时,将插入重复的数据。通过每天(通过lambda)从S3位置删除文件并实现暂存和目标表,可以解决此问题。数据将根据匹配的键列进行插入/更新。
答案 5 :(得分:0)
请检查this答案。有说明和代码示例如何使用登台表将数据上载到Redshift中。在Glue使用preactions
和postactions
选项写入数据之前或之后,可以使用相同的方法来运行任何SQL查询:
// Write data to staging table in Redshift
glueContext.getJDBCSink(
catalogConnection = "redshift-glue-connections-test",
options = JsonOptions(Map(
"database" -> "conndb",
"dbtable" -> staging,
"overwrite" -> "true",
"preactions" -> "<another SQL queries>",
"postactions" -> "<some SQL queries>"
)),
redshiftTmpDir = tempDir,
transformationContext = "redshift-output"
).writeDynamicFrame(datasetDf)