Once the schema has been created the next step is to add data. It is interesting to note that the schema can be modified later with no great impact to the database, which is an important characteristic of the Sparksee graph database.
In the HelloSparksee example we are adding enough information to be able to perform some simple queries afterwards.
Although is out of the scope of this guide, it is worth noting that you can use Sparksee loaders if you are dealing with large data sets.
We are going to add information about these movies:
And some of the people that worked in these movies:
In a previous section we have seen that all this information is stored as nodes with attributes. So, we have to create 3 MOVIE nodes for the films, which will each have the attributes ID, Title and Year. Also we have to create 6 PEOPLE nodes for the cast and crew, where each will have the attributes ID and Name. Notice that we use the class Value
to set the attributes values, and the same Value
object is reused for all the attributes.
// Add some MOVIE nodes
Value value = new Value();
long mLostInTranslation = g.newNode(movieType);
g.setAttribute(mLostInTranslation, movieIdType, value.setLong(1));
g.setAttribute(mLostInTranslation, movieTitleType, value.setString("Lost in Translation"));
g.setAttribute(mLostInTranslation, movieYearType, value.setInteger(2003));
long mVickyCB = g.newNode(movieType);
g.setAttribute(mVickyCB, movieIdType, value.setLong(2));
g.setAttribute(mVickyCB, movieTitleType, value.setString("Vicky Cristina Barcelona"));
g.setAttribute(mVickyCB, movieYearType, value.setInteger(2008));
long mManhattan = g.newNode(movieType);
g.setAttribute(mManhattan, movieIdType, value.setLong(3));
g.setAttribute(mManhattan, movieTitleType, value.setString("Manhattan"));
g.setAttribute(mManhattan, movieYearType, value.setInteger(1979));
// Add some PEOPLE nodes
long pScarlett = g.newNode(peopleType);
g.setAttribute(pScarlett, peopleIdType, value.setLong(1));
g.setAttribute(pScarlett, peopleNameType, value.setString("Scarlett Johansson"));
long pBill = g.newNode(peopleType);
g.setAttribute(pBill, peopleIdType, value.setLong(2));
g.setAttribute(pBill, peopleNameType, value.setString("Bill Murray"));
long pSofia = g.newNode(peopleType);
g.setAttribute(pSofia, peopleIdType, value.setLong(3));
g.setAttribute(pSofia, peopleNameType, value.setString("Sofia Coppola"));
long pWoody = g.newNode(peopleType);
g.setAttribute(pWoody, peopleIdType, value.setLong(4));
g.setAttribute(pWoody, peopleNameType, value.setString("Woody Allen"));
long pPenelope = g.newNode(peopleType);
g.setAttribute(pPenelope, peopleIdType, value.setLong(5));
g.setAttribute(pPenelope, peopleNameType, value.setString("Penélope Cruz"));
long pDiane = g.newNode(peopleType);
g.setAttribute(pDiane, peopleIdType, value.setLong(6));
g.setAttribute(pDiane, peopleNameType, value.setString("Diane Keaton"));
// Add some MOVIE nodes
Value value = new Value();
long mLostInTranslation = g.NewNode(movieType);
g.SetAttribute(mLostInTranslation, movieIdType, value.SetLong(1));
g.SetAttribute(mLostInTranslation, movieTitleType, value.SetString("Lost in Translation"));
g.SetAttribute(mLostInTranslation, movieYearType, value.SetInteger(2003));
long mVickyCB = g.NewNode(movieType);
g.SetAttribute(mVickyCB, movieIdType, value.SetLong(2));
g.SetAttribute(mVickyCB, movieTitleType, value.SetString("Vicky Cristina Barcelona"));
g.SetAttribute(mVickyCB, movieYearType, value.SetInteger(2008));
long mManhattan = g.NewNode(movieType);
g.SetAttribute(mManhattan, movieIdType, value.SetLong(3));
g.SetAttribute(mManhattan, movieTitleType, value.SetString("Manhattan"));
g.SetAttribute(mManhattan, movieYearType, value.SetInteger(1979));
// Add some PEOPLE nodes
long pScarlett = g.NewNode(peopleType);
g.SetAttribute(pScarlett, peopleIdType, value.SetLong(1));
g.SetAttribute(pScarlett, peopleNameType, value.SetString("Scarlett Johansson"));
long pBill = g.NewNode(peopleType);
g.SetAttribute(pBill, peopleIdType, value.SetLong(2));
g.SetAttribute(pBill, peopleNameType, value.SetString("Bill Murray"));
long pSofia = g.NewNode(peopleType);
g.SetAttribute(pSofia, peopleIdType, value.SetLong(3));
g.SetAttribute(pSofia, peopleNameType, value.SetString("Sofia Coppola"));
long pWoody = g.NewNode(peopleType);
g.SetAttribute(pWoody, peopleIdType, value.SetLong(4));
g.SetAttribute(pWoody, peopleNameType, value.SetString("Woody Allen"));
long pPenelope = g.NewNode(peopleType);
g.SetAttribute(pPenelope, peopleIdType, value.SetLong(5));
g.SetAttribute(pPenelope, peopleNameType, value.SetString("Penélope Cruz"));
long pDiane = g.NewNode(peopleType);
g.SetAttribute(pDiane, peopleIdType, value.SetLong(6));
g.SetAttribute(pDiane, peopleNameType, value.SetString("Diane Keaton"));
// Add some MOVIE nodes
Value *value = new Value();
oid_t mLostInTranslation = g->NewNode(movieType);
g->SetAttribute(mLostInTranslation, movieIdType, value->SetLong(1));
g->SetAttribute(mLostInTranslation, movieTitleType, value->SetString(L"Lost in Translation"));
g->SetAttribute(mLostInTranslation, movieYearType, value->SetInteger(2003));
oid_t mVickyCB = g->NewNode(movieType);
g->SetAttribute(mVickyCB, movieIdType, value->SetLong(2));
g->SetAttribute(mVickyCB, movieTitleType, value->SetString(L"Vicky Cristina Barcelona"));
g->SetAttribute(mVickyCB, movieYearType, value->SetInteger(2008));
oid_t mManhattan = g->NewNode(movieType);
g->SetAttribute(mManhattan, movieIdType, value->SetLong(3));
g->SetAttribute(mManhattan, movieTitleType, value->SetString(L"Manhattan"));
g->SetAttribute(mManhattan, movieYearType, value->SetInteger(1979));
// Add some PEOPLE nodes
oid_t pScarlett = g->NewNode(peopleType);
g->SetAttribute(pScarlett, peopleIdType, value->SetLong(1));
g->SetAttribute(pScarlett, peopleNameType, value->SetString(L"Scarlett Johansson"));
oid_t pBill = g->NewNode(peopleType);
g->SetAttribute(pBill, peopleIdType, value->SetLong(2));
g->SetAttribute(pBill, peopleNameType, value->SetString(L"Bill Murray"));
oid_t pSofia = g->NewNode(peopleType);
g->SetAttribute(pSofia, peopleIdType, value->SetLong(3));
g->SetAttribute(pSofia, peopleNameType, value->SetString(L"Sofia Coppola"));
oid_t pWoody = g->NewNode(peopleType);
g->SetAttribute(pWoody, peopleIdType, value->SetLong(4));
g->SetAttribute(pWoody, peopleNameType, value->SetString(L"Woody Allen"));
oid_t pPenelope = g->NewNode(peopleType);
g->SetAttribute(pPenelope, peopleIdType, value->SetLong(5));
g->SetAttribute(pPenelope, peopleNameType, value->SetString(L"Penélope Cruz"));
oid_t pDiane = g->NewNode(peopleType);
g->SetAttribute(pDiane, peopleIdType, value->SetLong(6));
g->SetAttribute(pDiane, peopleNameType, value->SetString(L"Diane Keaton"));
# Add some MOVIE nodes
value = sparksee.Value()
lost_in_translation_movie = graph.new_node(movie_type)
graph.set_attribute(lost_in_translation_movie, movie_id_type, value.set_long(1))
graph.set_attribute(lost_in_translation_movie, movie_title_type, value.set_string(u"Lost in Translation"))
graph.set_attribute(lost_in_translation_movie, movie_year_type, value.set_integer(2003))
vicky_cb_movie = graph.new_node(movie_type)
graph.set_attribute(vicky_cb_movie, movie_id_type, value.set_long(2))
graph.set_attribute(vicky_cb_movie, movie_title_type, value.set_string(u"Vicky Cristina Barcelona"))
graph.set_attribute(vicky_cb_movie, movie_year_type, value.set_integer(2008))
manhattan_movie = graph.new_node(movie_type)
graph.set_attribute(manhattan_movie, movie_id_type, value.set_long(3))
graph.set_attribute(manhattan_movie, movie_title_type, value.set_string(u"manhattan_movie"))
graph.set_attribute(manhattan_movie, movie_year_type, value.set_integer(1979))
# Add some PEOPLE nodes
scarlett_people = graph.new_node(people_type)
graph.set_attribute(scarlett_people, people_id_type, value.set_long(1))
graph.set_attribute(scarlett_people, people_name_type, value.set_string(u"Scarlett Johansson"))
bill_people = graph.new_node(people_type)
graph.set_attribute(bill_people, people_id_type, value.set_long(2))
graph.set_attribute(bill_people, people_name_type, value.set_string(u"Bill Murray"))
sofia_people = graph.new_node(people_type)
graph.set_attribute(sofia_people, people_id_type, value.set_long(3))
graph.set_attribute(sofia_people, people_name_type, value.set_string(u"Sofia Coppola"))
woody_people = graph.new_node(people_type)
graph.set_attribute(woody_people, people_id_type, value.set_long(4))
graph.set_attribute(woody_people, people_name_type, value.set_string(u"Woody Allen"))
penelope_people = graph.new_node(people_type)
graph.set_attribute(penelope_people, people_id_type, value.set_long(5))
graph.set_attribute(penelope_people, people_name_type, value.set_string(u"Penélope Cruz"))
diane_people = graph.new_node(people_type)
graph.set_attribute(diane_people, people_id_type, value.set_long(6))
graph.set_attribute(diane_people, people_name_type, value.set_string(u"Diane Keaton"))
// Add some MOVIE nodes
STSValue *value = [[STSValue alloc] init];
long long mLostInTranslation = [g createNode: movieType];
[g setAttribute: mLostInTranslation attr: movieIdType value: [value setLong: 1]];
[g setAttribute: mLostInTranslation attr: movieTitleType value: [value setString: @"Lost in Translation"]];
[g setAttribute: mLostInTranslation attr: movieYearType value: [value setInteger: 2003]];
long long mVickyCB = [g createNode: movieType];
[g setAttribute: mVickyCB attr: movieIdType value: [value setLong: 2]];
[g setAttribute: mVickyCB attr: movieTitleType value: [value setString: @"Vicky Cristina Barcelona"]];
[g setAttribute: mVickyCB attr: movieYearType value: [value setInteger: 2008]];
long long mManhattan = [g createNode: movieType];
[g setAttribute: mManhattan attr: movieIdType value: [value setLong: 3]];
[g setAttribute: mManhattan attr: movieTitleType value: [value setString: @"Manhattan"]];
[g setAttribute: mManhattan attr: movieYearType value: [value setInteger: 1979]];
// Add some PEOPLE nodes
long long pScarlett = [g createNode: peopleType];
[g setAttribute: pScarlett attr: peopleIdType value: [value setLong: 1]];
[g setAttribute: pScarlett attr: peopleNameType value: [value setString: @"Scarlett Johansson"]];
long long pBill = [g createNode: peopleType];
[g setAttribute: pBill attr: peopleIdType value: [value setLong: 2]];
[g setAttribute: pBill attr: peopleNameType value: [value setString: @"Bill Murray"]];
long long pSofia = [g createNode: peopleType];
[g setAttribute: pSofia attr: peopleIdType value: [value setLong: 3]];
[g setAttribute: pSofia attr: peopleNameType value: [value setString: @"Sofia Coppola"]];
long long pWoody = [g createNode: peopleType];
[g setAttribute: pWoody attr: peopleIdType value: [value setLong: 4]];
[g setAttribute: pWoody attr: peopleNameType value: [value setString: @"Woody Allen"]];
long long pPenelope = [g createNode: peopleType];
[g setAttribute: pPenelope attr: peopleIdType value: [value setLong: 5]];
[g setAttribute: pPenelope attr: peopleNameType value: [value setString: @"Penélope Cruz"]];
long long pDiane = [g createNode: peopleType];
[g setAttribute: pDiane attr: peopleIdType value: [value setLong: 6]];
[g setAttribute: pDiane attr: peopleNameType value: [value setString: @"Diane Keaton"]];
Now that we have all the nodes in the database we can start adding the relationships between them. As previously explained in the Set the schema section we are going to build two types of relationships depending on whether the person attached to a movie is part of the cast (edge CAST) or its director (edge DIRECTS).
We are going to add an edge of type CAST between each node of type PEOPLE and each node of type MOVIE when the person worked as an actor in that movie. Then, we will set the edge attribute CHARACTER as the name of the character played by that actor in the movie.
After this, we will create an edge of type DIRECTS between a node of type PEOPLE and a node of type MOVIE for the director of each movie.
// Add some CAST edges
// Remember that we are reusing the Value class instance to set the attributes
long anEdge;
anEdge = g.newEdge(castType, mLostInTranslation, pScarlett);
g.setAttribute(anEdge, castCharacterType, value.setString("Charlotte"));
anEdge = g.newEdge(castType, mLostInTranslation, pBill);
g.setAttribute(anEdge, castCharacterType, value.setString("Bob Harris"));
anEdge = g.newEdge(castType, mVickyCB, pScarlett);
g.setAttribute(anEdge, castCharacterType, value.setString("Cristina"));
anEdge = g.newEdge(castType, mVickyCB, pPenelope);
g.setAttribute(anEdge, castCharacterType, value.setString("Maria Elena"));
anEdge = g.newEdge(castType, mManhattan, pDiane);
g.setAttribute(anEdge, castCharacterType, value.setString("Mary"));
anEdge = g.newEdge(castType, mManhattan, pWoody);
g.setAttribute(anEdge, castCharacterType, value.setString("Isaac"));
// Add some DIRECTS edges
anEdge = g.newEdge(directsType, pSofia, mLostInTranslation);
anEdge = g.newEdge(directsType, pWoody, mVickyCB);
anEdge = g.newEdge(directsType, pWoody, mManhattan);
// Add some CAST edges
// Remember that we are reusing the Value class instance to set the attributes
long anEdge;
anEdge = g.NewEdge(castType, mLostInTranslation, pScarlett);
g.SetAttribute(anEdge, castCharacterType, value.SetString("Charlotte"));
anEdge = g.NewEdge(castType, mLostInTranslation, pBill);
g.SetAttribute(anEdge, castCharacterType, value.SetString("Bob Harris"));
anEdge = g.NewEdge(castType, mVickyCB, pScarlett);
g.SetAttribute(anEdge, castCharacterType, value.SetString("Cristina"));
anEdge = g.NewEdge(castType, mVickyCB, pPenelope);
g.SetAttribute(anEdge, castCharacterType, value.SetString("Maria Elena"));
anEdge = g.NewEdge(castType, mManhattan, pDiane);
g.SetAttribute(anEdge, castCharacterType, value.SetString("Mary"));
anEdge = g.NewEdge(castType, mManhattan, pWoody);
g.SetAttribute(anEdge, castCharacterType, value.SetString("Isaac"));
// Add some DIRECTS edges
anEdge = g.NewEdge(directsType, pSofia, mLostInTranslation);
anEdge = g.NewEdge(directsType, pWoody, mVickyCB);
anEdge = g.NewEdge(directsType, pWoody, mManhattan);
// Add some CAST edges
// Remember that we are reusing the Value class instance to set the attributes
oid_t anEdge;
anEdge = g->NewEdge(castType, mLostInTranslation, pScarlett);
g->SetAttribute(anEdge, castCharacterType, value->SetString(L"Charlotte"));
anEdge = g->NewEdge(castType, mLostInTranslation, pBill);
g->SetAttribute(anEdge, castCharacterType, value->SetString(L"Bob Harris"));
anEdge = g->NewEdge(castType, mVickyCB, pScarlett);
g->SetAttribute(anEdge, castCharacterType, value->SetString(L"Cristina"));
anEdge = g->NewEdge(castType, mVickyCB, pPenelope);
g->SetAttribute(anEdge, castCharacterType, value->SetString(L"Maria Elena"));
anEdge = g->NewEdge(castType, mManhattan, pDiane);
g->SetAttribute(anEdge, castCharacterType, value->SetString(L"Mary"));
anEdge = g->NewEdge(castType, mManhattan, pWoody);
g->SetAttribute(anEdge, castCharacterType, value->SetString(L"Isaac"));
// Add some DIRECTS edges
anEdge = g->NewEdge(directsType, pSofia, mLostInTranslation);
anEdge = g->NewEdge(directsType, pWoody, mVickyCB);
anEdge = g->NewEdge(directsType, pWoody, mManhattan);
# Add some CAST edges
# Remember that we are reusing the Value class instance to set the attributes
an_edge = graph.new_edge(cast_type, lost_in_translation_movie, scarlett_people)
graph.set_attribute(an_edge, cast_character_type, value.set_string(u"Charlotte"))
an_edge = graph.new_edge(cast_type, lost_in_translation_movie, bill_people)
graph.set_attribute(an_edge, cast_character_type, value.set_string(u"Bob Harris"))
an_edge = graph.new_edge(cast_type, vicky_cb_movie, scarlett_people)
graph.set_attribute(an_edge, cast_character_type, value.set_string(u"Cristina"))
an_edge = graph.new_edge(cast_type, vicky_cb_movie, penelope_people)
graph.set_attribute(an_edge, cast_character_type, value.set_string(u"Maria Elena"))
an_edge = graph.new_edge(cast_type, manhattan_movie, diane_people)
graph.set_attribute(an_edge, cast_character_type, value.set_string(u"Mary"))
an_edge = graph.new_edge(cast_type, manhattan_movie, woody_people)
graph.set_attribute(an_edge, cast_character_type, value.set_string(u"Isaac"))
# Add some DIRECTS edges
an_edge = graph.new_edge(directs_type, sofia_people, lost_in_translation_movie)
an_edge = graph.new_edge(directs_type, woody_people, vicky_cb_movie)
an_edge = graph.new_edge(directs_type, woody_people, manhattan_movie)
// Add some CAST edges
long long anEdge;
anEdge = [g createEdge: castType tail: mLostInTranslation head: pScarlett];
[g setAttribute: anEdge attr: castCharacterType value: [value setString: @"Charlotte"]];
anEdge = [g createEdge: castType tail: mLostInTranslation head: pBill];
[g setAttribute: anEdge attr: castCharacterType value: [value setString: @"Bob Harris"]];
anEdge = [g createEdge: castType tail: mVickyCB head: pScarlett];
[g setAttribute: anEdge attr: castCharacterType value: [value setString: @"Cristina"]];
anEdge = [g createEdge: castType tail: mVickyCB head: pPenelope];
[g setAttribute: anEdge attr: castCharacterType value: [value setString: @"Maria Elena"]];
anEdge = [g createEdge: castType tail: mManhattan head: pDiane];
[g setAttribute: anEdge attr: castCharacterType value: [value setString: @"Mary"]];
anEdge = [g createEdge: castType tail: mManhattan head: pWoody];
[g setAttribute: anEdge attr: castCharacterType value: [value setString: @"Isaac"]];
// Add some DIRECTS edges
anEdge = [g createEdge: directsType tail: pSofia head: mLostInTranslation];
anEdge = [g createEdge: directsType tail: pWoody head: mVickyCB];
anEdge = [g createEdge: directsType tail: pWoody head: mManhattan];