Prerequisites
- Basic understanding of Hedera Mirror Nodes.
- Basic understanding of terminal commands and SQL.
- Java (openjdk@17: Java version 17), Gradle (the latest version), and PostgreSQL (the latest version) are installed on your machine.
- Docker (
>= v20.10.x)installed and open on your machine. Rundocker -vin your terminal to check the version you have installed.
Table of Contents
Set Up Mirror Node
Clone the Hedera mirror node repository and navigate to the project directory:Note: Cloning the mirror node repository could require some time to
complete.
Configure Mirror Node
In this example, we will configure the mirror node to store the last 90 days of data or data for a specific account ID (entity). To achieve this, we will need to create and modify anapplication.yml file. This file contains configuration settings for the mirror node, such as which data to store and how long to store it.
First, create a new configuration folder and file inside the hedera-mirror-importer directory. The Mirror Node importer directory contains the source code for the importer tool, which allows users to import data from the Hedera Mainnet, Testnet, or Previewnet. This creates and imports a read-only instance of the Hedera network data stored in its own database.
Run the following command to create the right folder and file:
Transaction and entity filtering
The mirror node may be configured only to store a subset of data for entities and/or transaction types of interest — essentially, which rows of data to retain. In this example, we’ll use theapplication.yml format for demonstration purposes. This configuration retains transaction and crypto transfer data for 90 days, excludes data for entity 0.0.111478, and includes specific transactions for entities 0.0.111710 and 0.0.111734. Furthermore, it prevents the storage of topic data. You can check out the other two alternative formats here if you don’t like working with the YAML format.
Breaking down application.yml configuration
Here’s an overview theapplication.yml file. Copy the following lines into application.yml and save it.
application.yml
behaviorhedera: This is the root section of the configuration file, indicating that the settings apply to the Hedera network.mirror: This is a sub-section that pertains specifically to the Mirror node.importer: This sub-section defines settings for the Mirror node’s importer, which is responsible for retrieving transaction data from the network and storing it in a local database for querying.importer.network: DEMO: This specifies that the importer should connect to a bucket with demo data. It’s the easiest way to experiment with the mirror node and importer. If you want to connect to theTESTNET,MAINNET, orPREVIEWNET, you need to follow this tutorial.importer.retention: This sub-section specifies the retention period and frequency for importing data. In this case, the importer will clean data that is older than 90 days every 60 seconds. If you omit thefrequencykey, the default behavior for cleaning data is once a day.importer.retention.include: This specifies the database tables that should be included in the imported data. The tables specified aretransactionandcrypto_transfer. You can find all tables in the GitHub repository for the mirror node.parser: This sub-section defines settings for the data parser, which determines the data that gets stored in the database or the data that should be filtered.parser.exclude: This specifies the entities or transaction types that should be excluded from the imported data. In this case, theparser.exclude.entitywith ID0.0.111478is excluded.parser.include: This specifies the entities or transaction types that should be included from the imported data. In this case, theparser.include.entitywith ID0.0.111478is included, and two specific transaction types (CRYPTOTRANSFERandCRYPTOCREATEACCOUNT) are included viaparser.include.transaction.
You can also combineentityandtransactionfields. In our example, we only want to storeCONTRACTCREATEINSTANCEtransactions for the entity with ID0.0.111710.parser.record: This sub-section specifies how the imported data should be recorded. In this case, theentityobject is specified, which means that data should be recorded for each unique entity (account) involved in the transactions. Thepersistsetting is set tofalse, which means that topic data for entities should not be persisted.period: 90D: This indicates that the importer should retain the imported data for a period of 90 days. After this period, the data will be deleted.
Note: The
parser.exclude properties take priority over the parser.include properties. If you list the same value in both lists, it will be excluded.In addition, the various boolean hedera.mirror.importer.record.entity.persist properties may be specified to control which additional fields get stored (which additional tables get recorded).See the hedera.mirror.importer.parser.include._andhedera.mirror.importer.parser.exclude._ properties listed in this table.Alternative configuration formats
Alternative configuration formats
application.propertiesTo configure the above scenario via environment variablesTo configure the above scenario via environmental variables, set the following:
application.properties file, include the following lines:Start Mirror Node
The PostgreSQL container is responsible for creating the database for the mirror node instance, and the REST API container allows you to use the REST APIs to query the mirror node instance. The database stores the transaction data retrieved by the importer component of the mirror node, and the REST API provides an interface for accessing that data using HTTP requests. The importer component is responsible for retrieving the transaction data from the Hedera network and storing it in the database. Let’s start up the database!1. Start the database
Open Docker and start the PostgreSQL and REST API containers in the root directory:control + c to terminate the current process.

2. Run the importer
Now the database is ready, let’s import demo data. Run the importer in the same root directory:control + c. If you let the process run, it will import more data that you don’t need for this tutorial.

3. Connect to the PostgreSQL database
To connect the PostgreSQL database, we need to retrieve the database credentials. Open a new terminal window and run the following command in the root directory. Copy the database password that your console returns. You need this for the next step.Query Mirror Node
In this section, you can try out multiple queries that show you how to retrieve data from the PostgreSQL database. You need a basic understanding of SQL queries to craft your own queries.Most queries include the field
type which refers to a transaction type, e.g. 11 refers toCRYPTOCREATEACCOUNTand 14 refers toCRYPTOTRANSFER. The most common transaction types are:- Type 7:
CONTRACTCALL - Type 11:
CRYPTOCREATEACCOUNT - Type 14:
CRYPTOTRANSFER - Type 24:
CONSENSUSCREATETOPIC - Type 27:
CONSENSUSSUBMITMESSAGE - Type 29:
TOKENCREATION - Type 37:
TOKENMINT - Type 40:
TOKENASSOCIATE
TransactionTypes.java file.Query output verification ✅
Query output verification ✅
Note that your output might differ depending on how much data you have imported into your database with the
./gradlew :importer:bootrun command.The PostgreSQL database is not an end product; the Hedera Mirror Node REST API
is. Hence, SQL queries might fail when the engineering team updates the
underlying database model in new releases. This tutorial has been tested with
release v0.79.1.
psql console, run the quit command: