Merge multiple lines of a file in Hadoop basing on another File
===

I am working on a solution to a problem using Hadoop and has been trying to find some help on it for some days. But I could not. Below are the details.

I have two text files. The first file A is a very big one with several million records in it, and the other one B is relatively small in size, say some thousands of records in it. I need to merge the records of A and get concatenated records. For the merge the records of file B would be used. 

I know the above description is not clear and so I would explain it using an example.

Contents of A are
NE_1-S1-1-1111--NE_1-S2-1-2222
NE_1-S1-1-1123--NE_1-S2-1-1222
NE_2-S1-1-1111--NE_2-S2-1-2222
NE_2-S1-1-2322--NE_2-S2-1-1122
NE_3-S1-1-2222--NE_3-S2-1-2222

Contents of B are 
NE_1-S1-1--NE_2-S1-1
NE_2-S2-1--NE_3-S1-1
NE_3-S2-1--NE_1-S2-1

The first record of file A represent a connection in NE_1, between S1-1-1111 and S2-1-2222. The first record of file B represent a link between NE_1 and NE_2 between NE_1-S1-1 and NE_2-S1-1. Now I need to obtain all possible links among NE_1, NE_2 and NE_3 by using the combination of Links between NEs (file B) and internal connections of the NEs (file A).

Correspondingly the output would be 1 record something like
NE_1-S2-1-2222--NE_1-S1-1-1111--NE_2-S1-1-1111--NE_2-S2-1-2222--NE_3-S1-1-2222--NE_3-S2-1-2222

For sample data like the ones shown above I could do the merging in java by caching the records, but with the number of records in a very large number, I could not store the data in memory, and hence I am trying to use hadoop so that I can access the records without caching them them in memory and also the fact that the merge can be faster.

Could you please help me in identifying some algorithm which can be used in hadoop to solve my problem.

Many thanks in Advance.
