Helping the Industrial Internet of Things and other dimensions of industrial big data: organization [16]

01: Other Dimensions: Organization

  • Goal : To realize the design and construction of organizational dimensions

  • path

    • step1: Requirements
    • step2: design
    • step3: Realize
  • implement

    • Requirements : Realize the construction of the organization dimension table, and obtain the organization information corresponding to each engineer

      image-20211003103949211

      • Count the number of work orders and write-offs of different service personnel
    • design

      • org_employee: employee information table [employee id, employee code, employee name, user system id]

        select empid,empcode,empname,userid from org_employee;
        
      • org_empposition: employee position information table [employee id, position id]

        select empid,positionid from org_empposition;
        
      • org_position: position information table [position id, position code, position name, department id]

        select positionid,posicode,posiname,orgid from org_position;
        
      • org_organization: department information table [department id, department code, department name]

        select orgid,orgcode,orgname from org_organization;
        

      image-20211010153834323

    • accomplish

      • Create dimension table

        -- 创建组织机构维度表,组织机构人员是经常变动的,所以按照日期分区
        create external table if not exists one_make_dws.dim_emporg(
            empid string comment '人员id'   
            , empcode string comment '人员编码(erp对应的账号id)'
            , empname string comment '人员姓名'
            , userid string comment '用户系统id(登录用户名)'
            , posid string comment '岗位id'
            , posicode string comment '岗位编码'
            , posiname string comment '岗位名称'
            , orgid string comment '部门id'
            , orgcode string comment '部门编码'
            , orgname string comment '部门名称'
        ) comment '组织机构维度表'
        partitioned by (dt string)
        stored as orc
        location '/data/dw/dws/one_make/dim_emporg';
        
      • extract data

        -- 先根据dwd层的表进行关联,然后分别把数据取出来
        insert overwrite table one_make_dws.dim_emporg partition(dt='20210101')
        select
            emp.empid as empid
            , emp.empcode as empcode
            , emp.empname as empname
            , emp.userid as userid
            , pos.positionid as posid
            , pos.posicode as posicode
            , pos.posiname as posiname
            , org.orgid as orgid
            , org.orgcode as orgcode
            , org.orgname as orgname
        from  one_make_dwd.org_employee emp
        left join one_make_dwd.org_empposition emppos
            on emp.empid = emppos.empid and emp.dt = '20210101' and emppos.dt = '20210101'
        left join one_make_dwd.org_position pos
            on emppos.positionid = pos.positionid and pos.dt = '20210101'
        left join one_make_dwd.org_organization org
            on pos.orgid = org.orgid and org.dt = '20210101';
        
  • summary

    • Realize the design and construction of organizational dimensions

02: Other dimensions: warehouse, logistics

  • Goal : Realize the construction of warehouse dimension and logistics dimension

  • path

    • step1: warehouse dimension
    • step2: logistics dimension
  • implement

    • warehouse dimension

      • build table

        -- 仓库维度表
        create external table if not exists one_make_dws.dim_warehouse(
            code string comment '仓库编码'
            , name string comment '仓库名称'
            , company_id string comment '所属公司'
            , company string comment '公司名称'
            , srv_station_id string comment '所属服务网点ID'
            , srv_station_name string comment '所属服务网点名称'
        )comment '仓库维度表'
        partitioned by (dt string)
        stored as orc
        location '/data/dw/dws/one_make/dim_warehouse';
        
      • load

        insert overwrite table one_make_dws.dim_warehouse partition(dt='20210101')
        select
            warehouse.code as code
            , warehouse.name as name
            , warehouse.company as company_id
            , cmp.compmay as compmay
            , station.id as srv_station_id
            , station.name as srv_station_name
        from
            one_make_dwd.ciss_base_warehouse warehouse
        -- 关联公司信息表
        left join (
             select
                   ygcode as company_id, max(companyname) as compmay
             from one_make_dwd.ciss_base_baseinfo where dt='20210101'
             -- 需要对company信息进行分组去重,里面有一些重复数据 
             group by ygcode) cmp
             on warehouse.dt = '20210101' and cmp.company_id = warehouse.company
        -- 关联服务网点和仓库关系表
        left join one_make_dwd.ciss_r_serstation_warehouse station_r_warehouse
             on station_r_warehouse.dt = '20210101' and station_r_warehouse.warehouse_code = warehouse.code
        -- 关联服务网点表 
        left join one_make_dwd.ciss_base_servicestation station
             on station.dt = '20210101' and station.id = station_r_warehouse.service_station_id;
        
    • Logistics dimension

      • build table

        -- 物流维度表(和服务属性表类似)
        create external table if not exists one_make_dws.dim_logistics(
            prop_name string comment '字典名称'
            , type_id string comment '属性id'
            , type_name string comment '属性名称'
        )comment '物流维度表'
        partitioned by (dt string)
        stored as orc
        location '/data/dw/dws/one_make/dim_logistics';
        
      • load

        insert overwrite table one_make_dws.dim_logistics partition(dt = '20210101')
        select
            dict_t.dicttypename as prop_name
            , dict_e.dictid as type_id
            , dict_e.dictname as type_name
        from  one_make_dwd.eos_dict_type dict_t
        inner join one_make_dwd.eos_dict_entry dict_e
            on dict_t.dt = '20210101'
                and dict_e.dt = '20210101'
                and dict_t.dicttypeid = dict_e.dicttypeid
                and dict_t.dicttypename in (
                    '物流公司'
                    , '物流类型'
                )
        order by dict_t.dicttypename, dict_e.dictid;
        
      • It would be better to use the following

        insert overwrite table one_make_dws.dim_logistics partition (dt = '20210101')
        select dict_t.dicttypename as prop_name
             , dict_e.dictid       as type_id
             , dict_e.dictname     as type_name
        from one_make_dwd.eos_dict_type dict_t
                inner join one_make_dwd.eos_dict_entry dict_e on dict_t.dt = '20210101'
                and dict_e.dt = '20210101'
                and dict_t.dicttypeid = dict_e.dicttypeid   -- 通过状态字符串进行关联
                and dict_t.dicttypename in ('物流公司', '物流类型') -- 通过和物流相关的字样进行过滤
        order by prop_name, type_id;
        
  • summary

    • Realize the construction of warehouse dimension and logistics dimension

Appendix 1: Frequently Asked Questions

1. Error: Cross Join is not enabled

Exception in thread "main" org.apache.spark.sql.AnalysisException: Detected implicit cartesian product for INNER join between logical plans.Use the CROSS JOIN syntax to allow cartesian products between these relations
  • Spark2.x does not allow Cartesian product by default, unless the cross join is explicitly declared or the attribute is enabled:spark.sql.crossJoin.enabled true

2.错误:Unable to move source

Error: org.apache.spark.sql.AnalysisException: org.apache.hadoop.hive.ql.metadata.HiveException: Unable to move source hdfs://hadoop.bigdata.cn:9000/data/dw/dws/one_make/dim_warehouse/.hive-staging_hive_2020-12-23_04-26-01_363_5663538019799519260-16/-ext-10000/part-00000-63069107-6405-4e31-a55a-6bdeefcd7d9b-c000 to destination hdfs://hadoop.bigdata.cn:9000/data/dw/dws/one_make/dim_warehouse/dt=20210101/part-00000-63069107-6405-4e31-a55a-6bdeefcd7d9b-c000; (state=,code=0)
  • Restart SparkSQL's ThriftServer and establish a new session connection with MetaStore

Guess you like

Origin blog.csdn.net/xianyu120/article/details/131939047