1

I want to connect mongodb database with AWS Kinesis Stream. I read my database using bellow code:

// To connect to mongodb server  
MongoClient mongoClient = new MongoClient( "localhost" , 27017 );

// Now connect to your databases
DB db = mongoClient.getDB( "test" );
System.out.println("Connect to database successfully");

DBCollection coll1 = db.getCollection("appopens");
System.out.println("Collection mycol selected successfully");

DBCursor cursor = coll1.find();

In order to write this result into Kinesis stream I use bellow code segment :

int i = 0;
while(true) 
{         
  PutRecordsRequestEntry putRecordsRequestEntry  = new PutRecordsRequestEntry();
  putRecordsRequestEntry.setData(cursor.toString());              
  putRecordsRequestEntry.setPartitionKey(String.format("partitionKey-%d", i));
  putRecordsRequestEntryList.add(putRecordsRequestEntry); 

  putRecordsRequest.setRecords(putRecordsRequestEntryList);
  PutRecordsResult putRecordsResult  = client.putRecords(putRecordsRequest);
  System.out.println("Put Result" + putRecordsResult); 
  i++;
} 

But it's seems not working. My program is stop after displaying bellow parse

Jul 25, 2017 3:50:21 PM com.mongodb.diagnostics.logging.JULLogger log

INFO: Opened connection [connectionId{localValue:2, serverValue:55}] to localhost:27017

java.util.NoSuchElementException

at com.mongodb.DBCursor.next(DBCursor.java:163)

at com.amazonaws.samples.AmazonKinesisApplicationSample.writeToStream (AmazonKinesisApplicationSample.java:150)

at com.amazonaws.samples.Demo.main(Demo.java:10)

I assumed error with the bellow code

putRecordsRequestEntry.setData(cursor.toString());

But I can't understand the exact problem. Can someone help me?

GihanDB
  • 591
  • 2
  • 6
  • 23
  • You would probably be better off going from [`Iterable` to `Stream`](https://stackoverflow.com/questions/23932061/convert-iterable-to-stream-using-java-8-jdk). But for some reason you are mixing legacy classes with more modern implementations. I would therefore rather go to `Iterable` since it actually is supported in modern drivers. – Neil Lunn Jul 25 '17 at 05:29

0 Answers0