mvn clean scala:compile compile install

Maven Dependency

spark-shell user guide

  1. spark-shell --jars spark-iotdb-connector-0.12.0.jar,iotdb-jdbc-0.12.0-jar-with-dependencies.jar
  2. import org.apache.iotdb.spark.db._
  3. val df = spark.read.format("org.apache.iotdb.spark.db").option("url","jdbc:iotdb://127.0.0.1:6667/").option("sql","select * from root").load
  4. df.printSchema()
  5. df.show()

Schema Inference

Take the following TsFile structure as an example: There are three Measurements in the TsFile schema: status, temperature, and hardware. The basic information of these three measurements is as follows:

The existing data in the TsFile is as follows:

The wide(default) table form is as follows:

You can also use narrow table form which as follows: (You can see part 4 about how to use narrow form)

Transform between wide and narrow table

  • from wide to narrow
  1. import org.apache.iotdb.spark.db._
  2. val wide_df = spark.read.format("org.apache.iotdb.spark.db").option("url", "jdbc:iotdb://127.0.0.1:6667/").option("sql", "select * from root where time < 1100 and time > 1000").load
  3. val narrow_df = Transformer.toNarrowForm(spark, wide_df)
  • from narrow to wide

Java user guide

  1. import org.apache.spark.sql.Dataset;
  2. import org.apache.spark.sql.Row;
  3. import org.apache.spark.sql.SparkSession;
  4. import org.apache.iotdb.spark.db.*
  5. public class Example {
  6. public static void main(String[] args) {
  7. SparkSession spark = SparkSession
  8. .builder()
  9. .getOrCreate();
  10. Dataset<Row> df = spark.read().format("org.apache.iotdb.spark.db")
  11. .option("url","jdbc:iotdb://127.0.0.1:6667/")
  12. .option("sql","select * from root").load();
  13. df.printSchema();
  14. df.show();
  15. Dataset<Row> narrowTable = Transformer.toNarrowForm(spark, df)
  16. narrowTable.show()
  17. }

Write Data to IoTDB

  1. You can directly write data to IoTDB whatever the dataframe contains a wide table or a narrow table.
  2. The parameter numPartition is used to set the number of partitions. The dataframe that you want to save will be repartition based on this parameter before writing data. Each partition will open a session to write data to increase the number of concurrent requests.