C# Native API

Environment Requirements

  • .NET SDK >= 5.0 or .NET Framework 4.x
  • Thrift >= 0.14.1
  • NLog >= 4.7.9

Dependency Installation

You can use NuGet Package Manager, .NET CLI, or other tools to install the required packages.

Using .NET CLI:

If you are using .NET 5.0 or later, run the following command to install the latest NuGet package

dotnet add package Apache.IoTDB

For .NET Framework 4.x, we provide a separate NuGet package. Run

dotnet add package Apache.IoTDB.framework

To install an older version, specify the version explicitly

# Install v0.12.1.2
dotnet add package Apache.IoTDB --version 0.12.1.2

Quick Start

// Define parameters  
string host = "localhost";  
int port = 6667;  
int pool_size = 2;  

// Initialize session pool  
var session_pool = new SessionPool(host, port, pool_size);  

// Open session  
await session_pool.Open(false);  

// Create time series  
await session_pool.CreateTimeSeries("root.test_group.test_device.ts1", TSDataType.TEXT, TSEncoding.PLAIN, Compressor.UNCOMPRESSED);  
await session_pool.CreateTimeSeries("root.test_group.test_device.ts2", TSDataType.BOOLEAN, TSEncoding.PLAIN, Compressor.UNCOMPRESSED);  
await session_pool.CreateTimeSeries("root.test_group.test_device.ts3", TSDataType.INT32, TSEncoding.PLAIN, Compressor.UNCOMPRESSED);  

// Insert a record  
var measures = new List<string>{"ts1", "ts2", "ts3"};  
var values = new List<object> { "test_text", true, (int)123 };  
var timestamp = 1;  
var rowRecord = new RowRecord(timestamp, values, measures);  
await session_pool.InsertRecordAsync("root.test_group.test_device", rowRecord);  

// Insert a tablet  
var timestamp_lst = new List<long>{ timestamp + 1 };  
var value_lst = new List<object> {"iotdb", true, (int) 12};  
var tablet = new Tablet("root.test_group.test_device", measures, value_lst, timestamp_lst);  
await session_pool.InsertTabletAsync(tablet);  

// Close session  
await session_pool.Close();

Full API Reference

SessionPoolis a thread-safe connection pool that supports concurrent client requests. When pool_size=1, it behaves like a single session.

Basic API

MethodParametersDescriptionExample
OpenboolOpen sessionsession_pool.Open(false)
ClosenullClose sessionsession_pool.Close()
IsOpennullCheck session statussession_pool.IsOpen()
OpenDebugModeLoggingConfiguration=nullEnable debug modesession_pool.OpenDebugMode()
CloseDebugModenullDisable debug modesession_pool.CloseDebugMode()
SetTimeZonestringSet timezonesession_pool.GetTimeZone()
GetTimeZonenullGet timezonesession_pool.GetTimeZone()

Metadata API

MethodParametersDescriptionExample
SetStorageGroupstringCreate a storage groupsession_pool.SetStorageGroup(“root.97209_TEST_CSHARP_CLIENT_GROUP_01”)
CreateTimeSeriesstring, TSDataType, TSEncoding, CompressorCreate a time seriessession_pool.InsertTabletsAsync(tablets)
CreateMultiTimeSeriesAsyncList, List , List , ListCreate multiple time seriessession_pool.CreateMultiTimeSeriesAsync(ts_path_lst, data_type_lst, encoding_lst, compressor_lst);
CheckTimeSeriesExistsAsyncstringCheck if a time series existssession_pool.CheckTimeSeriesExistsAsync(time series)

Write API

IoTDB C# client supports RowRecord (single-row) and Tablet (batch) writing

  • RowRecord: For single-row insertion.
var rowRecord = 
  new RowRecord(long timestamps, List<object> values, List<string> measurements);
  • Tablet: For batch insertion (device + multiple rows).
var tablet = 
  new Tablet(string deviceId,  List<string> measurements, List<List<object>> values, List<long> timestamps);

Record

MethodParametersDescriptionExample
InsertRecordAsyncstring, RowRecordInsert a single recordsession_pool.InsertRecordAsync(“root.97209_TEST_CSHARP_CLIENT_GROUP.TEST_CSHARP_CLIENT_DEVICE”, new RowRecord(1, values, measures));
InsertRecordsAsyncList, ListInsert multiple recordssession_pool.InsertRecordsAsync(device_id, rowRecords)
InsertRecordsOfOneDeviceAsyncstring, ListInsert records for one devicesession_pool.InsertRecordsOfOneDeviceAsync(device_id, rowRecords)
InsertRecordsOfOneDeviceSortedAsyncstring, ListInsert sorted records for one devicesession_pool.InsertRecordsOfOneDeviceSortedAsync(deviceId, sortedRowRecords);
TestInsertRecordAsyncstring, RowRecordTest record insertionsession_pool.TestInsertRecordAsync(“root.97209_TEST_CSHARP_CLIENT_GROUP.TEST_CSHARP_CLIENT_DEVICE”, rowRecord)
TestInsertRecordsAsyncList, ListTest record insertionsession_pool.TestInsertRecordsAsync(device_id, rowRecords)

Tablet

MethodParametersDescriptionExample
InsertTabletAsyncTabletInsert a single tabletsession_pool.InsertTabletAsync(tablet)
InsertTabletsAsyncListInsert multiple tabletssession_pool.InsertTabletsAsync(tablets)
TestInsertTabletAsyncTabletTest tablet insertionsession_pool.TestInsertTabletAsync(tablet)
TestInsertTabletsAsyncListTest tablet insertionsession_pool.TestInsertTabletsAsync(tablets)

Query API

MethodParametersDescriptionExample
ExecuteQueryStatementAsyncstringExecute a querysession_pool.ExecuteQueryStatementAsync(“select * from root.97209_TEST_CSHARP_CLIENT_GROUP.TEST_CSHARP_CLIENT_DEVICE where time<15”);
ExecuteNonQueryStatementAsyncstringExecute a non-query statementsession_pool.ExecuteNonQueryStatementAsync( “create timeseries root.97209_TEST_CSHARP_CLIENT_GROUP.TEST_CSHARP_CLIENT_DEVICE.status with datatype=BOOLEAN,encoding=PLAIN”)

Delete API

MethodParametersDescriptionExample
DeleteStorageGroupAsyncstringDelete a storage groupsession_pool.DeleteStorageGroupAsync(“root.97209_TEST_CSHARP_CLIENT_GROUP_01”)
DeleteStorageGroupsAsyncListDelete storage groupssession_pool.DeleteStorageGroupAsync(“root.97209_TEST_CSHARP_CLIENT_GROUP”)
DeleteTimeSeriesAsyncListDelete time seriessession_pool.DeleteTimeSeriesAsync(ts_path_lst)
DeleteTimeSeriesAsyncstringDelete time seriessession_pool.DeleteTimeSeriesAsync(ts_path)
DeleteDataAsyncList, long, longDelete datasession_pool.DeleteDataAsync(ts_path_lst, 2, 3)

Sample Code

For complete examples, refer to: Apache.IoTDB.Samples