Qeasy Cloud
Get Started

Pulling DingTalk Contacts Incrementally into MySQL: A Hands-on Tutorial Built on Qeasy

· Integration Solutions· 7 views· 4 min read

What This Strategy Solves (Scenario & Value)

In supply chain systems, people master data is the accountable party behind every document. For a retail business whose transactional system runs on MySQL, the organizational hierarchy lives in DingTalk — new hires, transfers, and resignations happen every day. We need to pull these changes from DingTalk contacts as an "insert + update" stream into a business table in MySQL, which then serves as the dictionary for owners and approvers in downstream documents.

On the surface it looks like "just pulling a list of people", but the engineering pain points are real: the interface is a query-style WebAPI, not an event push, so we have to control deduplication, decide insert vs update, and handle empty fields ourselves. Qeasy (轻易云数据集成平台) wraps "scheduling + mapping + write" into one observable strategy unit, and on the customer site we focused on getting this single strategy stable.

Data Flow and Field Mapping (Source → Middle Layer → Target)

The flow is one-way: DingTalk on side B → Qeasy middle layer → MySQL on side A. The source uses topapi/v2/user/get to fetch the detail of a specific user; the target uses an INSERT ... ON DUPLICATE KEY UPDATE SQL to write into the dingtalk_user table.

Key field mapping (DingTalk user → MySQL dingtalk_user):

Business meaningDingTalk source fieldMySQL target fieldHandling notes
DingTalk unique user iduseriduseridPrimary key, basis for UPSERT
Job numberjob_numberjob_numberString, nullable
NamenamenameNon-null validation
TitletitletitleNullable
Union idunionidunionidCross-tenant identifier
Department listdept_id_listdept_id_listOriginal array, needs serialization
Dept leader flagleader_in_deptleader_in_deptSerialized to string
Delete flagderived from activedel_flagactive=false writes 1

One easily overlooked request parameter is dep_strategy, which points to another "DingTalk departments" strategy id and carries the department context on the source side.

How to Configure It on Qeasy

Source side: a WebAPI query strategy, api=topapi/v2/user/get, method=POST, effect=QUERY. Enable autoFillResponse so the platform spreads response fields into the mapping panel automatically — no need to hand-write a long list of describe entries. Keep idCheck and buildModel off for a lean configuration.

Target side: a WebAPI execute strategy, api=execute, effect=EXECUTE. main_params is an object input that exposes named placeholders like :userid and :name to the mapping layer; the actual main_sql is placed under otherRequest as an UPSERT.

The mapping layer is the heart of this design. Qeasy's mapping panel supports centralized management of encodings and constants. We like to extract small rules such as "DingTalk status → delete flag" into a dedicated mapping rule rather than embedding them in SQL. For nullable fields like title, unionid, and dept_id_list, we explicitly enable "write NULL on empty" to avoid empty strings polluting downstream JOINs.

Implementation Steps

  1. Create table and initialize. On the MySQL side, create the dingtalk_user table with userid as primary key and name non-nullable; do an initial full backfill as the baseline.
  2. First full trigger. After the upstream strategy user-钉钉部门-新增-修改-OK produces the department dictionary, run this strategy in full mode department by department to confirm there are no duplicates and no missing users.
  3. Switch to incremental scheduling. Source cron is 5,17 12,18 * * 1-6, target cron is 12,24 12,18 * * 1-6 — the source runs 7 minutes earlier than the target to leave a time window for fetching and mapping. This is a common pattern among Qeasy customers: "source first, target later, dual frequency points, staggered". We exclude Sunday and Monday morning to avoid weekend turnover noise.
  4. Observability and reconciliation. After 18:24 each day, run SELECT COUNT(*) FROM dingtalk_user and compare with the DingTalk admin console. If the deviation exceeds the threshold, trigger manual intervention.

Pitfalls Recap

  1. UPSERT without a primary key constraint. ON DUPLICATE KEY UPDATE relies on a primary key or unique index. Early on we only made userid a regular index, which led to insert failures under concurrency — it looked like "the strategy didn't run" when the database was actually rejecting the duplicates.
  2. dept_id_list written as raw array. DingTalk returns a JSON array; writing it directly makes downstream LIKE queries fail. The safer way is to serialize to a comma-separated string in the mapping layer and decode it back when needed.
  3. Resigned users being "resurrected". If we don't derive from active=false on the source side, UPSERT will overwrite the resigned flag and bring them back to active. We added a dedicated mapping rule to convert active into del_flag before writing.
  4. dep_strategy parameter overlooked. This field depends on the output of another department strategy. Early on the dependency wasn't drawn clearly, and we saw users running before departments were ready. Explicitly declaring depends_on in Qeasy solved it.
  5. Schedule too aggressive. We initially tried every 5 minutes, but DingTalk rate-limited us. Switching to 4 frequency points per day stabilized the pipeline.

When This Applies and When It Doesn't

Applies: organizations whose hierarchy lives in DingTalk while the business database is MySQL, in a private deployment where a people dictionary is needed for downstream documents; DingTalk provides no push channel and must be polled.

Does not apply: scenarios that need minute-level real-time approval flow (polling inherently has latency), or scenarios where DingTalk departments/users rarely change and a periodic sync is overkill.

Original content. Please credit the source when reposting: https://www.qeasy.cloud/insights/solutions/strat-mysql-dingtalk-4900-user-326aa509

Comments