Skip to content

社交网络分析

数据模型设计

1. 核心实体

  • 用户(User):社交网络的用户
  • 帖子(Post):用户发布的内容
  • 评论(Comment):用户对帖子的评论
  • 点赞(Like):用户对帖子或评论的点赞
  • 关注(Follow):用户之间的关注关系
  • 好友(Friend):用户之间的好友关系

2. 关系类型

  • 发布(POSTED):用户发布帖子
  • 评论(COMMENTED_ON):用户评论帖子
  • 点赞(LIKED):用户点赞帖子或评论
  • 关注(FOLLOWS):用户关注其他用户
  • 好友(FRIENDS_WITH):用户之间的好友关系

3. 属性设计

  • User 节点属性

    • id: 唯一标识符
    • username: 用户名
    • name: 真实姓名
    • email: 邮箱
    • age: 年龄
    • location: 位置
    • join_date: 加入日期
  • Post 节点属性

    • id: 唯一标识符
    • content: 内容
    • created_at: 创建时间
    • likes_count: 点赞数
    • comments_count: 评论数
  • Comment 节点属性

    • id: 唯一标识符
    • content: 内容
    • created_at: 创建时间
    • likes_count: 点赞数
  • 关系属性

    • created_at: 创建时间
    • strength: 关系强度(用于好友关系)

4. 数据模型示例

cypher
// 用户节点
(:User {id: 1, username: 'john', name: 'John Doe', email: 'john@example.com', age: 30, location: 'New York', join_date: '2023-01-01'})

// 帖子节点
(:Post {id: 1, content: 'Hello World!', created_at: '2023-01-02', likes_count: 10, comments_count: 5})

// 评论节点
(:Comment {id: 1, content: 'Great post!', created_at: '2023-01-02', likes_count: 3})

// 关系
(:User {id: 1})-[:POSTED {created_at: '2023-01-02'}]->(:Post {id: 1})
(:User {id: 2})-[:COMMENTED_ON {created_at: '2023-01-02'}]->(:Post {id: 1})
(:User {id: 3})-[:LIKED {created_at: '2023-01-02'}]->(:Post {id: 1})
(:User {id: 1})-[:FOLLOWS {created_at: '2023-01-01'}]->(:User {id: 2})
(:User {id: 1})-[:FRIENDS_WITH {created_at: '2023-01-01', strength: 5}]->(:User {id: 2})

好友推荐

1. 基于共同好友的推荐

cypher
// 查找用户的好友的好友,且不是当前用户的好友
MATCH (u:User {id: 1})-[:FRIENDS_WITH]->(friend:User)-[:FRIENDS_WITH]->(potential:User)
WHERE NOT (u)-[:FRIENDS_WITH]->(potential) AND u <> potential
RETURN potential.username, count(friend) AS common_friends
ORDER BY common_friends DESC
LIMIT 10

2. 基于兴趣的推荐

cypher
// 查找与用户点赞相同帖子的其他用户
MATCH (u:User {id: 1})-[:LIKED]->(p:Post)<-[:LIKED]-(other:User)
WHERE u <> other
RETURN other.username, count(p) AS common_likes
ORDER BY common_likes DESC
LIMIT 10

3. 基于位置的推荐

cypher
// 查找与用户在同一位置的其他用户
MATCH (u:User {id: 1, location: 'New York'})-[:FRIENDS_WITH]->(friend:User)
MATCH (friend)-[:FRIENDS_WITH]->(potential:User {location: 'New York'})
WHERE NOT (u)-[:FRIENDS_WITH]->(potential) AND u <> potential
RETURN potential.username, count(friend) AS common_friends
ORDER BY common_friends DESC
LIMIT 10

社区发现

1. 使用标签传播算法

cypher
// 创建图
CALL gds.graph.create('socialGraph', 'User', 'FRIENDS_WITH')

// 运行标签传播算法
CALL gds.labelPropagation.stream('socialGraph')
YIELD nodeId, communityId
MATCH (user:User) WHERE id(user) = nodeId
RETURN communityId, collect(user.username) AS members
ORDER BY communityId

// 删除图
CALL gds.graph.drop('socialGraph')

2. 使用 Louvain 算法

cypher
// 创建图
CALL gds.graph.create('socialGraph', 'User', 'FRIENDS_WITH')

// 运行 Louvain 算法
CALL gds.louvain.stream('socialGraph')
YIELD nodeId, communityId
MATCH (user:User) WHERE id(user) = nodeId
RETURN communityId, collect(user.username) AS members
ORDER BY communityId

// 删除图
CALL gds.graph.drop('socialGraph')

3. 社区分析

cypher
// 分析社区大小
CALL gds.graph.create('socialGraph', 'User', 'FRIENDS_WITH')

CALL gds.louvain.stream('socialGraph')
YIELD nodeId, communityId
MATCH (user:User) WHERE id(user) = nodeId
WITH communityId, count(*) AS size
RETURN communityId, size
ORDER BY size DESC

// 删除图
CALL gds.graph.drop('socialGraph')

影响力分析

1. 度中心性

cypher
// 创建图
CALL gds.graph.create('socialGraph', 'User', 'FOLLOWS')

// 计算度中心性
CALL gds.degree.stream('socialGraph')
YIELD nodeId, score
MATCH (user:User) WHERE id(user) = nodeId
RETURN user.username, score AS followers_count
ORDER BY score DESC
LIMIT 10

// 删除图
CALL gds.graph.drop('socialGraph')

2. 介数中心性

cypher
// 创建图
CALL gds.graph.create('socialGraph', 'User', 'FRIENDS_WITH')

