将Spark数据框转换为AWS胶动态框

时间:2019-11-24 04:25:52

标签: apache-spark pyspark aws-glue

我尝试将spark数据帧转换为动态数据,然后以胶粘木地板文件的形式输出,但出现错误

  

“ DataFrame”对象没有属性“ fromDF””

我的代码使用大量Spark数据帧。有没有一种方法可以将Spark数据帧转换为动态帧,以便可以将其粘贴为胶粘地板?如果可以,请提供示例,并在下面指出我做错了什么?

代码:

# importing libraries

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

glueContext = GlueContext(SparkContext.getOrCreate())

# updated 11/19/19 for error caused in error logging function

spark = glueContext.spark_session

from pyspark.sql import Window
from pyspark.sql.functions import col
from pyspark.sql.functions import first
from pyspark.sql.functions  import date_format
from pyspark.sql.functions import lit,StringType
from pyspark.sql.types import *
from pyspark.sql.functions import substring, length, min,when,format_number,dayofmonth,hour,dayofyear,month,year,weekofyear,date_format,unix_timestamp


base_pth='s3://test/'

bckt_pth1=base_pth+'test_write/glueparquet/'


test_df=glueContext.create_dynamic_frame.from_catalog(
                 database='test_inventory',
                 table_name='inventory_tz_inventory').toDF()

test_df.fromDF(test_df, glueContext, "test_nest")


glueContext.write_dynamic_frame.from_options(frame = test_nest,
                                             connection_type = "s3",
                                             connection_options = {"path": bckt_pth1+'inventory'},
                                             format = "glueparquet")

错误:

'DataFrame' object has no attribute 'fromDF'
Traceback (most recent call last):
  File "/mnt/yarn/usercache/livy/appcache/application_1574556353910_0001/container_1574556353910_0001_01_000001/pyspark.zip/pyspark/sql/dataframe.py", line 1300, in __getattr__
    "'%s' object has no attribute '%s'" % (self.__class__.__name__, name))
AttributeError: 'DataFrame' object has no attribute 'fromDF'

2 个答案:

答案 0 :(得分:6)

fromDF是一个类函数。她是您将Dataframe转换为DynamicFrame

的方法
from awsglue.dynamicframe import DynamicFrame

DynamicFrame.fromDF(test_df, glueContext, "test_nest")

答案 1 :(得分:3)

仅仅是为了巩固Scala用户的答案,以下是将Spark Dataframe转换为DynamicFrame的方法(DynamicFrame的scala API中不存在fromDF方法):

import com.amazonaws.services.glue.DynamicFrame  
val dynamicFrame = DynamicFrame(df, glueContext)

希望对您有帮助!