博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
postgresql plpythonu例子
阅读量:6029 次
发布时间:2019-06-20

本文共 5349 字,大约阅读时间需要 17 分钟。

以下代码仅作为参考之用

select md5, crc32, record->'UserModerAnalysis'->'base_info'->'file_malware' as file_malware

from reports
CREATE OR REPLACE FUNCTION py_get_file_malware(record TEXT)
    RETURNS TEXT
AS $$
    # pl/python functioin body
    import json
    plpy.notice('type of record is', type(record))
    # plpy.notice('import json')
    # plpy.notice('begin to loads()')
    #if 'json' in SD:
    #    json = SD['json']
    #else:
    #    import json
    #    SD['json'] = json
    obj = json.loads(record)
    plpy.notice('UserModerAnalysis = %s'%(str(obj['UserModerAnalysis'])))
    try:
    file_malware = obj['UserModerAnalysis']['base_info']['file_malware']
    except Exception, e:
    #plpy.error(record)
    plpy.notice('ERROR!')
    file_malware = ''
    return file_malware
$$ LANGUAGE plpythonu
select md5, crc32, py_get_file_malware(record::TEXT)
from reports
limit 2
-- create table summary
CREATE TABLE summary_file_malware
(
  description character varying(10) NOT NULL,
  count integer,
  CONSTRAINT summary_file_malware_pkey PRIMARY KEY (description)
)
DROP FUNCTION calculate_file_malware()
CREATE OR REPLACE FUNCTION calculate_file_malware()
    RETURNS trigger AS $$
    plpy.notice('calculate_file_malware invoked')
    import json
    event = TD['event']
    
    if event == 'INSERT':
        plpy.notice('insert triggered')
    elif event == 'UPDATE':
        plpy.notice('update triggered')
        # parse parameter
    old_obj = json.loads(TD['old']['record'])
    new_obj = json.loads(TD['new']['record'])
    plpy.notice('old = %s, new = %s'%(old_obj['UserModerAnalysis']['base_info']['file_malware'],
        new_obj['UserModerAnalysis']['base_info']['file_malware']))
        
        # sub old
        try:
        plpy.notice('begin')
            plan = plpy.prepare('SELECT * FROM summary_file_malware WHERE description = $1', ['text'])
        old_value = old_obj['UserModerAnalysis']['base_info']['file_malware']
        plpy.notice("old_value = " + old_value)
        rv = plpy.execute(plan, [old_value], 1)
        old_count = int(rv[0]['count'])
        plpy.notice('old_count = %s'%(old_count))
        plan = plpy.prepare('UPDATE summary_file_malware SET count = $1 WHERE description = $2', ['int', 'text'])
        plpy.execute(plan, [old_count - 1, old_value])
    except Exception, e:
        plpy.notice('exception occured, exception msg = '+str(e))
    # add new
        try:
            plan = plpy.prepare('SELECT * FROM summary_file_malware WHERE description = $1', ['text'])
        old_value = new_obj['UserModerAnalysis']['base_info']['file_malware']
        rv = plpy.execute(plan, [old_value], 1)
        old_count = int(rv[0]['count'])
        plpy.notice('old_count = %s'%(old_count))
        plan = plpy.prepare('UPDATE summary_file_malware SET count = $1 WHERE description = $2', ['int', 'text'])
        plpy.execute(plan, [old_count + 1, old_value])
    except Exception, e:
        plpy.notice('exception occured, exception msg = '+str(e))
    
    elif event == 'DELETE':
        plpy.notice('delete triggered')
    elif event == 'TRUNCATE':
        plpy.notice('trancate triggered')
    else:
        plpy.notice('unknow event, event = ', event)
$$ LANGUAGE plpythonu
DROP TRIGGER IF EXISTS calculate on reports;
CREATE TRIGGER  calculate AFTER UPDATE OF record
    ON reports
    FOR EACH ROW
    EXECUTE PROCEDURE calculate_file_malware ();
