2

I am trying to learn DynamoDb and trying loading data from json to table using example in Amazon developer guide but i am getting below exception:

com.fasterxml.jackson.databind.node.IntNode cannot be cast to com.fasterxml.jackson.databind.node.ObjectNode

at below line:

 currentNode = (ObjectNode) iter.next();

Can you please suggest me what i am doing wrong here.

Also can you please let me know is there any way i can use GUI to view my table content. I am using DynamoDbLocal using maven in my project.

Thanks,

Manish
  • 1,274
  • 3
  • 22
  • 59
  • You can use a free account to access DynamoDb directly from the AWS console. This is the easiest solution to start programming because it gives you the ability to see the data using the AWS interface. Leave the local database for when you are more confortable on using it – Davide Lorenzo MARINO May 08 '19 at 08:20

2 Answers2

2

Issue was there in my json file.I was using json in my input file like this:

{ 
"year" : "2013",
"title" : "Turn It Down, Or Else!",
"info" : {
    "directors" : [
        "Alice Smith",
        "Bob Jones"
    ],
    "release_date" : "2013-01-18T00:00:00Z",
    "rating" : "6.2",
    "genres" : [
        "Comedy",
        "Drama"
    ],
    "image_url" : "http://ia.media-imdb.com/images/N/O9ERWAU7FS797AJ7LU8HN09AMUP908RLlo5JF90EWR7LJKQ7@@._V1_SX400_.jpg",
    "plot" : "A rock band plays their music at high volumes, annoying the neighbors.",
    "rank" : "11",
    "running_time_secs" : "5215",
    "actors" : [
        "David Matthewman",
        "Ann Thomas",
        "Jonathan G. Neff"
   ]
 }
}

While my code was expecting it like this way:

[
{  
"year" : "2013",
"title" : "Turn It Down, Or Else!",
"info" : {
    "directors" : [
        "Alice Smith",
        "Bob Jones"
    ],
    "release_date" : "2013-01-18T00:00:00Z",
    "rating" : "6.2",
    "genres" : [
        "Comedy",
        "Drama"
    ],
    "image_url" : "http://ia.media-imdb.com/images/N/O9ERWAU7FS797AJ7LU8HN09AMUP908RLlo5JF90EWR7LJKQ7@@._V1_SX400_.jpg",
    "plot" : "A rock band plays their music at high volumes, annoying the neighbors.",
    "rank" : "11",
    "running_time_secs" : "5215",
    "actors" : [
        "David Matthewman",
        "Ann Thomas",
        "Jonathan G. Neff"
   ]
  }
 }
]
Manish
  • 1,274
  • 3
  • 22
  • 59
1

ObjectNode is not a descendant of IntNode so you can't cast an IntNode to ObjectNode. You need to cast that particular line of code to IntNode:

 IntNode currentNode = (IntNode) iter.next();

or to any superclass of IntNode:

 JsonNode currentNode = (JsonNode) iter.next();

This is the hierarcy of ObjectNode

java.lang.Object*
  com.fasterxml.jackson.databind.JsonSerializable.Base*
    com.fasterxml.jackson.databind.JsonNode*
      com.fasterxml.jackson.databind.node.BaseJsonNode*
        com.fasterxml.jackson.databind.node.ContainerNode<ObjectNode>
          com.fasterxml.jackson.databind.node.ObjectNode

and this is the hierarchy for IntNode

java.lang.Object*
  com.fasterxml.jackson.databind.JsonSerializable.Base*
    com.fasterxml.jackson.databind.JsonNode*
      com.fasterxml.jackson.databind.node.BaseJsonNode*
        com.fasterxml.jackson.databind.node.ValueNode
          com.fasterxml.jackson.databind.node.NumericNode
            com.fasterxml.jackson.databind.node.IntNode

I added a * close to common superclasses that you can use to cast for a more generic type that can include both IntNode and ObjectNode.

Note that you need also to change the type of the variable currentNode accordingly to the cast if necessary.


But what is strange here is that you need to go at a so deep level using jackson. Why don't you convert the data stored in DynamoDb as a bean, if you don't have a predefined structure for your data (and so you haven't yet defined a bean to host them) you can use also a Map.

There are functions of dynamodb java library doing that automatically for you, for example you can use a code similar to the following:

    Table table = getDynamoDB().getTable("yourTable");
    Item item = table.getItem("id", "yourId");
    Map map = item.asMap();
    // map is holding in java the data present on Dynamodb
    // in the table yourTable with id yourId
Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
  • I have to out data in Dynamo. I don't have read it from table.Example i am following is from amazon docs. How it's working for them – Manish May 08 '19 at 09:09
  • @Manish what it means that you have to out data in Dynamo? The data in dynamo are stored in tables, so you can extract data from a table. Or what you have to extract? – Davide Lorenzo MARINO May 08 '19 at 09:49
  • @Manish post the link to the dynamodb documentation that you are following. – Davide Lorenzo MARINO May 08 '19 at 09:56
  • Link is already there in my post. Also i got the issue. Issue was in my json file. I was using json in it while it should a list of json. – Manish May 08 '19 at 11:01
  • if you cast it to IntNode, how do you alter it? .put() is not a method of IntNode.... – john k Apr 08 '22 at 21:19