//
//  Licensed to the Apache Software Foundation (ASF) under one or more
//  contributor license agreements.  See the NOTICE file distributed with
//  this work for additional information regarding copyright ownership.
//  The ASF licenses this file to You under the Apache License, Version 2.0
//  (the "License"); you may not use this file except in compliance with
//  the License.  You may obtain a copy of the License at
//
//      https://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//
:imagesdir: ../../images/
:icons: font

== Apache IotDB

Apache IoTDB is database for storing time serie data.
Therefore, it can be a good solution for managing the data which is collected by PLC4x.


=== Data Model (Concept)

Given a PLC address and some fields, we can consider the PLC as a `device` in IoTDB, and each field
as a `measurement` in IoTDB. A couple of PLCs form a `storage group`.

For example, there is a virtual storage group `mi`, and a PLC `d1`, which has one field `RANDOM/foo:Integer`.
Then, in IoTDB, we can get a time series like `root.mi.d1.foo` (or `root.mi.d1.RANDOM_foo_Integer`, as you like).
Then, we can write data into IoTDB using JDBC with SQL or native API called session API.

=== Example 

https://github.com/apache/plc4x/tree/develop/plc4j/examples/hello-integration-iotdb shows an example
to collect data using PLC4x and then writing data to IoTDB.

To run the java example, some arguments are needed:

e.g., `java -jar .....  --connection-string simulated://127.0.0.1 --field-address RANDOM/foo:Integer  --polling-interval 1000 --iotdb-address 127.0.0.1:6667 --iotdb-user-name root --iotdb-user-password root --iotdb-sg mi --iotdb-device d1 --iotdb-datatype INT32 --use-jdbc false`

Then PLC4x will collect data from a simulated PLC device, which generate random integer per 1 second.
IoTDB's address is 127.0.0.1 and the port is 6667. The data will be stored in device `root.m1.d1`, and the measurement name is RANDOM_foo_Integer.

If you are using IoTDB v0.10.0 with the default configuration, and do not manually create the above time series, then IoTDB will consider the data as float.
So, after running the program a few secondes, you can query the data using IoTDB's command line: `select * from root.mi.d1;`

....
+-----------------------------+-----------------------------+
|                         Time|root.mi.d1.RANDOM_foo_Integer|
+-----------------------------+-----------------------------+
|2020-07-16T20:01:39.216+08:00|                -1.34242598E9|
|2020-07-16T20:01:40.150+08:00|                 -9.4141104E7|
|2020-07-16T20:01:41.154+08:00|                 1.40969664E9|
|2020-07-16T20:01:42.155+08:00|                  9.5824531E8|
|2020-07-16T20:01:43.157+08:00|                  -4.207406E7|
+-----------------------------+-----------------------------+
....
 
The detailed usage about IoTDB can be found https://iotdb.apache.org/UserGuide/Master/Client/Programming%20-%20Native%20API.html