// 计算介数中心性
CALL gds.betweenness.stream('socialGraph')
YIELD nodeId, score
MATCH (user:User) WHERE id(user) = nodeId
RETURN user.username, score AS betweenness
ORDER BY score DESC
LIMIT 10

// 删除图
CALL gds.graph.drop('socialGraph')

3. 特征向量中心性

cypher
// 创建图
CALL gds.graph.create('socialGraph', 'User', 'FRIENDS_WITH')

// 计算特征向量中心性
CALL gds.eigenvector.stream('socialGraph')
YIELD nodeId, score
MATCH (user:User) WHERE id(user) = nodeId
RETURN user.username, score AS eigenvector
ORDER BY score DESC
LIMIT 10

// 删除图
CALL gds.graph.drop('socialGraph')

社交网络可视化

1. 使用 Neo4j Bloom

  • 创建视图:在 Neo4j Bloom 中创建社交网络视图
  • 定义规则:为节点和关系定义视觉规则
  • 探索数据:使用搜索和路径查找功能探索社交网络

2. 使用 D3.js

示例代码

html
<!DOCTYPE html>
<html>
<head>
    <title>社交网络可视化</title>
    <script src="https://d3js.org/d3.v7.min.js"></script>
    <style>
        .node {
            fill: #ccc;
            stroke: #333;
            stroke-width: 1.5px;
        }
        .link {
            stroke: #999;
            stroke-opacity: 0.6;
        }
        .node text {
            pointer-events: none;
            font-size: 10px;
        }
        .user { fill: #4CAF50; }
        .post { fill: #2196F3; }
        .comment { fill: #FF9800; }
    </style>
</head>
<body>
    <div id="graph"></div>
    <script>
        // 模拟社交网络数据
        const data = {
            nodes: [
                {id: 1, label: 'John', type: 'user'},
                {id: 2, label: 'Alice', type: 'user'},
                {id: 3, label: 'Bob', type: 'user'},
                {id: 4, label: 'Post 1', type: 'post'},
                {id: 5, label: 'Comment 1', type: 'comment'}
            ],
            links: [
                {source: 1, target: 2, type: 'friends'},
                {source: 1, target: 3, type: 'follows'},
                {source: 1, target: 4, type: 'posted'},
                {source: 2, target: 4, type: 'liked'},
                {source: 3, target: 4, type: 'commented'},
                {source: 3, target: 5, type: 'posted'}
            ]
        };

        const width = 800;
        const height = 600;

        const svg = d3.select('#graph')
            .append('svg')
            .attr('width', width)
            .attr('height', height);

        const simulation = d3.forceSimulation(data.nodes)
            .force('link', d3.forceLink(data.links).id(d => d.id).distance(100))
            .force('charge', d3.forceManyBody().strength(-300))
            .force('center', d3.forceCenter(width / 2, height / 2));

        const link = svg.append('g')
            .selectAll('line')
            .data(data.links)
            .enter()
            .append('line')
            .attr('class', 'link');

        const node = svg.append('g')
            .selectAll('.node')
            .data(data.nodes)
            .enter()
            .append('g')
            .attr('class', 'node')
            .call(d3.drag()
                .on('start', dragstarted)
                .on('drag', dragged)
                .on('end', dragended));

        node.append('circle')
            .attr('r', d => d.type === 'user' ? 20 : 15)
            .attr('class', d => d.type);

        node.append('text')
            .attr('text-anchor', 'middle')
            .attr('dy', '.35em')
            .text(d => d.label);

        simulation.on('tick', () => {
            link
                .attr('x1', d => d.source.x)
                .attr('y1', d => d.source.y)
                .attr('x2', d => d.target.x)
                .attr('y2', d => d.target.y);

            node
                .attr('transform', d => `translate(${d.x},${d.y})`);
        });

        function dragstarted(event, d) {
            if (!event.active) simulation.alphaTarget(0.3).restart();
            d.fx = d.x;
            d.fy = d.y;
        }

        function dragged(event, d) {
            d.fx = event.x;
            d.fy = event.y;
        }

        function dragended(event, d) {
            if (!event.active) simulation.alphaTarget(0);
            d.fx = null;
            d.fy = null;
        }
    </script>
</body>
</html>

案例应用

1. 社交网络分析平台

  • 功能

    • 用户管理:注册、登录、个人资料管理
    • 社交互动:发布帖子、评论、点赞
    • 好友管理:添加好友、关注用户
    • 推荐系统:好友推荐、内容推荐
    • 数据分析:社区发现、影响力分析
  • 技术栈

    • 前端:React/Vue.js
    • 后端:Node.js/Java
    • 数据库:Neo4j
    • 缓存:Redis
    • 搜索:Elasticsearch

2. 企业内部社交网络

  • 功能

    • 员工管理:员工信息、部门结构
    • 项目协作:项目管理、任务分配
    • 知识共享:文档管理、经验分享
    • 沟通交流:消息传递、讨论组
  • 技术栈

    • 前端:Angular
    • 后端:Spring Boot
    • 数据库:Neo4j
    • 认证:LDAP/OAuth

小结

社交网络分析是 Neo4j 的重要应用场景之一,通过图数据库的强大能力,可以实现好友推荐、社区发现和影响力分析等功能。本文介绍了社交网络的数据模型设计、好友推荐算法、社区发现方法和影响力分析技术,以及社交网络可视化和案例应用。在实际应用中,需要根据具体的业务需求和数据特点,选择合适的算法和技术方案,构建高效、可扩展的社交网络分析系统。