Sandeepan Nath
Sandeepan Nath

Reputation: 10284

Unable to read one single record from aerospike set using index other than primary key

Following is code inside my DaoImpl class file -

@Component
public class RowDetailsDaoImpl implements RowDetailsDao{
Logger logger = LoggerFactory.getLogger(getClass());

@Autowired
private AerospikeConfiguration aeroconf;

@Autowired
private Spikeify sfy;

@Autowired
SequenceIdGenerator sequenceIdGenerator;

@Autowired
private AerospikeClient client;

private final String setName = "row_transaction_details";



@Override
public RowDetails getRowDetailsUsingTransactionId(Long transactionId) {

    Key key = new Key(aeroconf.getHistoryNamespace(), setName, transactionId);
    try {
        return sfy.get(RowDetails.class).key(key).now();
    } catch (Exception e) {
        logger.error("Error fetching Row Details ", e);
    }
    return null;
}
}

Calling it from my application logic is returning null -

RowDetails rowDetails = rowDetailsDao.getRowDetailsUsingTransactionId(1622831l);

However, fetching entry by PK is working -

@Override
    public RoqDetails getRowDetailsUsingPk(Long pK) {
        Key key = new Key(aeroconf.getHistoryNamespace(), setName, pK);
        try {
            return sfy.get(RowDetails.class).key(key).now();
        } catch (Exception e) {
            logger.error("Error fetching Row Details ", e);
        }
        return null;
    }

This call is working -

RowDetails rowDetailsHard = rowDetailsDao.getRowDetailsUsingPk(150l);

Following is sample data and necessary index -

insert into dummy.row_transaction_details (PK,transactionId) values(150,1622831)
CREATE INDEX row_transaction_details_transactionId ON dummy.row_transaction_details (transactionId) NUMERIC

Upvotes: 0

Views: 220

Answers (1)

pgupta
pgupta

Reputation: 5415

You cannot do that - syntactically. Aerospike cannot fetch a record using a secondary index as a primary key. Secondary index can only be used to filter a record. What you will have to do is issue a secondary index query with equality filter on transactionId.

Following your AQL example, I repeated your commands (my namespace is 'ns1' instead of 'dummy'):

aql> insert into ns1.row_transaction_details (PK,transactionId) values(150,1622831)
OK, 1 record affected.

aql> CREATE INDEX row_transaction_details_transactionId ON ns1.row_transaction_details (transactionId) NUMERIC
OK, 1 index added.

aql> select * from ns1.row_transaction_details where transactionId = 1622831
+---------------+
| transactionId |
+---------------+
| 1622831       |
+---------------+
1 row in set (0.001 secs)

OK

Upvotes: 3

Related Questions