Use a StarRocks node in DataWorks to process the ods_user_info_d_starrocks user information table and the ods_raw_log_d_starrocks access log data synchronized to StarRocks to generate target user profile data. You will learn how to compute and analyze synchronized data and complete a simple data processing scenario in a data warehouse.
Prerequisites
Before you begin, complete the operations in Synchronize data.
-
You used Data Integration to synchronize basic user information from the
ods_user_info_dtable in MySQL to theods_user_info_d_starrockstable in StarRocks. -
You used Data Integration to synchronize website access logs from the
user_log.txtobject in Object Storage Service (OSS) to theods_raw_log_d_starrockstable in StarRocks.
Objectives
Process the ods_user_info_d_starrocks and ods_raw_log_d_starrocks tables in StarRocks to generate a basic user profile table.
-
Parse the log information in the synchronized log table
ods_raw_log_d_starrocksinto multiple fields and generate a new detailed log table nameddwd_log_info_di_starrocks. -
Join the detailed log table
dwd_log_info_di_starrockswith the user tableods_user_info_d_starrockson the uid field to generate an aggregated user log table nameddws_user_info_all_di_starrocks. -
Because the
dws_user_info_all_di_starrockstable contains many fields and a large amount of data, you will further process it into theads_user_info_1d_starrockstable for data consumption.
Go to DataStudio
Log on to the DataWorks console. In the target region, click in the left-side navigation pane. Select a workspace from the drop-down list and click Go to Data Development.
Step 1: Design the workflow
In the Synchronize data stage, you synchronized data to StarRocks. The next step is to process this data to generate basic user profile data.
-
Nodes and logic at each layer.
On the workflow canvas, click New Node to create the following nodes for data processing.
Node category
Node type
Node name
(Named after the output table)
Code logic
Database
StarRocksdwd_log_info_di_starrocksUse built-in functions and user-defined functions (UDFs) to split the raw log data in
ods_raw_log_d_starrocksand write the data into multiple fields of thedwd_log_info_di_starrockstable.Database
StarRocksdws_user_info_all_daily_incremental_starrocksAggregate the basic user information and the initially processed log data and merge them into a single table.
Database
StarRocksads_user_info_1d_starrocksFurther process the data to generate a basic user profile.
-
Workflow DAG.
Drag the node components onto the workflow canvas and connect them by drawing lines to set their dependencies. This creates the workflow for the data processing stage.
Step 2: Create a function
Upload a function resource and register it as a function in the data source so you can use it in the workflow to process data. For more information, see Create resources and functions.
Before you register a function by using the Java UDF feature in StarRocks, you must set the FE configuration parameter enable_udf to TRUE on the Instance Configuration page and restart the instance for the parameter to take effect. For more information, see Parameter Configuration.
If this parameter is not configured, the following error is reported:
FAILED: Getting analyzing error. Detail message: UDF is not enabled in FE, please configure enable_udf=true in fe/conf/fe.conf or .
Upload the function
This tutorial provides the resource package ip2region-starrocks.jar required for the UDF. Download the package to your local computer, log on to the Object Storage Service (OSS) console, and upload the package to your OSS bucket.
For more information about how to create an OSS bucket, see Create a bucket. You can upload the JAR package to the created bucket. This operation incurs OSS storage fees.
The storage path in the OSS environment for this tutorial is as follows:
-
Bucket name:
test(The name of an OSS bucket must be globally unique. Specify a custom name for the bucket.) -
Resource storage path:
dataworks_starrocks/ip2region-starrocks.jar -
Full path:
https://test.oss-cn-shanghai-internal.aliyuncs.com/dataworks_starrocks/ip2region-starrocks.jar. The full path is formatted as follows.https://${Bucket name}.oss-cn-shanghai-internal.aliyuncs.com/${Resource storage path}Note-
Use the ECS classic network (internal network) access address for the bucket where the UDF is located.
-
When you use an internal network address, the region of the OSS bucket must be the same as the region of the DataWorks workspace. In this tutorial, both are in the China (Shanghai) region.
-
Register the function
-
Create an Ad Hoc Query node.
In the left-side navigation pane of the DataStudio page, click
to go to the Ad Hoc Query panel. Right-click Ad Hoc Query and choose . -
Edit and run the code.
CREATE FUNCTION getregion(string) RETURNS string PROPERTIES ( "symbol" = "com.starrocks.udf.sample.Ip2Region", "type" = "StarrocksJar", "file" = "Replace this with the full OSS path of the file. For information about how to obtain the file path, see the 'Upload the function' step." );Verify that the function is registered successfully.
select getregion('Your local IP address');
Step 3: Configure the StarRocks nodes
dwd_log_info_di_starrocks node
On the workflow panel, double-click the StarRocks database node dwd_log_info_di_starrocks, go to the edit page of the dwd_log_info_di_starrocks node, and write the SQL code that processes the fields of the upstream table ods_raw_log_d_starrocks and writes the results into the dwd_log_info_di_starrocks table.
1. Configure the code
Double-click the dwd_log_info_di_starrocks node to open the node configuration page and enter the following statement.
CREATE TABLE IF NOT EXISTS dwd_log_info_di_starrocks (
uid STRING COMMENT 'User ID',
ip STRING COMMENT 'IP address',
TIME STRING COMMENT 'Time, in yyyymmddhh:mi:ss format',
status STRING COMMENT 'Status code returned by the server',
bytes STRING COMMENT 'Number of bytes returned to the client',
region STRING COMMENT 'Region, obtained from the IP address',
method STRING COMMENT 'HTTP request type',
url STRING COMMENT 'URL',
protocol STRING COMMENT 'HTTP protocol version',
referer STRING COMMENT 'Source URL',
device STRING COMMENT 'Terminal type',
identity STRING COMMENT 'Access type: crawler, feed, user, or unknown',
dt DATE NOT NULL COMMENT 'Time'
) DUPLICATE KEY(uid)
COMMENT 'User behavior analysis case - Detailed table for website access logs'
PARTITION BY(dt)
PROPERTIES ("replication_num" = "1");
-- This example uses dynamic partitioning on the dt field. To prevent data duplication if the node is re-run, the following command deletes the target partition before each run.
ALTER TABLE dwd_log_info_di_starrocks DROP PARTITION IF EXISTS p${var} FORCE;
-- Scenario: The following SQL uses the getregion function to parse IP addresses from raw log data. It also uses regular expressions and other methods to break down the raw data into analyzable fields and writes them to the dwd_log_info_di_starrocks table.
-- Notes:
-- 1. You must register a UDF before using it in a DataWorks node.
-- 2. DataWorks provides scheduling parameters to write daily incremental data to the corresponding business partition of a destination table.
-- In actual development, you can define code variables in the ${variable_name} format. On the scheduling properties page, you can then assign scheduling parameters as values to these variables to enable dynamic parameter passing in scheduling scenarios.
INSERT INTO dwd_log_info_di_starrocks
SELECT
uid
, ip
, time
, status
, bytes
, getregion(ip) as region -- Use a UDF to get the region from the IP address.
,REGEXP_EXTRACT(request, '([^ ]+)', 1) AS method
,REGEXP_EXTRACT(request, '^[^ ]+ (.*) [^ ]+$', 1) AS url
,REGEXP_EXTRACT(request, '([^ ]+)$', 1) AS protocol
,REGEXP_EXTRACT(referer, '^[^/]+://([^/]+)', 1) AS referer
, CASE
WHEN LOWER(agent) REGEXP 'android' THEN 'android'
WHEN LOWER(agent) REGEXP 'iphone' THEN 'iphone'
WHEN LOWER(agent) REGEXP 'ipad' THEN 'ipad'
WHEN LOWER(agent) REGEXP 'macintosh' THEN 'macintosh'
WHEN LOWER(agent) REGEXP 'windows phone' THEN 'windows_phone'
WHEN LOWER(agent) REGEXP 'windows' THEN 'windows_pc'
ELSE 'unknown'
END AS device
, CASE
WHEN LOWER(agent) REGEXP '(bot|spider|crawler|slurp)' THEN 'crawler'
WHEN LOWER(agent) REGEXP 'feed' OR REGEXP_EXTRACT(request, '^[^ ]+ (.*) [^ ]+$', 0) REGEXP 'feed' THEN 'feed'
WHEN NOT (LOWER(agent) REGEXP '(bot|spider|crawler|feed|slurp)')
AND agent REGEXP '^(Mozilla|Opera)'
AND NOT (REGEXP_EXTRACT(request, '^[^ ]+ (.*) [^ ]+$', 0) REGEXP 'feed') THEN 'user'
ELSE 'unknown'
END AS identity,
cast('${var}' as DATE )as dt
FROM (
SELECT
SPLIT_PART(CAST(col AS VARCHAR(65533)), '##@@', 1) AS ip
, SPLIT_PART(CAST(col AS VARCHAR(65533)), '##@@', 2) AS uid
, SPLIT_PART(CAST(col AS VARCHAR(65533)), '##@@', 3) AS time
, SPLIT_PART(CAST(col AS VARCHAR(65533)), '##@@', 4) AS request
, SPLIT_PART(CAST(col AS VARCHAR(65533)), '##@@', 5) AS status
, SPLIT_PART(CAST(col AS VARCHAR(65533)), '##@@', 6) AS bytes
, SPLIT_PART(CAST(col AS VARCHAR(65533)), '##@@', 7) AS referer
, SPLIT_PART(CAST(col AS VARCHAR(65533)), '##@@', 8) AS agent
FROM
ods_raw_log_d_starrocks
WHERE
dt = '${var}'
) a;
2. Configure scheduling properties
Click Scheduling Configuration on the right side of the edit page for the dwd_log_info_di_starrocks node. The configuration details are as follows:
|
Parameter |
Configuration |
|
Scheduling Parameters |
In the Scheduling Parameters section, click Add Parameter and add:
|
|
Scheduling Dependency |
In the Scheduling Dependency section, click Parsing input and output from code to quickly set dependencies for the node based on data lineage. For more information, see Configure scheduling dependencies. |
When the Scheduling period is set to Daily, you do not need to separately configure the Scheduling Time for the current node. The daily start time of the current node is controlled by the scheduled time of the workflow virtual node workshop_start_starrocks. This means the node is scheduled only after 00:30 every day.
3. Save the configuration
Configure other properties as needed. When you are finished, click the
icon in the node code editor's toolbar to save the configuration.
dws_user_info_all_di_starrocks node
In the workflow panel, double-click the StarRocks database node dws_user_info_all_di_starrocks to go to the edit page of the dws_user_info_all_di_starrocks node. On the edit page, write the SQL code to merge the upstream tables dwd_log_info_di_starrocks and ods_user_info_d_starrocks into the dws_user_info_all_di_starrocks table.
Edit the code
Double-click the dws_user_info_all_di_starrocks node to open the node configuration page, and enter the following statement.
CREATE TABLE IF NOT EXISTS dws_user_info_all_di_starrocks (
uid STRING COMMENT 'User ID',
gender STRING COMMENT 'Gender',
age_range STRING COMMENT 'Age range',
zodiac STRING COMMENT 'Zodiac sign',
region STRING COMMENT 'Region, obtained from the IP address',
device STRING COMMENT 'Terminal type',
identity STRING COMMENT 'Access type: crawler, feed, user, or unknown',
method STRING COMMENT 'HTTP request type',
url STRING COMMENT 'URL',
referer STRING COMMENT 'Source URL',
TIME STRING COMMENT 'Time, in yyyymmddhh:mi:ss format',
dt DATE NOT NULL COMMENT 'Time'
) DUPLICATE KEY(uid)
COMMENT 'User behavior analysis case - Wide table for user website access information'
PARTITION BY(dt)
PROPERTIES ("replication_num" = "1");
-- This example uses dynamic partitioning on the dt field. To prevent data duplication if the node is re-run, the following command deletes the target partition before each run.
ALTER TABLE dws_user_info_all_di_starrocks DROP PARTITION IF EXISTS p${var} FORCE;
-- Scenario: Aggregate the processed log data from dwd_log_info_di_starrocks and the basic user information data from ods_user_info_d_starrocks, and write the result to the dws_user_info_all_di_starrocks table.
-- Note: DataWorks provides scheduling parameters to write daily incremental data to the corresponding business partition of a destination table.
-- In actual development, you can define code variables in the ${variable_name} format. On the scheduling properties page, you can then assign scheduling parameters as values to these variables to enable dynamic parameter passing in scheduling scenarios.
INSERT INTO dws_user_info_all_di_starrocks
SELECT
IFNULL(a.uid, b.uid) AS uid,
b.gender,
b.age_range,
b.zodiac,
a.region,
a.device,
a.identity,
a.method,
a.url,
a.referer,
a.time,
a.dt
FROM dwd_log_info_di_starrocks a
LEFT JOIN ods_user_info_d_starrocks b
ON a.uid = b.uid
WHERE a.dt = '${var}';
Configure scheduling properties
Click Scheduling Configuration on the right side of the edit page for the dws_user_info_all_di_starrocks node. The configuration details are as follows:
|
Parameter |
Configuration |
Illustration |
|
Scheduling Parameters |
In the Scheduling Parameters section, click Add Parameter and add:
|
|
|
Scheduling Dependency |
In the Scheduling Dependency section, click Parsing input and output from code to quickly set dependencies for the node based on data lineage. For more information, see Configure scheduling dependencies. |
When the Scheduling period is set to Daily, you do not need to separately configure the Scheduling Time for the current node. The daily start time of the current node is controlled by the scheduled time of the workflow virtual node workshop_start_starrocks. This means the node is scheduled only after 00:30 every day.
3. Save the configuration
Configure other required properties as needed. After the configuration is complete, click the
icon in the toolbar of the node code editor to save the current configuration.
Configure the ads_user_info_1d_starrocks node
In the workflow panel, double-click the ads_user_info_1d_starrocks node in the StarRocks database to open its edit page. On this page, write SQL code to process the upstream dws_user_info_all_di_starrocks table and write the result to the ads_user_info_1d_starrocks table.
1. Edit the code
CREATE TABLE IF NOT EXISTS ads_user_info_1d_starrocks (
uid STRING COMMENT 'User ID',
region STRING COMMENT 'Region, obtained from the IP address',
device STRING COMMENT 'Terminal type',
pv BIGINT COMMENT 'Page views (PV)',
gender STRING COMMENT 'Gender',
age_range STRING COMMENT 'Age range',
zodiac STRING COMMENT 'Zodiac sign',
dt DATE NOT NULL COMMENT 'Time'
) DUPLICATE KEY(uid)
COMMENT 'User behavior analysis case - User profile data'
PARTITION BY(dt)
PROPERTIES ("replication_num" = "1");
-- This example uses dynamic partitioning on the dt field. To prevent data duplication if the node is re-run, the following command deletes the target partition before each run.
ALTER TABLE ads_user_info_1d_starrocks DROP PARTITION IF EXISTS p${var} FORCE;
-- Scenario: The following SQL is used to further process the dws_user_info_all_di_starrocks wide table of user access information to generate basic user profile data and write it to the ads_user_info_1d_starrocks table.
-- Note: DataWorks provides scheduling parameters to write daily incremental data to the corresponding business partition of a destination table.
-- In actual development, you can define code variables in the ${variable_name} format. On the scheduling properties page, you can then assign scheduling parameters as values to these variables to enable dynamic parameter passing in scheduling scenarios.
INSERT INTO ads_user_info_1d_starrocks
SELECT
uid,
MAX(region) AS region,
MAX(device) AS device,
COUNT(*) AS pv,
MAX(gender) AS gender,
MAX(age_range) AS age_range,
MAX(zodiac) AS zodiac,
dt
FROM dws_user_info_all_di_starrocks
WHERE dt = '${var}'
GROUP BY uid, dt;
select * FROM dws_user_info_all_di_starrocks
WHERE dt = '${var}';
2. Configure scheduling properties
Click Scheduling Configuration on the right side of the ads_user_info_1d_starrocks node's edit page. The configuration details are as follows:
|
Parameter |
Configuration |
Illustration |
|
Scheduling Parameters |
In the Scheduling Parameters section, click Add Parameter and add:
|
|
|
Scheduling Dependency |
In the Scheduling Dependency section, click Parsing input and output from code to quickly set dependencies for the node based on data lineage. For more information, see Configure scheduling dependencies. |
When the Scheduling period is set to Daily, you do not need to separately configure the Scheduling Time for the current node. The daily start time of the current node is controlled by the scheduled time of the workflow virtual node workshop_start_starrocks. This means the node is scheduled only after 00:30 every day.
3. Save the configuration
Configure other required properties as needed. After the configuration is complete, click the
icon in the toolbar of the node code editor to save the current configuration.
Run the task
Run the workflow
-
Go to the workflow pane.
Double-click Workflow under Workflows to go to the workflow canvas.
-
Run the workflow.
On the workflow canvas, click the
icon in the toolbar. This runs the workflow for the data inheritance stage based on upstream and downstream dependencies. -
Check the task running status.
If the node is in the
state, the synchronization process is successful. -
View the task execution log.
Right-click the
dwd_log_info_di_starrocks,dws_user_info_all_di_starrocks, andads_user_info_1d_starrocksnodes on the canvas and select View Log to view the detailed synchronization process.
View the workflow results
-
Create an Ad Hoc Query file. For more information, see Create an Ad Hoc Query.
On the DataStudio page, in the navigation pane on the left, click
to go to the Ad Hoc Query pane. Right-click Ad Hoc Query and select . -
Query the processed result table.
Run the following SQL statement to confirm that the user profile data and user website access log data have been synchronized from the test database to your StarRocks instance.
-- The partition column in the query statement must be updated to the business date. For example, if the task runs on 20240102, the business date is 20240101, which is the day before the task run date. SELECT * from ads_user_info_1d_starrocks where dt=business_date;
Schedule tasks
After you process the data and confirm that the task runs correctly, commit and deploy the task to the production environment. For more information, see Deploy Center.
Commit to the development environment
On the workflow pane toolbar, click the
icon to commit all tasks in the workflow. Then, click Confirm.
Deploy to the production environment
After you commit a task, it enters the development environment. Tasks in the development environment are not automatically scheduled. Deploy the task to the production environment.
On the workflow page, click the
icon on the toolbar, or choose . You are directed to the Create Release Package page. On this page, two deployment methods are available.
-
Deploy a single item: When the status of a task item changes to Check Passed, click the Publish button to the right of the item to deploy it.
-
Deploy items in a batch:
-
Select multiple items and click Deploy.
-
Select multiple items and click Add to List. Then, click Nodes to Deploy in the upper-right corner. In the pane that appears, check the task list. If the list is correct, click Package and Deploy All.
-
When deploying tasks in a batch, you must also deploy the resources and functions used by the workflow.
6. Backfill data
You can backfill historical data in the production environment. For more information, see Manage backfill instances. Follow these steps.
-
Go to the Operation Center.
After the task is deployed, click Operation and Maintenance Center in the upper-right corner, or choose to go to the Operation Center page.
-
Backfill data for the scheduled node.
-
In the navigation pane on the left, choose to go to the Auto Triggered Node page. Click the
workshop_start_starrocksvirtual start node to go to the DAG. -
Right-click the
workshop_start_starrocksnode and choose . -
Select all descendant nodes of the
workshop_start_starrocksnode, enter the business date, and click OK. You are redirected to the Retroactive Instances page.
-
Next steps
After deployment, you can view the created table details or consume its data. For more information, see Manage Data and API Data Service.