将JSON文件转换为postgresql,避免重复

时间:2016-08-01 14:58:43

标签: sql json postgresql

我正在使用Postgresql中的一组脚本将我的JSON文件放入表中。

我知道Postgresql可以解析JSON,但我想做统计,在不同类型的对象之间加入,......

感谢几个github,我编写了一个SQL函数,可以在对象中提供所有属性。它是递归的,它处理表:

ObjectID | AttributeName     | AttributeValue
---------|-------------------|------------
1        | SomeName          | SomeValue
2        | SomeName(same)    | AnotherValue

然后我有一张表:

 {"param":[
              {"name":"parm1","value":"CSR"},
              {"name":"parm2","value":"C"},
              {"name":"parm3","value":"IFRS"}]} 

这就是我想要的。我的问题是:当我有数组时如何避免重复? 例如,如果我的JSON是:

id; AttributeName            | value
1|"job__params__param__name" |""parm3""
1|"job__params__param__name" |""parm2""
1|"job__params__param__name" |""parm1""
1|"job__params__param__value"|""IFRS""
1|"job__params__param__value"|""CSR""
1|"job__params__param__value"|""C""

我的功能会给我:

id; AttributeName            | value
1|"job__params__param3__name" |""parm3""
1|"job__params__param2__name" |""parm2""
1|"job__params__param1__name" |""parm1""
1|"job__params__param3__value"|""IFRS""
1|"job__params__param2__value"|""CSR""
1|"job__params__param1__value"|""C""

我会有像

这样的东西
OPEN jobCursor FOR (SELECT id FROM jobsJSON); -- 
    LOOP
        FETCH jobCursor INTO record_job;
        EXIT WHEN NOT FOUND;
        PERFORM listAttribut1Job(record_job.jobID);
    END LOOP;

所以我尝试了'IF属性名称IN(SELECT属性来自......)之类的东西,但它不起作用。

我在这个上下文中使用上面的函数:

{{1}}

(我的对象叫做工作)。当然,我可以修改这个背景。

你会知道做某事的方法吗? 'IF属性ALREADY IN attributeTable THEN属性:= attribute1'或attribute2如果使用了attribute1 si,或者......?

非常感谢

PS:如果你想要完整的代码我可以给它,我只是希望帖子易于阅读。

1 个答案:

答案 0 :(得分:0)

确定,

我尝试更新客户端应用程序,但我对PHP太蹩脚,所以我创建了一个大脚本来避免重复。如果您知道如何改进它,请随意升级并发表评论:

CREATE OR REPLACE FUNCTION avoidDuplicateJobs(jobID integer)
RETURNS void AS
$BODY$
DECLARE
queryString varchar;
queryStringGroup varchar;
newAN varchar;
dupsCursor REFCURSOR;
dupsNameCursor REFCURSOR;
dupsR RECORD;
NameR RECORD;
IDR RECORD;
indice integer;
BEGIN
    queryString :=('SELECT attribID, jobID, attributeName,value from jobsAttributeFullList jAFL '
    ||'where (select count(*) from jobsAttributeFullList jAFL2 '
    ||'where jobid='||jobID||' AND '
    ||'jAFL.attributeName = jAFL2.attributename) >1 '
    ||' AND jobid='||jobID
    ||' ORDER BY attributename, attribid  ');
    DROP TABLE IF EXISTS dups;
    CREATE TEMP TABLE dups(attribID integer, jobID integer , attributeName varchar(100) NOT NULL, value varchar); 
    --RAISE INFO 'Query : %',queryString;
--  INSERT INTO dups (attribID, jobID, attributeName,value) 
    OPEN dupsCURSOR FOR EXECUTE (queryString);
    --EXECUTE queryString INTO dupsCursor;
    --OPEN dupsNameCursor FOR(SELECT attributeName FROM dupsCursor GROUP BY attributeName);
    LOOP 
        FETCH dupsCursor INTO dupsR;
        EXIT WHEN NOT FOUND;
        --RAISE INFO 'dups = %' ,dupsR;
        INSERT  INTO dups (attribID, jobID, attributeName,value) VALUES (dupsR.attribID,dupsR.jobID, dupsR.attributeName, dupsR.value) ;
    END LOOP;
    CLOSE dupsCURSOR;
    OPEN dupsNameCursor FOR (SELECT DISTINCT attributeName FROM dups);
    LOOP
        FETCH dupsNameCursor INTO NameR;
        EXIT WHEN NOT FOUND;
        RAISE INFO 'attributeName = %' ,NameR;
        indice:=0;
        queryStringGroup:='SELECT attribID FROM dups where attributename ='||$$'$$||NameR.attributeName||$$'$$||' ORDER BY attribid;';
        OPEN dupsCURSOR FOR EXECUTE queryStringGroup;
        LOOP
            FETCH dupsCursor INTO IDR;
            EXIT WHEN NOT FOUND;
            RAISE INFO 'attribID =%',IDR;
            indice:=indice +1;
            queryString:= ('SELECT attributeName FROM dups WHERE attribID='||IDR.attribID);
            EXECUTE queryString INTO newAN;
            newAN:=newAN||indice;
            RAISE INFO 'newAN = %', newAN;
            queryString:= $$UPDATE jobsattributefulllist SET attributeName='$$||newAN||$$' WHERE attribID=$$||IDR.attribID||' ;';
            RAISE INFO 'Update Query = %',queryString;
            EXECUTE queryString;
        END LOOP;
        CLOSE dupsCURSOR;

    END LOOP;
    CLOSE dupsNameCursor;


END;
$BODY$ language plpgsql;