The CloudTable OpenTSDB and MRS OpenTSDB can be connected to DLI as data sources.
A datasource connection has been created on the DLI management console.
Hard-coded or plaintext passwords pose significant security risks. To ensure security, encrypt your passwords, store them in configuration files or environment variables, and decrypt them when needed.
1 2 3 | from __future__ import print_function from pyspark.sql.types import StructType, StructField, StringType, LongType, DoubleType from pyspark.sql import SparkSession |
1 | sparkSession = SparkSession.builder.appName("datasource-opentsdb").getOrCreate() |
1 2 3 4 | sparkSession.sql("create table opentsdb_test using opentsdb options( 'Host'='opentsdb-3xcl8dir15m58z3.cloudtable.com:4242', 'metric'='ct_opentsdb', 'tags'='city,location')") |
sparkSession.sql("insert into opentsdb_test values('aaa', 'abc', '2021-06-30 18:00:00', 30.0)")
result = sparkSession.sql("SELECT * FROM opentsdb_test")
1 2 3 4 | schema = StructType([StructField("location", StringType()),\ StructField("name", StringType()), \ StructField("timestamp", LongType()),\ StructField("value", DoubleType())]) |
1 | dataList = sparkSession.sparkContext.parallelize([("aaa", "abc", 123456L, 30.0)]) |
1 | dataFrame = sparkSession.createDataFrame(dataList, schema) |
1 | dataFrame.write.insertInto("opentsdb_test") |
1 2 3 4 5 6 7 | jdbdDF = sparkSession.read .format("opentsdb")\ .option("Host","opentsdb-3xcl8dir15m58z3.cloudtable.com:4242")\ .option("metric","ctopentsdb")\ .option("tags","city,location")\ .load() jdbdDF.show() |
spark.driver.extraClassPath=/usr/share/extension/dli/spark-jar/datasource/opentsdb/*
spark.executor.extraClassPath=/usr/share/extension/dli/spark-jar/datasource/opentsdb/*
# _*_ coding: utf-8 _*_
from __future__ import print_function
from pyspark.sql.types import StructType, StructField, StringType, LongType, DoubleType
from pyspark.sql import SparkSession
if __name__ == "__main__":
# Create a SparkSession session.
sparkSession = SparkSession.builder.appName("datasource-opentsdb").getOrCreate()
# Create a DLI cross-source association opentsdb data table
sparkSession.sql(\
"create table opentsdb_test using opentsdb options(\
'Host'='10.0.0.171:4242',\
'metric'='cts_opentsdb',\
'tags'='city,location')")
sparkSession.sql("insert into opentsdb_test values('aaa', 'abc', '2021-06-30 18:00:00', 30.0)")
result = sparkSession.sql("SELECT * FROM opentsdb_test")
result.show()
# close session
sparkSession.stop()
# _*_ coding: utf-8 _*_
from __future__ import print_function
from pyspark.sql.types import StructType, StructField, StringType, LongType, DoubleType
from pyspark.sql import SparkSession
if __name__ == "__main__":
# Create a SparkSession session.
sparkSession = SparkSession.builder.appName("datasource-opentsdb").getOrCreate()
# Create a DLI cross-source association opentsdb data table
sparkSession.sql(
"create table opentsdb_test using opentsdb options(\
'Host'='opentsdb-3xcl8dir15m58z3.cloudtable.com:4242',\
'metric'='ct_opentsdb',\
'tags'='city,location')")
# Create a DataFrame and initialize the DataFrame data.
dataList = sparkSession.sparkContext.parallelize([("aaa", "abc", 123456L, 30.0)])
# Setting schema
schema = StructType([StructField("location", StringType()),\
StructField("name", StringType()),\
StructField("timestamp", LongType()),\
StructField("value", DoubleType())])
# Create a DataFrame from RDD and schema
dataFrame = sparkSession.createDataFrame(dataList, schema)
# Set cross-source connection parameters
metric = "ctopentsdb"
tags = "city,location"
Host = "opentsdb-3xcl8dir15m58z3.cloudtable.com:4242"
# Write data to the cloudtable-opentsdb
dataFrame.write.insertInto("opentsdb_test")
# ******* Opentsdb does not currently implement the ctas method to save data, so the save() method cannot be used.*******
# dataFrame.write.format("opentsdb").option("Host", Host).option("metric", metric).option("tags", tags).mode("Overwrite").save()
# Read data on CloudTable-OpenTSDB
jdbdDF = sparkSession.read\
.format("opentsdb")\
.option("Host",Host)\
.option("metric",metric)\
.option("tags",tags)\
.load()
jdbdDF.show()
# close session
sparkSession.stop()