我在S3中有一个文件,我们正在使用Glue将其导入到redshift中。 搜寻器部分已完成。
数据的一列是日期时间类型,但格式不正确,因此抓手无法识别并将其标记为字符串。
现在,我已经在redshift中创建了表,并将列数据类型标记为timestamp,现在在创建作业时需要在脚本中的何处以及需要更改什么,以便将字符串转换为redshift timestamp。
S3文件中的日期格式为'yyyy.mm.dd HH:mi:ss';
并且脚本在下面。
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
## @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)
## @type: DataSource
## @args: [database = "", table_name = "", transformation_ctx = "datasource0"]
## @return: datasource0
## @inputs: []
datasource0 = glueContext.create_dynamic_frame.from_catalog(database = "", table_name = "", transformation_ctx = "datasource0")
## @type: ApplyMapping
## @args: [mapping = [("mrp", "long", "mrp", "decimal(10,2)"), ("mop", "double", "mop", "decimal(10,2)"), ("mop_update_timestamp", "string", "mop_update_timestamp", "timestamp"), ("special_price", "long", "special_price", "decimal(10,2)"), ("promotion_identifier", "string", "promotion_identifier", "string"), ("is_percentage_promotion", "string", "is_percentage_promotion", "string"), ("promotion_value", "string", "promotion_value", "decimal(10,2)"), ("max_discount", "long", "max_discount", "decimal(10,2)"), ("promotion_start_date", "string", "promotion_start_date", "timestamp"), ("promotion_end_date", "string", "promotion_end_date", "timestamp")], transformation_ctx = "applymapping1"]
## @return: applymapping1
## @inputs: [frame = datasource0]
applymapping1 = ApplyMapping.apply(frame = datasource0, mappings = [ ("mrp", "long", "mrp", "decimal(10,2)"), ("mop", "double", "mop", "decimal(10,2)"), ("mop_update_timestamp", "string", "mop_update_timestamp", "timestamp"), ("special_price", "long", "special_price", "decimal(10,2)"), ("promotion_identifier", "string", "promotion_identifier", "string"), ("is_percentage_promotion", "string", "is_percentage_promotion", "string"), ("promotion_value", "string", "promotion_value", "decimal(10,2)"), ("max_discount", "long", "max_discount", "decimal(10,2)"), ("promotion_start_date", "string", "promotion_start_date", "timestamp"), ("promotion_end_date", "string", "promotion_end_date", "timestamp")], transformation_ctx = "applymapping1")
## @type: ResolveChoice
## @args: [choice = "make_cols", transformation_ctx = "resolvechoice2"]
## @return: resolvechoice2
## @inputs: [frame = applymapping1]
resolvechoice2 = ResolveChoice.apply(frame = applymapping1, choice = "make_cols", transformation_ctx = "resolvechoice2")
## @type: DropNullFields
## @args: [transformation_ctx = "dropnullfields3"]
## @return: dropnullfields3
## @inputs: [frame = resolvechoice2]
dropnullfields3 = DropNullFields.apply(frame = resolvechoice2, transformation_ctx = "dropnullfields3")
## @type: DataSink
## @args: [catalog_connection = "", connection_options = {"dbtable": "", "database": ""}, redshift_tmp_dir = TempDir, transformation_ctx = "datasink4"]
## @return: datasink4
## @inputs: [frame = dropnullfields3]
datasink4 = glueContext.write_dynamic_frame.from_jdbc_conf(frame = dropnullfields3, catalog_connection = "", connection_options = {"dbtable": "", "database": ""}, redshift_tmp_dir = args["TempDir"], transformation_ctx = "datasink4")
job.commit()
答案 0 :(得分:0)
您是否尝试过将其放入数据框,然后将其强制转换为时间戳,因为格式为'yyyy.mm.dd HH:mi:ss'?像这样:
@Throws(JSONException::class)
fun parseHitEvents(jsonObject: JSONObject): MutableList<AlgoliaEvent> {
val hits = jsonObject.optJSONArray("hits")
val results = ArrayList<AlgoliaEvent>(hits.length()) // Initialize the array list to be of size hits.length
hits?.forEach<JSONObject> { hit ->
// Check that each hit is not null
if (hit == null) return@forEach
// Parse each hit with a correspond ConciseEvent object
val event = jsonToObject<ConciseEvent>(hit).apply {
// We need to get the values from AbstractQuery.LatLng and add them into each event object
with(hit.getJSONObject("_geoloc")) {
_geoloc.lat = getDouble("lat")
_geoloc.lng = getDouble("lng")
}
// Add the main event in the list of events, so that we can send them as the return value
results.add(AlgoliaEvent(HitEvent(event)))
}
return results
}
让我知道它是否对您有用