SQL
This is a legacy Apache Ignite documentation
The new documentation is hosted here: https://ignite.apache.org/docs/latest/
SQL Query
Node.JS client fully supports Ignite SQL queries. A query method returns a cursor class that can be used to iterate over a result set lazily or to get the whole result at once.
First, create a query by creating and configuring an instance of the SqlQuery
class.
Then, pass the SqlQuery
instance to the instance of Cache
class and execute the query. Finally, use the Cursor
instance to iterate over or get all cache entries returned by the query.
const IgniteClient = require('apache-ignite-client');
const IgniteClientConfiguration = IgniteClient.IgniteClientConfiguration;
const CacheConfiguration = IgniteClient.CacheConfiguration;
const QueryEntity = IgniteClient.QueryEntity;
const QueryField = IgniteClient.QueryField;
const ObjectType = IgniteClient.ObjectType;
const ComplexObjectType = IgniteClient.ComplexObjectType;
const CacheEntry = IgniteClient.CacheEntry;
const SqlQuery = IgniteClient.SqlQuery;
async function performSqlQuery() {
const igniteClient = new IgniteClient();
try {
await igniteClient.connect(new IgniteClientConfiguration('127.0.0.1:10800'));
// cache configuration required for sql query execution
const cacheConfiguration = new CacheConfiguration().
setQueryEntities(
new QueryEntity().
setValueTypeName('Person').
setFields([
new QueryField('name', 'java.lang.String'),
new QueryField('salary', 'java.lang.Double')
]));
const cache = (await igniteClient.getOrCreateCache('sqlQueryPersonCache', cacheConfiguration)).
setKeyType(ObjectType.PRIMITIVE_TYPE.INTEGER).
setValueType(new ComplexObjectType({ 'name' : '', 'salary' : 0 }, 'Person'));
// put multiple values using putAll()
await cache.putAll([
new CacheEntry(1, { 'name' : 'John Doe', 'salary' : 1000 }),
new CacheEntry(2, { 'name' : 'Jane Roe', 'salary' : 2000 }),
new CacheEntry(3, { 'name' : 'Mary Major', 'salary' : 1500 })]);
// create and configure sql query
const sqlQuery = new SqlQuery('Person', 'salary > ? and salary <= ?').
setArgs(900, 1600);
// obtain sql query cursor
const cursor = await cache.query(sqlQuery);
// getAll cache entries returned by the sql query
for (let cacheEntry of await cursor.getAll()) {
console.log(cacheEntry.getValue());
}
await igniteClient.destroyCache('sqlQueryPersonCache');
}
catch (err) {
console.log(err.message);
}
finally {
igniteClient.disconnect();
}
}
performSqlQuery();
SQL Fields Query
This type of queries is used to obtain individual fields as a part of an SQL query result set, execute DML and DDL statements such as INSERT, UPDATE, DELETE, CREATE and other.
First, define the query by creating and configuring an instance of the SqlFieldsQuery
class. Then, pass the SqlFieldsQuery
to the query method of the Cache
instance and obtain an instance of the SqlFieldsCursor
class. Finally, use the SqlFieldsCursor
instance to iterate over or get all elements returned by the query.
const IgniteClient = require('apache-ignite-client');
const IgniteClientConfiguration = IgniteClient.IgniteClientConfiguration;
const CacheConfiguration = IgniteClient.CacheConfiguration;
const ObjectType = IgniteClient.ObjectType;
const CacheEntry = IgniteClient.CacheEntry;
const SqlFieldsQuery = IgniteClient.SqlFieldsQuery;
async function performSqlFieldsQuery() {
const igniteClient = new IgniteClient();
try {
await igniteClient.connect(new IgniteClientConfiguration('127.0.0.1:10800'));
const cache = await igniteClient.getOrCreateCache('myPersonCache', new CacheConfiguration().
setSqlSchema('PUBLIC'));
// create table using SqlFieldsQuery
(await cache.query(new SqlFieldsQuery(
'CREATE TABLE Person (id INTEGER PRIMARY KEY, firstName VARCHAR, lastName VARCHAR, salary DOUBLE)'))).getAll();
// insert data into the table
const insertQuery = new SqlFieldsQuery('INSERT INTO Person (id, firstName, lastName, salary) values (?, ?, ?, ?)').
setArgTypes(ObjectType.PRIMITIVE_TYPE.INTEGER);
(await cache.query(insertQuery.setArgs(1, 'John', 'Doe', 1000))).getAll();
(await cache.query(insertQuery.setArgs(2, 'Jane', 'Roe', 2000))).getAll();
// obtain sql fields cursor
const sqlFieldsCursor = await cache.query(
new SqlFieldsQuery("SELECT concat(firstName, ' ', lastName), salary from Person").
setPageSize(1));
// iterate over elements returned by the query
do {
console.log(await sqlFieldsCursor.getValue());
} while (sqlFieldsCursor.hasMore());
// drop the table
(await cache.query(new SqlFieldsQuery("DROP TABLE Person"))).getAll();
}
catch (err) {
console.log(err.message);
}
finally {
igniteClient.disconnect();
}
}
performSqlFieldsQuery();
Node.JS example files
Node.JS thin client contains fully workable examples to demonstrate the behavior of the client.
Updated 2 months ago