Practicalgremlin.Pdf
Total Page:16
File Type:pdf, Size:1020Kb
PRACTICAL GREMLIN An Apache TinkerPop Tutorial Kelvin R. Lawrence Version 283-preview, August 2nd 2021 Table of Contents 1. INTRODUCTION . 1 1.1. How this book came to be. 1 1.2. Providing feedback. 2 1.3. Some words of thanks . 2 1.4. What is this book about?. 2 1.5. Introducing the book sources, sample programs and data . 4 1.6. A word about TinkerPop 3.4 . 5 1.7. Introducing TinkerPop 3.5 . 6 1.8. So what is a graph database and why should I care? . 6 1.9. A word about terminology . 8 2. GETTING STARTED . 10 2.1. What is Apache TinkerPop? . 10 2.2. The Gremlin console . 11 2.2.1. Downloading, installing and launching the console . 11 2.2.2. Saving output from the console to a file . 15 2.3. Introducing TinkerGraph . 16 2.4. Introducing the air-routes graph . 18 2.4.1. Updated versions of the air-route data . 22 2.5. TinkerPop 3 migration notes . 22 2.5.1. Creating a TinkerGraph TP2 vs TP3 . 22 2.5.2. Loading a graphML file TP2 vs TP3 . 22 2.5.3. A word about the TinkerPop.sugar plugin . 23 2.6. Loading the air-routes graph using the Gremlin console . 24 2.7. Turning off some of the Gremlin console’s output. 25 2.8. A word about indexes and schemas. 25 3. WRITING GREMLIN QUERIES . 27 3.1. Introducing Gremlin . 27 3.1.1. A quick look at Gremlin and SQL . 27 3.2. Some fairly basic Gremlin queries . 29 3.2.1. Retrieving property values from a vertex . 31 3.2.2. Does a specific property exist on a given vertex or edge? . 32 3.2.3. Counting things . 33 3.2.4. Counting groups of things. 34 3.3. Starting to walk the graph . 36 3.3.1. Some simple graph traversal examples . 37 3.3.2. What vertices and edges did I visit? - Introducing path . 38 3.3.3. Modifying a path using from and to modulators. 41 3.3.4. Does an edge exist between two vertices? . 44 3.3.5. Using as, select and project to refer to traversal steps . 44 3.3.6. Using multiple as steps with the same label. 47 3.3.7. Returning selected parts of a path . 49 3.3.8. Examining the edge between two vertices . 49 3.4. Limiting the amount of data returned . 50 3.4.1. Retrieving a range of vertices . 52 3.4.2. Removing duplicates - introducing dedup. 54 3.5. Using valueMap to explore the properties of a vertex or edge . 55 3.5.1. Changes to valueMap introduced in TinkerPop 3.4 . 57 3.6. An alternative to valueMap - introducing elementMap . 60 3.7. Assigning query results to a variable . 61 3.7.1. Introducing toList, toSet, bulkSet and fill . 63 3.8. Working with IDs . 65 3.9. Working with labels. 67 3.10. Using the local step to make sure we get the result we intended . 69 3.11. Basic statistical and numerical operations . 71 3.12. Testing values and ranges of values . 73 3.12.1. Using between to simulate startsWith . 77 3.12.2. Refining flight routes analysis using not, neq, within and without. 79 3.12.3. Using coin and sample to sample a dataset. 81 3.12.4. Using Math.random to more randomly select a single vertex . 82 3.13. New text search predicates added in TinkerPop 3.4 . 85 3.13.1. startingWith . 85 3.13.2. endingWith. 87 3.13.3. containing. 87 3.13.4. notStartingWith . ..