您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 能源与动力工程 > Neo4j图形数据库的好处
NoSQL?Imagecredit:@emileifrememil@neotechnology.comNoSQLoverviewFirstoff:thenameNoSQLisNOT“NeverSQL”NoSQLisNOT“NoToSQL”NOSQLNotOnlySQL!issimplyFour(emerging)NOSQLcategoriesKey-valuestoresBasedonAmazon'sDynamopaperDatamodel:(global)collectionofK-VpairsExample:Dynomite,Voldemort,Tokyo*BigTableclonesBasedonGoogle'sBigTablepaperDatamodel:bigtable,columnfamiliesExample:HBase,Hypertable,CassandraFour(emerging)NOSQLcategoriesDocumentdatabasesInspiredbyLotusNotesDatamodel:collectionsofK-VcollectionsExample:CouchDB,MongoDBGraphdatabasesInspiredbyEuler&graphtheoryDatamodel:nodes,rels,K-VonbothExample:AllegroGraph,Sones,Neo4jNOSQLdatamodelsBigtableclonesKey-valuestoresDocumentdatabasesGraphdatabasesDatacomplexityDatasizeNOSQLdatamodelsDatacomplexityDatasizeBigtableclonesKey-valuestoresDocumentdatabases90%ofusecases(Thisisstillbillionsofnodes&relationships)GraphdatabasesGraphDBs&Neo4jintroTheGraphDBmodel:representationCoreabstractions:NodesRelationshipsbetweennodesPropertiesonbothname=“Emil”age=29sex=“yes”type=KNOWStime=4yearstype=carvendor=“SAAB”model=“95Aero”112233Example:TheMatrixname=“ThomasAnderson”age=2911name=“TheArchitect”4242CODED_BYdisclosure=publicname=“Cypher”lastname=“Reagan”disclosure=secretage=6monthsname=“AgentSmith”version=1.0blanguage=C++331313KNOWSKNOWSname=“Morpheus”rank=“Captain”occupation=“Totalbadass”age=3daysname=“Trinity”7722KNOWSKNOWSKNOWSCode(1):BuildinganodespaceGraphDatabaseServicegraphDb=...//Getfactory//CreateThomas'Neo'AndersonNodemrAnderson=graphDb.createNode();mrAnderson.setProperty(name,ThomasAnderson);mrAnderson.setProperty(age,29);//CreateMorpheusNodemorpheus=graphDb.createNode();morpheus.setProperty(name,Morpheus);morpheus.setProperty(rank,Captain);morpheus.setProperty(occupation,Totalbadass);//CreatearelationshiprepresentingthattheyknoweachothermrAnderson.createRelationshipTo(morpheus,RelTypes.KNOWS);//...createTrinity,Cypher,AgentSmith,ArchitectsimilarlyCode(1):BuildinganodespaceGraphDatabaseServicegraphDb=...//GetfactoryTransactiontx=neo.beginTx();//CreateThomas'Neo'AndersonNodemrAnderson=graphDb.createNode();mrAnderson.setProperty(name,ThomasAnderson);mrAnderson.setProperty(age,29);//CreateMorpheusNodemorpheus=graphDb.createNode();morpheus.setProperty(name,Morpheus);morpheus.setProperty(rank,Captain);morpheus.setProperty(occupation,Totalbadass);//CreatearelationshiprepresentingthattheyknoweachothermrAnderson.createRelationshipTo(morpheus,RelTypes.KNOWS);//...createTrinity,Cypher,AgentSmith,Architectsimilarlytx.commit();Code(1b):DefiningRelationshipTypes//Inpackageorg.neo4j.graphdbpublicinterfaceRelationshipType{Stringname();}//Inpackageorg.yourdomain.yourapp//ExampleonhowtorolldynamicRelationshipTypesclassMyDynamicRelTypeimplementsRelationshipType{privatefinalStringname;MyDynamicRelType(Stringname){this.name=name;}publicStringname(){returnthis.name;}}//Exampleonhowtokickit,static-RelationshipType-likeenumMyStaticRelTypesimplementsRelationshipType{KNOWS,WORKS_FOR,}WhiteboardfriendlyBjörnBigCarDayCareBjörnownsdrivesbuildTheGraphDBmodel:traversalTraverserframeworkforhigh-performancetraversingacrossthenodespacename=“Emil”age=31sex=“yes”type=KNOWStime=4yearstype=carvendor=“SAAB”model=“95Aero”112233Example:MrAnderson’sfriendsname=“ThomasAnderson”age=2911name=“TheArchitect”4242CODED_BYdisclosure=publicname=“Cypher”lastname=“Reagan”disclosure=secretage=6monthsname=“AgentSmith”version=1.0blanguage=C++331313KNOWSKNOWSname=“Morpheus”rank=“Captain”occupation=“Totalbadass”age=3daysname=“Trinity”7722KNOWSKNOWSKNOWSCode(2):Traversinganodespace//InstantiateatraverserthatreturnsMrAnderson'sfriendsTraverserfriendsTraverser=mrAnderson.traverse(Traverser.Order.BREADTH_FIRST,StopEvaluator.END_OF_GRAPH,ReturnableEvaluator.ALL_BUT_START_NODE,RelTypes.KNOWS,Direction.OUTGOING);//TraversethenodespaceandprintouttheresultSystem.out.println(MrAnderson'sfriends:);for(Nodefriend:friendsTraverser){System.out.printf(Atdepth%d=%s%n,friendsTraverser.currentPosition().getDepth(),friend.getProperty(name));}$bin/start-neo-exampleMrAnderson'sfriends:Atdepth1=MorpheusAtdepth1=TrinityAtdepth2=CypherAtdepth3=AgentSmith$friendsTraverser=mrAnderson.traverse(Traverser.Order.BREADTH_FIRST,StopEvaluator.END_OF_GRAPH,ReturnableEvaluator.ALL_BUT_START_NODE,RelTypes.KNOWS,Direction.OUTGOING);name=“ThomasAnderson”age=29name=“Morpheus”rank=“Captain”occupation=“Totalbadass”name=“TheArchitect”disclosure=publicage=3daysname=“Trinity”name=“Cypher”lastname=“Reagan”disclosure=secretage=6monthsname=“AgentSmith”version=1.0blanguage=C++7722331313424211KNOWSKNOWSCODED_BYKNOWSKNOWSKNOWSExample:Friendsinlove?name=“ThomasAnderson”age=29name=“Morpheus”rank=“Captain”occupation=“Totalbadass”name=“TheArchitect”disclosure=publicname=“Trinity”name=“Cypher”lastname=“Reagan”disclosure=secretage=6monthsname=“AgentSmith”version=1.0blanguage=C++7722331313424211KNOWSKNOWSCODED_BYKNOWSKNOWSKNOWSLOVESCode(3a):Customtraverser//Createatraverse
本文标题:Neo4j图形数据库的好处
链接地址:https://www.777doc.com/doc-4818835 .html