@@ -828,167 +828,6 @@ def demonstrate_gremlin_queries(db):
828828 print (f" 🌐 { name } from { city } " )
829829 print (f" ⏱️ Time: { time .time () - query_start :.3f} s" )
830830
831- print (f" ⏱️ OpenCypher section: { time .time () - section_start :.3f} s" )
832- print ("\n 💡 OpenCypher features demonstrated:" )
833- print (" • Pattern matching with MATCH" )
834- print (" • Filtering with WHERE" )
835- print (" • DISTINCT for deduplication" )
836- print (" • Aggregations with count()" )
837- print (" • Variable-length paths with repeat()" )
838- print (" • Sorting with order()" )
839-
840- except Exception as e :
841- print (f" ❌ Error in Gremlin queries: { e } " )
842- print (" 💡 Note: Gremlin support depends on your ArcadeDB build" )
843- import traceback
844-
845- traceback .print_exc ()
846-
847-
848- def demonstrate_gremlin_queries (db ):
849- """Demonstrate graph queries using Gremlin traversal language"""
850- print ("\n 🎯 Gremlin Queries (matching Cypher functionality):" )
851-
852- section_start = time .time ()
853-
854- try :
855- # 1. Find all friends of Alice using Gremlin
856- print ("\n 1️⃣ Find all friends of Alice (Gremlin):" )
857- query_start = time .time ()
858- result = db .query (
859- "gremlin" ,
860- """
861- g.V().hasLabel('Person').has('name', 'Alice Johnson')
862- .out('FRIEND_OF')
863- .project('name', 'city')
864- .by('name')
865- .by('city')
866- .order().by(select('name'))
867- """ ,
868- )
869-
870- for row in result :
871- name = row .get ("name" )
872- city = row .get ("city" )
873- print (f" 👥 { name } from { city } " )
874- print (f" ⏱️ Time: { time .time () - query_start :.3f} s" )
875-
876- # 2. Find friends of friends using Gremlin
877- print ("\n 2️⃣ Find friends of friends of Alice (Gremlin):" )
878- query_start = time .time ()
879- result = db .query (
880- "gremlin" ,
881- """
882- g.V().hasLabel('Person').has('name', 'Alice Johnson')
883- .out('FRIEND_OF').as('friend')
884- .out('FRIEND_OF').as('fof')
885- .where(values('name').is(neq('Alice Johnson')))
886- .select('fof', 'friend')
887- .by('name')
888- .by('name')
889- .order().by(select('fof'))
890- """ ,
891- )
892-
893- for row in result :
894- name = row .get ("fof" )
895- through = row .get ("friend" )
896- print (f" 🔗 { name } (through { through } )" )
897- print (f" ⏱️ Time: { time .time () - query_start :.3f} s" )
898-
899- # 3. Find mutual friends using Gremlin
900- print ("\n 3️⃣ Find mutual friends between Alice and Bob (Gremlin):" )
901- query_start = time .time ()
902- result = db .query (
903- "gremlin" ,
904- """
905- g.V().hasLabel('Person').has('name', 'Alice Johnson')
906- .out('FRIEND_OF').as('mutual')
907- .in('FRIEND_OF').has('name', 'Bob Smith')
908- .select('mutual')
909- .values('name')
910- .order()
911- """ ,
912- )
913-
914- mutual_friends = list (result )
915- if mutual_friends :
916- for row in mutual_friends :
917- print (f" 🤝 { row .get ('result' )} " )
918- else :
919- print (" ℹ️ No mutual friends found" )
920- print (f" ⏱️ Time: { time .time () - query_start :.3f} s" )
921-
922- # 4. Find close friendships using Gremlin
923- print ("\n 4️⃣ Find close friendships (Gremlin):" )
924- query_start = time .time ()
925- result = db .query (
926- "gremlin" ,
927- """
928- g.V().hasLabel('Person').as('p1')
929- .outE('FRIEND_OF').has('closeness', 'close').as('edge')
930- .inV().as('p2')
931- .select('p1', 'p2', 'edge')
932- .by('name')
933- .by('name')
934- .by('since')
935- .order().by(select('edge'))
936- """ ,
937- )
938-
939- for row in result :
940- person1 = row .get ("p1" )
941- person2 = row .get ("p2" )
942- since = row .get ("edge" )
943- print (f" 💙 { person1 } → { person2 } (since { since } )" )
944- print (f" ⏱️ Time: { time .time () - query_start :.3f} s" )
945-
946- # 5. Count friends per person using Gremlin
947- print ("\n 5️⃣ Count friends per person (Gremlin aggregation):" )
948- query_start = time .time ()
949- result = db .query (
950- "gremlin" ,
951- """
952- g.V().hasLabel('Person')
953- .project('name', 'friend_count')
954- .by('name')
955- .by(out('FRIEND_OF').count())
956- .order()
957- .by(select('friend_count'), desc)
958- .by(select('name'))
959- """ ,
960- )
961-
962- for row in result :
963- name = row .get ("name" )
964- count = row .get ("friend_count" )
965- print (f" • { name } : { count } friends" )
966- print (f" ⏱️ Time: { time .time () - query_start :.3f} s" )
967-
968- # 6. Find variable length paths using Gremlin
969- print ("\n 6️⃣ Find connections within 3 steps from Alice (Gremlin):" )
970- query_start = time .time ()
971- result = db .query (
972- "gremlin" ,
973- """
974- g.V().hasLabel('Person').has('name', 'Alice Johnson')
975- .repeat(out('FRIEND_OF').simplePath())
976- .times(3).emit()
977- .where(values('name').is(neq('Alice Johnson')))
978- .dedup()
979- .project('name', 'city')
980- .by('name')
981- .by('city')
982- .order().by(select('name'))
983- """ ,
984- )
985-
986- for row in result :
987- name = row .get ("name" )
988- city = row .get ("city" )
989- print (f" 🌐 { name } from { city } " )
990- print (f" ⏱️ Time: { time .time () - query_start :.3f} s" )
991-
992831 print (f" ⏱️ OpenCypher section: { time .time () - section_start :.3f} s" )
993832 print ("\n 💡 OpenCypher features demonstrated:" )
994833 print (" • Pattern matching with MATCH" )
0 commit comments