Regional statistics
The Lindorm Ganos spatio-temporal service provides a rich set of functions and data types to compute and analyze spatio-temporal data. You can use the Ganos spatio-temporal service to define geo-fences. You can then use spatio-temporal functions and the real-time computing power of Lindorm Stream to calculate real-time regional statistics based on these geo-fences.
Background information
Many location-based scenarios require aggregate queries on geographic areas. The results are often displayed as heat maps. For example:
In travel scenarios, you need to count the distribution of vehicles in each administrative region or grid. This data helps balance transportation capacity and scheduling.
In location-based service (LBS) customer operations, you need to count customer traffic in each area of interest (AOI). This helps you create rules for distributing coupons.
In Internet of Vehicles (IoV) scenarios, you need to count vehicle clusters in a specific area. This data can be displayed on a large screen as a density map.

Technical implementation
Regional statistics involve two types of data:
Geo-fences: The borders are closed polygons that do not change often. You can use the Polygon data type to store this data in a Lindorm wide table.
Vehicle locations: Vehicle location information is real-time stream data. You can store this data in a Kafka topic.
Lindorm Stream reads real-time data from the Kafka topic, performs real-time computations based on the geo-fences, and stores the results in a Lindorm wide table.

Prerequisites
LindormTable is activated, and its version is 2.6.5 or later. For more information about how to view or upgrade the version, see LindormTable version guide and Minor version update.
Lindorm Stream is activated. For more information, see Activate Lindorm Stream.
The client IP address is added to the Lindorm whitelist. For more information, see Set a whitelist.
Notes
If your application is deployed on an ECS instance and accesses a Lindorm instance over a VPC, ensure that the Lindorm instance and the ECS instance meet the following conditions to ensure network connectivity.
They are in the same region. We recommend that they are also in the same zone to reduce network latency.
The ECS instance and the Lindorm instance are in the same VPC.
Step 1: Create a geo-fence table and a result table
In LindormTable, create a geo-fence table and a result table. These tables will store geo-fence data and computation results.
Create a geo-fence table and insert sample data.
Create the geo-fence table named `regions`.
CREATE TABLE regions(rID INT, rName VARCHAR, fence GEOMETRY, PRIMARY KEY(rID));Insert geo-fence data for three areas: SoHo, Chinatown, and Tribeca.
INSERT INTO regions(rID, rName, fence) VALUES (1, 'SoHo', ST_GeomFromText('POLYGON((-74.00279525078275 40.72833625216264,-74.00547745979765 40.721929158663244,-74.00125029839018 40.71893680218994,-73.9957785919998 40.72521409075776,-73.9972377137039 40.72557184584898,-74.00279525078275 40.72833625216264))')), (2, 'Chinatown', ST_GeomFromText('POLYGON((-73.99712367114876 40.71281582267133,-73.9901070123658 40.71336881907936,-73.99023575839851 40.71452359088633,-73.98976368961189 40.71554823078944,-73.99551434573982 40.717337246783735,-73.99480624255989 40.718491949759304,-73.99652285632942 40.719109951574,-73.99776740131233 40.7168005470334,-73.99903340396736 40.71727219249899,-74.00193018970344 40.71938642421256,-74.00409741458748 40.71688186545551,-74.00051398334358 40.71517415773184,-74.0004281526551 40.714377212470005,-73.99849696216438 40.713450141693166,-73.99748845157478 40.71405192594819,-73.99712367114876 40.71281582267133))')), (3, 'Tribeca', ST_GeomFromText('POLYGON((-74.01091641815208 40.72583120006787,-74.01338405044578 40.71436586362705,-74.01370591552757 40.713617702123415,-74.00862044723533 40.711308107057235,-74.00194711120628 40.7194238654018,-74.01091641815208 40.72583120006787))'));
Create the result table `cresult`.
CREATE TABLE cresult(rName VARCHAR, ws TIMESTAMP, we TIMESTAMP, carCount BIGINT, PRIMARY KEY(rName,ws));
Step 2: Write stream data
Lindorm Stream is fully compatible with open source Kafka APIs. You can connect to Lindorm Stream and write test data using an open source Kafka client or script tool.
The following example shows how to write data using the open source Kafka script tool.
Download and install the Kafka script tool. For more information, see Connect to Lindorm Stream using the open source Kafka script tool.
Create a Kafka topic named
logVehicleTs.bin/kafka-topics.sh --bootstrap-server <Lindorm Stream Kafka endpoint> --topic logVehicleTs --createThe Lindorm Stream Kafka endpoint is the Kafka connection address for the stream engine. This endpoint supports access only over a VPC. For more information about how to obtain the endpoint, see View stream engine endpoints.
Write test data to the Kafka topic. Press Ctrl+C to stop writing.
bin/kafka-console-producer.sh --bootstrap-server <Lindorm Stream Kafka endpoint> --topic logVehicleTs {"UID": "A", "x":"-74.00035", "y": "40.72432", "tripTime":"2020-01-01 08:00:00"} {"UID": "B", "x":"-74.00239", "y": "40.71692", "tripTime":"2020-01-01 08:00:30"} {"UID": "C", "x":"-74.00201", "y": "40.72563", "tripTime":"2020-01-01 08:01:00"} {"UID": "D", "x":"-74.00158", "y": "40.72412", "tripTime":"2020-01-01 08:01:30"} {"UID": "E", "x":"-73.99836", "y": "40.71588", "tripTime":"2020-01-01 08:02:00"} {"UID": "F", "x":"-74.01015", "y": "40.71422", "tripTime":"2020-01-01 08:02:30"} {"UID": "G", "x":"-73.99183", "y": "40.71451", "tripTime":"2020-01-01 08:03:00"} {"UID": "H", "x":"-73.99595", "y": "40.71773", "tripTime":"2020-01-01 08:03:30"}You can use the
bin/kafka-console-consumer.sh --bootstrap-server <Lindorm Stream Kafka endpoint> --topic logVehicleTs --from-beginningcommand to check whether the data was written successfully.
Step 3: Submit a stream engine computing task
Use Flink SQL to submit a Lindorm Stream computing task. The task reads data from the Kafka topic and performs computations using the geo-fence data.
Connect to Lindorm Stream. For more information, see Step 2: Install the stream engine client.
Submit the computing task.
The computing task creates a 10-minute tumbling window and counts the data in the window every 10 minutes. This involves the following steps:
Load the
ganosfunction module.In the Flink Job, create three tables: the data source table `logCarWithTs`, the dimension table `regions`, and the result table `cresult`. Set the connector parameters to associate these tables with the Kafka topic, the `regions` geo-fence table, and the `cresult` result table.
Create a stream task. Use the
countstatistical function, theST_Containsrelational function, and theTUMBLEtime window function to filter the data. Then, write the computation results to the `cresult` result table.
The following is sample code:
CREATE FJOB fenceWs ( LOAD MODULE ganos; CREATE TABLE logCarWithTs( `uID` STRING, `x` DOUBLE, `y` DOUBLE, `tripTime` TIMESTAMP(0), WATERMARK for `tripTime` AS `tripTime`-INTERVAL '1' MINUTES ) WITH ('connector'='kafka', 'topic'='logVehicleTs', 'scan.startup.mode'='earliest-offset', 'properties.bootstrap.servers'='<Lindorm Stream Kafka endpoint>', 'format'='json'); -- create area table CREATE TABLE regions( `rID` INT, `rName` STRING, `fence` GEOMETRY, PRIMARY KEY (`rID`) NOT ENFORCED ) WITH ('connector'='lindorm', 'seedServer'='<LindormTable HBase Java API endpoint>', 'userName'='root', 'password'='test_password', 'tableName'='regions', 'namespace'='default'); -- create result table CREATE TABLE cresult( `rName` STRING, `ws` TIMESTAMP(0), `we` TIMESTAMP(0), `carCount` BIGINT, PRIMARY KEY (`rName`, `ws`) NOT ENFORCED ) WITH ('connector'='lindorm', 'seedServer'='<LindormTable HBase Java API endpoint>', 'userName'='root', 'password'='test_password', 'tableName'='cresult', 'namespace'='default'); -- count cars in each area every 10 minutes INSERT INTO cresult SELECT regions.rName AS rName, window_start AS ws, window_end AS we, count(*) AS carCount FROM TABLE(TUMBLE(TABLE logCarWithTs, DESCRIPTOR(tripTime),INTERVAL '10' MINUTES)) JOIN regions ON ST_Contains(regions.fence,ST_MakePoint(x,y)) GROUP BY regions.rName,window_start, window_end; );For more information about how to obtain the LindormTable HBase Java API endpoint, see View LindormTable endpoints.
NoteFor more information about the functions used in the computing task, see Relational functions, Count function, and TUMBLE window function.
Step 4: View the results
You can view the regional statistics by running the following statement.
SELECT rName, carCount FROM cresult;The following is returned:
+-----------+----------+ | rName | carCount | +-----------+----------+ | Chinatown | 3 | | SoHo | 4 | | Tribeca | 3 | +-----------+----------+`carCount` represents the number of vehicles in each area.