88using System . Data . Common ;
99using System . Linq ;
1010using System . Threading ;
11+ using System . Threading . Tasks ;
1112
1213namespace TaosADODemo
1314{
@@ -21,7 +22,7 @@ static void Main(string[] args)
2122 string database = "db_" + DateTime . Now . ToString ( "yyyyMMddHHmmss" ) ;
2223 var builder = new TaosConnectionStringBuilder ( )
2324 {
24- DataSource = "airleaderserver " ,
25+ DataSource = "taos " ,
2526 DataBase = database ,
2627 Username = "root" ,
2728 Password = "taosdata" ,
@@ -61,7 +62,7 @@ static void Main(string[] args)
6162 Console . WriteLine ( "" ) ;
6263 ConsoleTableBuilder . From ( reader . ToDataTable ( ) ) . WithFormat ( ConsoleTableBuilderFormat . MarkDown ) . ExportAndWriteLine ( ) ;
6364
64-
65+ connection . ChangeDatabase ( database ) ;
6566 Console . WriteLine ( "" ) ;
6667 connection . CreateCommand ( $ "CREATE TABLE datas ('reportTime' timestamp, type int, 'bufferedEnd' bool, address nchar(64), parameter nchar(64), value nchar(64)) TAGS ('boxCode' nchar(64), 'machineId' int);") . ExecuteNonQuery ( ) ;
6768 connection . CreateCommand ( $ "INSERT INTO data_history_67 USING datas TAGS (mongo, 67) values ( 1608173534840 2 false 'Channel1.窑.烟囱温度' '烟囱温度' '122.00' );") . ExecuteNonQuery ( ) ;
@@ -84,8 +85,15 @@ static void Main(string[] args)
8485 connection . CreateCommand ( "CREATE TABLE IF NOT EXISTS telemetrydata (ts timestamp,value_type tinyint, value_boolean bool, value_string binary(10240), value_long bigint,value_datetime timestamp,value_double double) TAGS (deviceid binary(32),keyname binary(64));" ) . ExecuteNonQuery ( ) ;
8586 var devid1 = $ "{ Guid . NewGuid ( ) : N} ";
8687 var devid2 = $ "{ Guid . NewGuid ( ) : N} ";
87- UploadTelemetryData ( connection , devid1 , "1#air-compressor-two-level-discharge-temperature" , 2000 ) ;
88- UploadTelemetryData ( connection , devid2 , "1#air-compressor-load-rate" , 2000 ) ;
88+ DateTime dt = DateTime . Now ;
89+ UploadTelemetryData ( connection , devid1 , "1#air-compressor-two-level-discharge-temperature" , 4000 ) ;
90+ UploadTelemetryData ( connection , devid2 , "1#air-compressor-load-rate" , 4000 ) ;
91+
92+ DateTime dt2 = DateTime . Now ;
93+ UploadTelemetryDataPool ( connection , devid1 , "1#air-compressor-two-level-discharge-temperature1" , 4000 ) ;
94+ UploadTelemetryDataPool ( connection , devid2 , "1#air-compressor-load-rate1" , 4000 ) ;
95+ Console . WriteLine ( $ "UploadTelemetryData 耗时:{ DateTime . Now . Subtract ( dt ) . TotalSeconds } ") ;
96+ Console . WriteLine ( $ "UploadTelemetryDataPool 耗时:{ DateTime . Now . Subtract ( dt2 ) . TotalSeconds } ") ;
8997 var reader2 = connection . CreateCommand ( "select last_row(*) from telemetrydata group by deviceid,keyname ;" ) . ExecuteReader ( ) ;
9098 ConsoleTableBuilder . From ( reader2 . ToDataTable ( ) ) . WithFormat ( ConsoleTableBuilderFormat . Default ) . ExportAndWriteLine ( ) ;
9199 var reader3 = connection . CreateCommand ( "select * from telemetrydata" ) . ExecuteReader ( ) ;
@@ -120,7 +128,7 @@ static void Main(string[] args)
120128 string [ ] jsonStr = {
121129 "{"
122130 + "\" metric\" : \" stb0_0\" ,"
123- + "\" timestamp\" : 1626006833 ,"
131+ + $ "\" timestamp\" : { DateTimeOffset . Now . ToUnixTimeSeconds ( ) } ,"
124132 + "\" value\" : 10,"
125133 + "\" tags\" : {"
126134 + " \" t1\" : true,"
@@ -169,7 +177,7 @@ static JObject AddTag(JObject tags, string name, object value, string type)
169177 payload . Add ( "metric" , "stb3_0" ) ;
170178
171179 var timestamp = new JObject ( ) ;
172- timestamp . Add ( "value" , 1626006833 ) ;
180+ timestamp . Add ( "value" , DateTimeOffset . Now . ToUnixTimeSeconds ( ) ) ;
173181 timestamp . Add ( "type" , "s" ) ;
174182 payload . Add ( "timestamp" , timestamp ) ;
175183
@@ -245,5 +253,13 @@ static void UploadTelemetryData( TaosConnection connection, string devid, strin
245253 connection . CreateCommand ( $ "INSERT INTO device_{ devid } USING telemetrydata TAGS(\" { devid } \" ,\" { keyname } \" ) values (now,2,true,'{ i } ',{ i } ,now,{ i } );") . ExecuteNonQuery ( ) ;
246254 }
247255 }
256+
257+ static void UploadTelemetryDataPool ( TaosConnection connection , string devid , string keyname , int count )
258+ {
259+ Parallel . For ( 0 , count , new ParallelOptions ( ) { MaxDegreeOfParallelism = connection . PoolSize } , i =>
260+ {
261+ connection . CreateCommand ( $ "INSERT INTO device_{ devid } USING telemetrydata TAGS(\" { devid } \" ,\" { keyname } \" ) values (now,2,true,'{ i } ',{ i } ,now,{ i } );") . ExecuteNonQuery ( ) ;
262+ } ) ;
263+ }
248264 }
249265}
0 commit comments