Electronic geofencing
Electronic geofencing, also known as an electronic alert system, is used in applications that process large volumes of data in real time, such as urban planning and traffic dispatching. This solution guides you through using the spatiotemporal functions of Lindorm Ganos to calculate the relationship between real-time vehicle positions and geofences, allowing for precise vehicle monitoring.
Background
In vehicle management, electronic geofencing monitors a vehicle's travel area and detects deviations from a predefined route. This capability improves vehicle scheduling and management.
You predefine several areas as geofences. Store this static data in a table in the Wide Table Engine. Vehicle location data (coordinates) is uploaded in real time and streamed into Kafka. The Lindorm Stream Engine subscribes to this real-time data from Kafka to continuously calculate the relationship between multiple vehicle positions and the geofences.
Prerequisites
-
The Wide Table Engine is enabled, and its version is 2.6.5 or later. To check or upgrade your current version, see Version notes for the Wide Table Engine and Upgrade a minor version.
-
The Lindorm Stream Engine is enabled. For instructions, see Enable the Stream Engine.
-
The client IP address is added to the Lindorm whitelist. For instructions, see Configure a whitelist.
-
Java Development Kit (JDK) 1.8 or later is installed.
Usage notes
In this tutorial, you deploy the Lindorm client on an ECS instance and access data over a VPC.
Before you access a Lindorm instance over a VPC, ensure that your Lindorm instance and ECS instance meet the following conditions to ensure network connectivity.
-
They are in the same region. For lower network latency, we recommend placing them in the same availability zone.
-
The ECS instance and the Lindorm instance are in the same VPC.
Step 1: Create geofence and result tables
In the Wide Table Engine, create a table to store geofence data and another table to store calculation results.
-
Connect to and use the Wide Table Engine by using Lindorm-cli.
-
Create a geofence table and insert sample data.
-
Create a geofence table named
regions. The table has three fields: rID (region ID), rName (region name), and fence (geofence area).CREATE TABLE regions(rID INT, rName VARCHAR, fence GEOMETRY, PRIMARY KEY(rID)); -
Insert geofence data for three areas named 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 a result table named
fresult.CREATE TABLE fresult(uID VARCHAR, rName VARCHAR, rID INT, PRIMARY KEY (uID));
Step 2: Write streaming data
You can connect to the Lindorm Stream Engine and write test data using an open-source client or script tool.
This example shows how to use an open-source script tool.
-
Download and install the script tool. For detailed instructions, see Connect to the Lindorm Stream Engine by using an open-source script tool.
-
Create a topic named
log_topic../bin/kafka-topics.sh --bootstrap-server <lindorm-stream-endpoint> --topic log_topic --createTo get the endpoint, in the left-side navigation pane of the Lindorm console, click Database Connection. On the Stream Engine tab, find the VPC endpoint in the Lindorm streaming engine endpoint for Kafka section. The format is similar to
ld-xxx-proxy-stream.lindorm.rds.aliyuncs.com:30080. Replace<lindorm-stream-endpoint>in the command with this address. -
Write test data to the topic. Press
Ctrl+Cto stop writing../bin/kafka-console-producer.sh --bootstrap-server <lindorm-stream-endpoint> --topic log_topic {"uID": "A", "x":"-74.00035", "y": "40.72432"} {"uID": "B", "x":"-74.00239", "y": "40.71692"} {"uID": "C", "x":"-74.00201", "y": "40.72563"} {"uID": "D", "x":"-74.00158", "y": "40.72412"} {"uID": "E", "x":"-73.99836", "y": "40.71588"} {"uID": "F", "x":"-74.01015", "y": "40.71422"} {"uID": "G", "x":"-73.99183", "y": "40.71451"} {"uID": "H", "x":"-73.99595", "y": "40.71773"}You can run the
./bin/kafka-console-consumer.sh --bootstrap-server <lindorm-stream-endpoint> --topic log_topic --from-beginningcommand to verify that the data was written.
Step 3: Submit a computing job
Use Flink SQL to submit a computing job that reads data from the topic and processes it with the geofence data.
-
Install the Stream Engine client.
-
On the ECS instance, run the following command to download the Stream Engine client package.
wget https://hbaseuepublic.oss-cn-beijing.aliyuncs.com/lindorm-sqlline-2.0.2.tar.gz -
Run the following command to decompress the package.
tar zxvf lindorm-sqlline-2.0.2.tar.gz -
Go to the
lindorm-sqlline-2.0.2/bindirectory and run the following command to connect to the Lindorm Stream Engine../lindorm-sqlline -url <lindorm-stream-sql-endpoint>
-
-
Submit the computing job.
The computing job performs the following steps:
-
Load the
ganosfunction module. -
In the Flink job, create a source table
carData, a dimension tableregions, and a result tablefresult. Connector parameters associate these tables with the Kafka topic, the geofence tableregions, and the result tablefresult, respectively. -
A streaming job uses the
ST_Containsspatial relationship function to determine the geofence for each real-time data point and writes the result to thefresulttable.
The following is the sample code:
CREATE FJOB fenceFilter ( LOAD MODULE ganos; -- Enable parallelism SET 'parallelism.default'='12'; -- Create stream table CREATE TABLE carData(`uID` STRING, `x` DOUBLE, `y` DOUBLE, `proctime` AS PROCTIME() ) WITH ( 'connector'='kafka', 'topic'='log_topic', 'scan.startup.mode'='earliest-offset', 'properties.bootstrap.servers'='<lindorm-stream-endpoint>', 'format'='json' ); -- Create area table CREATE TABLE regions ( `rID` INT, `rName` STRING, `fence` GEOMETRY, PRIMARY KEY (`rID`) NOT ENFORCED ) WITH ( 'connector'='lindorm', 'seedServer'='<hbase-api-address>', 'userName'='<your-username>', 'password'='<your-password>', 'tableName'='regions', 'namespace'='default' ); -- Create result table CREATE TABLE fresult ( `uID` STRING, `rName` STRING, `rID` INT, PRIMARY KEY (`uID`) NOT ENFORCED ) WITH ( 'connector'='lindorm', 'seedServer'='<hbase-api-address>', 'userName'='<your-username>', 'password'='<your-password>', 'tableName'='fresult', 'namespace'='default' ); -- Find the area that car located INSERT INTO fresult SELECT A.uID, B.rName, B.rID FROM carData AS A JOIN regions /*+ OPTIONS('geomHint'='fence:st_contains','geomIndex'='true','cacheTTLMs'='1800000') */ FOR SYSTEM_TIME AS OF A.proctime AS B ON B.fence=ST_MakePoint(A.x,A.y); );To get the required credentials, click Database Connection in the left-side navigation pane of the Lindorm console. At the top of the page, you can find the Default Username (root) and Default Initial Password. Use these values for the
userNameandpasswordparameters. For theseedServerparameter, use the VPC endpoint from the HBase-compatible address section. This endpoint is the Lindorm wide-table HBase Java API access address. -
Step 4: View the results
-
Connect to and use the Wide Table Engine by using Lindorm-cli.
-
Run the following statement to view the results.
SELECT * FROM fresult;Result:
+-----+-----------+-----+ | uID | rName | rID | +-----+-----------+-----+ | A | SoHo | 1 | | B | Chinatown | 2 | | C | SoHo | 1 | | D | SoHo | 1 | | E | Chinatown | 2 | | F | Tribeca | 3 | | G | Chinatown | 2 | | H | Chinatown | 2 | +-----+-----------+-----+The results show the area for each vehicle. For example, vehicle A is in SoHo (area 1).