这是我所拥有的大型JSON文件的一部分。我正在创建包含以下内容的文件夹
yamlPath
然后将内容从
sqlQuery
. 但在重复“yamlname+yamlpath”的情况下,我的代码只创建具有第一个结果的文件,并插入
SQL查询
它从for循环中获取的内容。所以,在这个重复的场景中,我只想
SQL查询
并根据最新的/max创建文件
jobEndTimestamp
.
如何使代码只提取最新的
作业结束时间戳
然后创建文件?
预期输出:
/app/computer/users/ship-notice-data.sql -> select from table.b
/app/computer/a/users/boat-notice-data.sql -> select from table.b
{
"stream": [
{
"applicationServiceId": "uhhj",
"yamlName": "/users/ship-notice-data.yml",
"yamlPath": "/app/computer",
"jobStartTimestamp": "2018-09-15 04:12:46",
"jobEndTimestamp": "2018-09-15 04:15:29",
"sourceHostName": "Teradata",
"sourceType": "Teradata",
"targetHostName": "DB2",
"targetType": "DB2",
"sqlQuery": "select from table.a"
},{
"applicationServiceId": "uhhj",
"yamlName": "/users/ship-notice-data.yml",
"yamlPath": "/app/computer",
"jobStartTimestamp": "2018-09-15 21:12:46",
"jobEndTimestamp": "2018-09-15 21:15:29",
"sourceHostName": "Teradata",
"sourceType": "Teradata",
"targetHostName": "DB2",
"targetType": "DB2",
"sqlQuery": "select from table.b"
},{
"applicationServiceId": "uhhj",
"yamlName": "/users/car-notice-data.yaml",
"yamlPath": "/app/computer/s",
"jobStartTimestamp": "2018-09-15 04:12:46",
"jobEndTimestamp": "2018-09-15 06:15:29",
"sourceHostName": "Teradata",
"sourceType": "Teradata",
"targetHostName": "DB2",
"targetType": "DB2",
"sqlQuery": "select from table.b"
},{
"applicationServiceId": "uhhj",
"yamlName": "/users/boat-notice-data.yaml",
"yamlPath": "/app/computer/a",
"jobStartTimestamp": "2018-09-15 04:12:46",
"jobEndTimestamp": "2018-09-15 06:15:29",
"sourceHostName": "Teradata",
"sourceType": "Teradata",
"targetHostName": "DB2",
"targetType": "DB2",
"sqlQuery": "select from table.b"
}
]
}
这是我的代码:
with open('/Users/mona/stream.json', 'r') as f:
item_dict = json.load(f)
for item in item_dict['stream']:
if (item['applicationServiceId'] == 'uhhj' and
item.get('targetHostName') == 'DB2' and
(item['targetType'] == 'DB2')):
# print(item.get('applicationServiceId'))
v3 = item.get('applicationServiceId')
v4 = item.get('jobEndTimestamp')
v = item.get('sqlQuery')
v1 = item.get('yamlName')
v2 = item.get('yamlPath')
print(v1+v2+" "+v4+ " " +str(v))
# v4 = str(item.get('yamlName').split('/')[-1].split('.')[0])
# print(v4)
originalPath = "/Users/mona/"
fullPath = os.path.join(originalPath+v2+(v1.split('/')[1].split('/')[0])+'/'+(v1.split('/')[2].split('/')[0])+'/')
# print(fullPath)
os.makedirs(fullPath, mode=0o777, exist_ok=True)
# print(v1)
with open(fullPath + str(item.get('yamlName').split('/')[-1].split('.')[0]) + ".sql", "w") as newFile:
newFile.write("%s \n" % (v))