Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Introduction to Ethereum Data Importer

DZone's Guide to

Introduction to Ethereum Data Importer

Ethereum is popular for creating decentralized applications and running smart contracts. However, it can have trouble performing complex analytical queries.

· Big Data Zone
Free Resource

Access NoSQL and Big Data through SQL using standard drivers (ODBC, JDBC, ADO.NET). Free Download 

Ethereum is one of the popular blockchain platforms that runs smart contracts. It is used by many companies to create dapps (decentralized applications).

Query Ethereum Public Data

Libraries like Web3j can be used to fetch blocks and transactions and perform basic operations. But complex analytical queries like find top five miners in a particular block range can't be performed. 

One possible solution could be to save blocks and transactions data in another database and perform complicated queries there.

Kundera With Ethereum

The Kundera-Ethereum module can be used to import block data to any Kundera-supported database and also perform JPA queries over it.

Kundera uses Web3j under the hood to fetch block data and the JPA layer to store the data.

Add dependency:

<dependency>
     <groupId>com.impetus.kundera.client</groupId>
     <artifactId>kundera-ethereum</artifactId>
     <version>${kundera.version}</version>
</dependency>

Define kundera-ethereum.properties file in the classpath.

Sample file to store data in MongoDB:

database.type=mongodb
database.host=localhost
database.port=27017
database.name=EthereumDB

## generate Block and Transaction tables 
schema.auto.generate=true

## Drop existing tables
schema.drop.existing=true

## RPC HTTP end point or IPC socket file location or infura end point can be specified
ethereum.node.endpoint=http://localhost:8545/

All set! Start importing blocks now.

Import Data

Import all the data starting from the genesis block:

BlockchainImporter importer = BlockchainImporter.initialize();
importer.importUptoLatestBlock();

Import the data from block 1,000,000 block 2,000,000th:

BlockchainImporter importer = BlockchainImporter.initialize();
importer.importBlocks(BigInteger.valueOf(1000000), BigInteger.valueOf(2000000));

Query Data

We'll consider database-specific queries and JPA queries.

Database-Specific Queries

For example, let's find the top five miners with the number of blocks mined.

db.Block.aggregate([
{$group:{_id:"$miner", numBlocksMined: { $sum: 1}}},
{ $sort : { numBlocksMined: -1 }},
{ $limit : 5 }
]);

JPA queries

Let's find gas and gasPricefor a particular user in a particular block.

Query query = em.createQuery(
            "Select t.gas,t.gasPrice from Transaction t where t.blockNumber='0x455a56' and t.from='0x6c15291028d082e1b9358e19f15c83b9c54f2b35'");
List<Transaction> results = query.getResultList();


For more details check Kundera with Ethereum Blockchain.


The fastest databases need the fastest drivers - learn how you can leverage CData Drivers for high performance NoSQL & Big Data Access.

Topics:
kundera ,ethereum ,blockchain ,nosql ,mongodb ,big data ,data analytics

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}