访客路径分析-Druid实践

一、背景

这里写图片描述

访客分析是常见数据分析的一种,通过如上图(Google Analytics)以比较直观的方式展现用户达到网站后各条访问路径的流失情况,帮助网站优化减少流失率。

访客路径分析有如下几个关键点:

  • 用户访问的路径通常有多级,默认展开包含着陆页在内的5级路径,支持往后每点击一次展开一级路径(最高支持到10级,再往后意义不大)。
  • 每级只展示top 5访问数的网页,每级路径网页之间连接线表示跳转情况。
  • 指标包含top 5网页的会话数、流失数和剩余网页的会话数。

通过上述分析,要实现访客路径分析需要完成如下几项工作:

  1. 计算每一级所有网页的会话总数。
  2. 计算每一级会话数top 5的网页。
  3. 计算每一级两两网页之间的跳转访问数。

本文提出一种基于druid的实现方案,将上述3个查询转化为druid中的Timeseries(求总数)、TopN(求前5)、GroupBy(求两两关联)查询。

二、技术方案

数据清洗(ETL)
将用户pv流水根据,聚合成一个session会话。session会话内用户的访问流水按时间排序,取前11个分别放于维度landing_page ~ path10,ETL处理后的数据表格示例如下:

hostlanding_pagepath1path2path10
www.xxx.com/index.html/a/b/e
www.xxx.com/product.html/c/dnull

数据入Druid供查询,schema设计如下

{
  "type" : "index_hadoop",
  "spec" : {
    "ioConfig" : {
      "type" : "hadoop",
      "inputSpec" : {
        "type" : "static",
        "paths" : ""
      }
    },
    "dataSchema" : {
      "dataSource" : "",
      "granularitySpec" : {
        "type" : "uniform",
        "segmentGranularity" : {"type":"period","period":"P1D","timeZone":"Asia/Shanghai"},
        "queryGranularity" : {"type":"period","period":"P1D","timeZone":"Asia/Shanghai"},
        "intervals" : []
      },
      "parser" : {
        "type" : "string",
        "parseSpec" : {
          "format" : "json",
          "dimensionsSpec" : { "dimensions": [ "host", "landing_page", "path1", ... "path10" ] },
          "timestampSpec" : { "format" : "auto", "column" : "time" } }
      },
      "metricsSpec": [
        {
          "name": "count",
          "type": "count"
        }
      ]
    },
    "tuningConfig" : {
      "type" : "hadoop",
      "partitionsSpec" : {
        "type" : "hashed",
        "targetPartitionSize" : 5000000
      },
      "indexSpec" : {
        "bitmap" : { "type" : "roaring"},
        "dimensionCompression":"LZ4",
        "metricCompression" : "LZ4",
        "longEncoding" : "auto"
      }
    }
  }
}

三、具体实践

查询语句示例
计算每一级所有网页的会话总数(默认展示前5级),过滤掉为null的情况(用户只访问到上一级就跳出)。
{
  "queryType": "timeseries",
  "dataSource": "visit_path_analysis",
  "granularity": "all",
  "filter": {
    "type": "and",
    "fields": [{"type": "selector", "dimension": "host", "value": "www.xxx.com"}]
  },
  "aggregations": [
    { 
      "type": "filtered",
      "filter": { 
        "type": "not", 
        "field": { "type": "selector", "dimension": "landing_page", "value": null }
      },
      "aggregator": { "type": "longSum", "name": "count0", "fieldName": "count" }
    },
    { 
      "type": "filtered",
      "filter": {
        "type": "not", 
        "field": { "type": "selector", "dimension": "path1", "value": null }
      },
      "aggregator": { "type": "longSum", "name": "count1", "fieldName": "count" }
    },
    {
      "type": "filtered",
      "filter": {
        "type": "not", 
        "field": { "type": "selector", "dimension": "path2", "value": null }
      },
      "aggregator": { "type": "longSum", "name": "count2", "fieldName": "count" }
    },
    { 
      "type": "filtered",
      "filter": { 
        "type": "not", 
        "field": { "type": "selector", "dimension": "path3", "value": null }
      },
      "aggregator": { "type": "longSum", "name": "count3", "fieldName": "count" }
    },
    { 
      "type": "filtered",
      "filter": { 
        "type": "not", 
        "field": { "type": "selector", "dimension": "path4", "value": null }
      },
      "aggregator": { "type": "longSum", "name": "count4", "fieldName": "count" }
    }
  ],
  "intervals": []
}
计算每一级会话数top5的网页,过滤掉为null的情况(用户只访问到上一级就跳出)。
{
  "queryType": "topN",
  "dataSource": "visit_path_analysis",
  "granularity": "all",
  "dimension": "landing_page",
  "filter": {
    "type": "and",
    "fields": [
      {"type": "selector", "dimension": "host", "value": "www.xxx.com"},
      {
        "type": "not", 
        "field": { "type": "selector", "dimension": "landing_page", "value": null }
      }
    ]
  },
  "threshold": 5,
  "metric": {
    "type": "numeric",
    "metric": "count"
  },
  "aggregations": [{ "type": "longSum", "name": "count", "fieldName": "count" }],
  "intervals": []
}
计算每一级两两网页之间的跳转访问数,后一级的null用来计算流水数。
{
  "queryType": "groupBy",
  "dataSource": "visit_path_analysis",
  "granularity": "all",
  "dimensions": ["landing_page", "path1"],
  "filter": {
    "type": "and",
    "fields": [
      {"type": "selector", "dimension": "host", "value": "www.xxx.com"},
      {
        "type": "in",
        "dimension": "landing_page",
        "values": ["/a", "/b", "/c", "/d", "e"]
      },
      {
        "type": "in",
        "dimension": "path1",
        "values": ["/f", "/g", "/h", "/i", "/j", null]
      }
    ]
  },
  "aggregations": [{ "type": "longSum", "name": "count", "fieldName": "count" }],
  "intervals": []
}

四、总结分析

本文提出基于Druid来做访客路径分析的方案需由多个请求来完成。

  • 计算每一级所有网页的会话总数和计算每一级会话数top5的网页,在默认展示的时候可以先并行向druid发起请求。获取每级总会话数后再减去top5的会话数就是剩余其他网页的会话数。

  • 当得到每一级top5的路径后,只需要相邻两级路径做GroupBy查询即可获得转化数与流水数。

  • 当需要展示往后一级路径流转时,只需要基于当前最后一级的top5与下一级别top5做GroupBy计算即可。

  • 从数据分布来看,大部分流水集中在前几步,往后有数据级的差距。

  • 该方案最大挑战来着对Druid的并发请求,一个页面展示会扩大为多个Druid并发语句请求。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Spring Boot是一个用于简化Spring应用开发的框架,而Druid是一个高效的数据库连接池。在Spring Boot项目中使用Druid连接池可以提供高性能的数据库连接管理和监控功能。 要在Spring Boot中使用Druid连接池,需要以下步骤: 1. 在项目的pom.xml文件中添加Druid依赖: ```xml <dependencies> <!-- Spring Boot Starter JDBC --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- Druid依赖 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.6</version> </dependency> </dependencies> ``` 2. 在application.properties或application.yml文件中配置Druid连接池相关属性,例如: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver # Druid连接池配置 spring.datasource.druid.initial-size=5 spring.datasource.druid.min-idle=5 spring.datasource.druid.max-active=20 spring.datasource.druid.max-wait=60000 spring.datasource.druid.time-between-eviction-runs-millis=60000 spring.datasource.druid.min-evictable-idle-time-millis=300000 spring.datasource.druid.validation-query=SELECT 1 spring.datasource.druid.test-while-idle=true spring.datasource.druid.test-on-borrow=false spring.datasource.druid.test-on-return=false spring.datasource.druid.filters=stat spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20 spring.datasource.druid.use-global-data-source-stat=true ``` 3. 在启动类上添加`@EnableTransactionManagement`和`@MapperScan`注解,例如: ```java @SpringBootApplication @EnableTransactionManagement @MapperScan("com.example.mapper") public class MyApp { public static void main(String[] args) { SpringApplication.run(MyApp.class, args); } } ``` 以上是在Spring Boot项目中使用Druid连接池的基本配置步骤,你可以根据自己的需求修改配置参数来满足具体业务场景。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值