Vertica:将表中的数据插入flextable

时间:2019-06-20 14:24:35

标签: etl vertica flextable

我在EON模式下使用Vertica 9.2.1。我有一个事实表,其中的列包含JSON字符串。我想将此数据与事实表中的一些标识符一起加载到弹性表中。这样我们就可以对该数据进行分析。我要避免的是,因为所有数据已经​​在Vertica中可用,所以将所有必需的数据加载到ETL机器上以转换数据,然后将其加载到flextable中。如何告诉Vertica将VARCHAR列解析为JSON?

CREATE TABLE public.tmp_facts ("id" INTEGER, "user_id" VARCHAR(64), "event_type" VARCHAR(50), /* other columns omitted */ "additional" VARCHAR(65000));
INSERT INTO public.tmp_facts ("id", "user_id", "event_type", "additional")
SELECT 1, 'user1', 'event1', '{"os":"Android", "time":"'||NOW()||'"}';
CREATE FLEX TABLE public.fact_additional
(
    "id" INTEGER NOT NULL,
    "user_id" VARCHAR(64) NOT NULL,
    "event_type" VARCHAR(50)
);
INSERT INTO public.fact_additional ("id", "user_id", "event_type")
SELECT "id", "user_id", "event_type", "additional" FROM tmp_facts;

SELECT "additional", "additional.os", "additional[os]" FROM fact_additional;

我希望最后一个查询输出至少一列Android

1 个答案:

答案 0 :(得分:2)

additional插入MapJSONExtractor()时,您需要使public.tmp_facts列通过public.fact_additional函数

INSERT INTO public.fact_additional ("id", "user_id", "event_type")
SELECT "id",
       "user_id",
       "event_type", 
       MapJSONExtractor("additional") as additional 
FROM tmp_facts;

SELECT "additional"['os'] as os FROM fact_additional;

   os    
---------
 Android
(1 row)

请注意在适当位置使用单引号/双引号。