Skip to main content

File Storage Overview

The FILE_STORAGE route writes MQTT messages to local CSV or JSON files. It provides simple persistence for logging, backups, and data export without requiring a database server.
File storage is ideal for simple logging, local backups, and exporting data for offline analysis. Use it when you don’t need a full database but want persistent records.

Basic Syntax

DEFINE ROUTE DataLogger WITH TYPE FILE_STORAGE
    ADD FILE_STORAGE_CONFIG
        WITH STORAGE_DIR "/data/logs"
        WITH FORMAT "CSV"
        WITH FILE_PATTERN "{0}_{date}.csv"
        WITH APPEND "true"
    ADD EVENT LogSensorData
        WITH SOURCE_TOPIC "sensors/#"
        WITH QUERY "CLEAN:{table: sensor_data, data: {value.json}}"

Connection Configuration

FILE_STORAGE_CONFIG Parameters

STORAGE_DIR
string
required
Directory path for storing files.
FILE_PATTERN
string
File name pattern. Use {0} for table name, {date} for current date. Default: {0}.csv
FORMAT
string
File format: CSV or JSON. Default: CSV.
CSV_DELIMITER
string
CSV delimiter character. Default: comma (,).
APPEND
boolean
Append to existing files. Default: false (overwrite).
INCLUDE_TIMESTAMP
boolean
Auto-add timestamp field. Default: false.
MAX_RECORDS
integer
Maximum records per file before rotation. Default: 10000.
DATE_FORMAT
string
Timestamp format string. Default: yyyy-MM-dd HH:mm:ss.

CLEAN Query Format

File storage routes use the CLEAN format:
WITH QUERY "CLEAN:{table: <name>, data: {value.json}}"
The table name is used in the file pattern with {0}.

Complete Examples

Log sensor data to daily CSV files:
DEFINE ROUTE CSVLogger WITH TYPE FILE_STORAGE
    ADD FILE_STORAGE_CONFIG
        WITH STORAGE_DIR "/data/logs"
        WITH FORMAT "CSV"
        WITH FILE_PATTERN "{0}_{date}.csv"
        WITH APPEND "true"
        WITH CSV_DELIMITER ","
        WITH INCLUDE_TIMESTAMP "true"
        WITH MAX_RECORDS "10000"
    ADD EVENT LogSensorData
        WITH SOURCE_TOPIC "sensors/#"
        WITH DESTINATION_TOPIC "file/status"
        WITH QUERY "CLEAN:{table: sensor_data, data: {value.json}}"
Creates files like: sensor_data_2025-01-15.csv

File Pattern Placeholders

PlaceholderDescriptionExample
{0}Table name from CLEAN querysensor_data
{date}Current date (yyyy-MM-dd)2025-01-15

Output Examples

CSV Output

timestamp,sensor_id,temperature,humidity
2025-01-15 10:30:00,temp001,23.5,65
2025-01-15 10:30:05,temp002,24.1,62

JSON Output (JSON Lines format)

{"timestamp":"2025-01-15T10:30:00Z","sensor_id":"temp001","temperature":23.5,"humidity":65}
{"timestamp":"2025-01-15T10:30:05Z","sensor_id":"temp002","temperature":24.1,"humidity":62}

Troubleshooting

  • Verify STORAGE_DIR exists
  • Check write permissions on directory
  • Ensure Coreflux process has access
  • Verify STORAGE_DIR path is correct
  • Check disk space is available
  • Ensure directory is writable
  • Verify SOURCE_TOPIC matches incoming messages
  • Check QUERY syntax is correct
  • Verify APPEND is set correctly
  • Check MAX_RECORDS is set appropriately
  • Verify FILE_PATTERN includes {date} for daily rotation

Next Steps