SELECT * FROM summary_file_malware WHERE description ='OK'
INSERT INTO summary_file_malware VALUES('OK', 0)
UPDATE reports SET record = '{"Name": "000BD3A69E56CD5E8D998FEDA8EF3CA6.CCD2FFE1", "UserModerAnalysis": {"base_info": {"file_malware": "YES"}, "file_monitor": [], "virusname": null, "danger_behavior": [], "relation": {"processtree": [{"processid": "608", "process": "000BD3A69E56CD5E8D998FEDA8EF3CA6.CCD2FFE1", "module": "", "parentid": 0, "relationtype": "Root", "id": 1}]}, "other_behavior": [], "network_monitor": [], "process_monitor": [], "reg_monitor": []}, "KernelModelAnalysis": {"MaliciousActives": {"000BD3A69E56CD5E8D998FEDA8EF3CA6.CCD2FFE1": {"MemoryOperations": {}, "FileOperations": {"CREATE_FILE.DROP_PE_TO_SYSTEM_DIR": [{"COMMENT": "Create_File_In_SystemDirectory", "DETAILS": {"file_path": "c:\\windows\\.exe"}, "LEVEL": "LEVEL_3"}]}, "NetworkOperations": {}, "ProcessOperations": {}, "RegistryOperations": {}, "OtherOperations": {}}}, "ProcessFamily": {"000BD3A69E56CD5E8D998FEDA8EF3CA6.CCD2FFE1": {"Parent_Process": "", "Command_Line": "", "Type_Created": "Root"}}, "ProcessActives": {"000BD3A69E56CD5E8D998FEDA8EF3CA6.CCD2FFE1": {"MemoryOperations": {}, "FileOperations": {"DELETE_FILE": [{"COMMENT": "Delete_File_Found", "DETAILS": {"file_path": "C:\\DOCUME~1\\autoer\\LOCALS~1\\Temp\\~DFCCF6.tmp"}, "LEVEL": "LEVEL_2"}], "CREATE_FILE": [{"COMMENT": "Create_File_Found", "DETAILS": {"file_path": "C:\\DOCUME~1\\autoer\\LOCALS~1\\Temp\\~DFCCF6.tmp"}, "LEVEL": "LEVEL_2"}]}, "NetworkOperations": {}, "ProcessOperations": {}, "RegistryOperations": {"SET_KEY_VALUE": [{"COMMENT": "Set_Key_Value_Found", "DETAILS": {"value": "Drive", "type": "REG_SZ", "name": "BaseClass", "key": "HKEY_USERS\\S-1-5-21-1708537768-287218729-1177238915-1003\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\MountPoints2\\{7fb46850-baea-11e1-9890-806d6172696f}"}, "LEVEL": "LEVEL_2"}]}, "OtherOperations": {}}}, "TimeOfReportCreated": "2013-06-03 11:25:25:724 +0800", "Summary": ["CREATE_FILE", "CREATE_FILE.DROP_PE_TO_SYSTEM_DIR", "DELETE_FILE", "SET_KEY_VALUE"], "FileName": "000BD3A69E56CD5E8D998FEDA8EF3CA6.CCD2FFE1"}, "Result": "Success", "Time": "2013-06-03 11:25:25:724 +0800", "DESCRIPTION": "\u64cd\u4f5c\u6210\u529f\u5b8c\u6210\u3002"}' WHERE md5 = '000BD3A69E56CD5E8D998FEDA8EF3CA6' and crc32 = 'CCD2FFE1'
select * from summary_file_malware

转载地址:http://olbhx.baihongyu.com/

你可能感兴趣的文章
【原创】 PostgreSQL 实现MySQL 的auto_increment 字段
查看>>
php闭包语法学习
查看>>
vs2015添加vc助手
查看>>
检测点1.1
查看>>
android--------阿里 AndFix 热修复
查看>>
java springcloud版b2b2c社交电商spring cloud分布式微服务 (七)高可用的分布式配置中心(Spring Cloud Config)...
查看>>
Oozie与Coordinator调度讲解及系统时区配置与定时触发两种配置方式
查看>>
RGB_YUV_YCbCr
查看>>
tesseract 安装及使用
查看>>
优化SqlServer--数据压缩
查看>>
SharePoint 自定义WebPart之间的连接
查看>>
231. Power of Two
查看>>
control.add()
查看>>
p点到(a,b)点两所在直线的垂点坐标及p点是否在(a,b)两点所在直线上
查看>>
GridView强制换行与自动换行
查看>>
51Nod 1003 阶乘后面0的数量(数学,思维题)
查看>>
Sublime text3中配置Github
查看>>
Getting Started with iOS Development Part10:Customizing your Mobile target's Splash screen
查看>>
asp.net C# MVC 提交表单后清空表单
查看>>
备份软件 FreeFileSync 怎么用
查看>>