使用 apoc.load.jsonParams 从 Zendesk 加载数据到 Neo4j,以了解文章订阅者
以下文档描述了如何使用 Zendesk API 将数据从 Zendesk 加载到 Neo4j,特别是关于选择订阅/关注知识库章节的用户的数据。本文档旨在解决 Zendesk 问答 中提出的相关问题。虽然 Zendesk UI 允许用户订阅/关注知识章节,但并未为 Zendesk 管理员提供查看每个章节订阅用户的相应界面。
下面的 Cypher 将创建支持索引,然后遍历每个章节,获取该章节的所有用户 ID,创建用户 ID 与章节之间的关系,并为用户 ID 填充更多标识信息。
create index on :Section(id);
create index on :User(id);
create index on :Organization(id);
// get all sections
CALL apoc.load.jsonParams("https://your_domain.zendesk.com/api/v2/help_center/sections.json",{Authorization:"Basic base64Encoded_username:password"},null)
yield value as sectionvalue
with sectionvalue
unwind sectionvalue.sections as section_item
Merge (n:Section {id:section_item.id,name:section_item.name, created_at:section_item.created_at, updated_at:section_item.updated_at,url:section_item.html_url})
with section_item.id as secid
// foreach section then find the subscribers
CALL apoc.load.jsonParams("https://your_domain.zendesk.com/api/v2/help_center/sections/"+secid+"/subscriptions.json?per_page=200",{Authorization:"Basic base64Encoded_username:password"},null)
yield value as subscribervalue
with subscribervalue, secid
unwind subscribervalue.subscriptions as subscription_item
// create the relationship from the User to the Secction through the :Follows relationship
match (s:Section {id:secid}) with s,subscription_item
merge (n:User {id: subscription_item.user_id})
merge (n)-[:Follows {subscribed_on: subscription_item.created_at}]->(s)
with subscription_item.user_id as s_userid
CALL apoc.load.jsonParams("https://your_domain.zendesk.com/api/v2/users/"+s_userid+".json",{Authorization:"Basic base64Encoded_username:password"},null)
yield value as userRecord
with userRecord, s_userid
unwind userRecord.user as uid
match (n:User {id:s_userid})
set
n.name=uid.name,
n.email=uid.email,
n.created_at=uid.created_at,
n.last_login=uid.last_login_at,
n.url=uid.url;
match (n:User) where exists(n.organization_id)
with n,n.organization_id as organization_id
CALL apoc.load.jsonParams("https://your_domain.zendesk.com/api/v2/organizations/"+organization_id+".json",{Authorization:"Basic base64Encoded_username:password"},null)
yield value as orgRecord
unwind orgRecord.organization as orgid
with n,orgid
merge (o:Organization {id: orgid.id, name: orgid.name, created_at: orgid.created_at})
merge (n)-[:IS_MEMBER_OF_ORG]->(o);
加载 140 个节点(81 个用户,7 个章节,52 个组织)以及相关关系共花费了 54 秒。
在上述 Cypher 代码中,您需要替换所有出现的
`your_domain` with the actual domain your Zendesk is hosted under `base64Encoded_username:password` with the base64 encoding (https://www.base64encode.org/) of a Zendesk Admin user and password who has Admin rights in Zendesk
此外,要使用基本身份验证,必须在 Zendesk Support 管理界面的 Admin > Channels > API 中启用 密码访问。
最后,根据 Zendesk API,如果您预计每次 API 调用的结果超过 100 条,则需要考虑分页。
分页 默认情况下,大多数列表端点每页返回最多 100 条记录。您可以通过在请求 URL 参数中传递 per_page 参数来按请求更改记录数。例如:per_page=50。但是,在大多数端点上每页记录数不能超过 100 条。
当响应超过每页最大数量时,您可以通过递增 page 参数来分页浏览记录。例如:page=3。列表结果在响应体中包含 next_page 和 previous_page URL,便于导航。
将上述 Cypher 复制到 shell 脚本文件中,例如 build_zd.cql,然后即可通过运行 cypher-shell 来执行。
$ cat build_zd.cql | bin/cypher-shell
因此,生成的图模型定义如下

整个图的展示如下

由此我们可以看到,有 4 个章节(即绿色节点/圆形)没有订阅者(左上角的 4 个绿色节点)。另外三个章节有订阅者,右侧的章节拥有最多的订阅者(即蓝色节点/圆形)。此外,部分订阅者/用户选择关注多个章节。
每个节点的定义属性如下
User:
name
email
created-at
last_login
url
suspended
orgainization_id
id
Section:
name
url
created_at
updated_at
id
Organization:
name
created_at
id
查询图的常用 Cypher 语句
-
查找每个章节的订阅用户数量
match (n:Section) return n.name, size ( (n)<-[:Follows]-() ) as subscribers order by subscribers desc; -
查找用户及其关联的组织、所属章节以及用户订阅的时间
match (s:Section)<-[r:Follows]-(u:User)-[:IS_MEMBER_OF_ORG]->(o:Organization) return s.name, u.name, u.email, o.name, u.suspended, r.subscribed_on as DateWhenSubscribed order by s.name, o.name, u.name
此页面有帮助吗?