github.com/dgraph-io/dgraph@v1.2.8/wiki/content/tutorial-1/index.md (about)

     1  +++
     2  title = "Get Started with Dgraph - Introduction"
     3  +++
     4  
     5  **Welcome to getting started with Dgraph.**
     6  
     7  [Dgraph](https://dgraph.io) is an open-source, transactional, distributed, native Graph Database. Here is the first tutorial of the get started series on using Dgraph.
     8  
     9  In this tutorial, we'll learn how to build the following graph on Dgraph,
    10  
    11  {{% load-img "/images/tutorials/1/gs-1.JPG" "The simple graph" %}}
    12  
    13  In the process, we'll learn about:
    14  
    15  - Running Dgraph using the `dgraph/standalone` docker image.
    16  - Running the following basic operations using Dgraph's UI Ratel,
    17   - Creating a node.
    18   - Creating an edge between two nodes.
    19   - Querying for the nodes.
    20  
    21  You can see the accompanying video below.
    22  
    23  {{< youtube u73ovhDCPQQ >}}
    24  
    25  ---
    26  
    27  ## Running Dgraph
    28  
    29  Running the `dgraph/standalone` docker image is the quickest way to get started with Dgraph.
    30  This standalone image is meant for quickstart purposes only.
    31  It is not recommended for production environments.
    32  
    33  Ensure that [Docker](https://docs.docker.com/install/) is installed and running on your machine.
    34  
    35  Now, it's just a matter of running the following command, and you have Dgraph up and running.
    36  
    37  ```sh
    38  docker run --rm -it -p 8000:8000 -p 8080:8080 -p 9080:9080 dgraph/standalone:latest
    39  ```
    40  
    41  ### Nodes and Edges
    42  
    43  In this section, we'll build a simple graph with two nodes and an edge connecting them.
    44  
    45  {{% load-img "/images/tutorials/1/gs-1.JPG" "The simple graph" %}}
    46  
    47  In a Graph Database, concepts or entities are represented as nodes.
    48  May it be a sale, a transaction, a place, or a person, all these entities are
    49  represented as nodes in a Graph Database.
    50  
    51  An edge represents the relationship between two nodes.
    52  The two nodes in the above graph represent people: `Karthic` and `Jessica`.
    53  You can also see that these nodes have two associated properties: `name` and `age`.
    54  These properties of the nodes are called `predicates` in Dgraph.
    55  
    56  Karthic follows Jessica. The `follows` edge between them represents their relationship.
    57  The edge connecting two nodes is also called a `predicate` in Dgraph,
    58  although this one points to another node rather than a string or an integer.
    59  
    60  The `dgraph/standalone` image setup comes with the useful Dgraph UI called Ratel.
    61  Just visit [http://localhost:8000](http://localhost:8000) from your browser, and you will be able to access it.
    62  
    63  {{% load-img "/images/tutorials/1/gs-2.png" "ratel-1" %}}
    64  
    65  We'll be using the latest stable release of Ratel.
    66  
    67  {{% load-img "/images/tutorials/1/gs-3.png" "ratel-2" %}}
    68  
    69  ### Mutations using Ratel
    70  
    71  The create, update, and delete operations in Dgraph are called mutations.
    72  
    73  Ratel makes it easier to run queries and mutations.
    74  We'll be exploring more of its features all along with the tutorial series.
    75  
    76  Let's go to the Mutate tab and paste the following mutation into the text area.
    77  _Do not execute it just yet!_
    78  
    79  ```json
    80  {
    81    "set": [
    82      {
    83        "name": "Karthic",
    84        "age": 28
    85      },
    86      {
    87        "name": "Jessica",
    88        "age": 31
    89      }
    90    ]
    91  }
    92  ```
    93  
    94  The query above creates two nodes, one corresponding to each of the JSON values associated with `"set"`.
    95  However, it doesn't create an edge between these nodes.
    96  
    97  A small modification to the mutation will fix it, so it creates an edge in between them.
    98  
    99  ```json
   100  {
   101    "set": [
   102      {
   103        "name": "Karthic",
   104        "age": 28,
   105        "follows": {
   106          "name": "Jessica",
   107          "age": 31
   108        }
   109      }
   110    ]
   111  }
   112  ```
   113  
   114  {{% load-img "/images/tutorials/1/explain-query.JPG" "explain mutation" %}}
   115  
   116  Let's execute this mutation. Click Run and boom!
   117  
   118  {{% load-img "/images/tutorials/1/mutate-example.gif" "Query-gif" %}}
   119  
   120  You can see in the response that two UIDs (Universal IDentifiers) have been created.
   121  The two values in the `"uids"` field of the response correspond
   122  to the two nodes created for "Karthic" and "Jessica".
   123  
   124  ### Querying using the has function
   125  
   126  Now, let's run a query to visualize the nodes which we just created.
   127  We'll be using Dgraph's `has` function.
   128  The expression `has(name)` returns all the nodes with a predicate `name` associated with them.
   129  
   130  ```sh
   131  {
   132    people(func: has(name)) {
   133      name
   134      age
   135    }
   136  }
   137  ```
   138  
   139  Go to the `Query` tab this time and type in the query above.
   140  Then, click `Run` on the top right of the screen.
   141  
   142  {{% load-img "/images/tutorials/1/query-1.png" "query-1" %}}
   143  
   144  Ratel renders a graph visualization of the result.
   145  
   146  Just click on any of them, notice that the nodes are assigned UIDs,
   147  matching the ones, we saw in the mutation's response.
   148  
   149  You can also view the JSON results in the JSON tab on the right.
   150  
   151  {{% load-img "/images/tutorials/1/query-2.png" "query-2" %}}
   152  
   153  #### Understanding the query
   154  
   155  {{% load-img "/images/tutorials/1/explain-query-2.JPG" "Illustration with explanation" %}}
   156  
   157  The first part of the query is the user-defined function name.
   158  In our query, we have named it as `people`. However, you could use any other name.
   159  
   160  The `func` parameter has to be associated with a built-in function of Dgraph.
   161  Dgraph offers a variety of built-in functions. The `has` function is one of them.
   162  Check out the [query language guide](https://docs.dgraph.io/query-language) to know more about other built-in functions in Dgraph.
   163  
   164  The inner fields of the query are similar to the column names in a SQL select statement or to a GraphQL query!
   165  
   166  You can easily specify which predicates you want to get back.
   167  
   168  ```graphql
   169  {
   170    people(func: has(name)) {
   171      name
   172    }
   173  }
   174  ```
   175  
   176  Similarly, you can use the `has` function to find all nodes with the `age` predicate.
   177  
   178  ```graphql
   179  {
   180    people(func: has(age)) {
   181      name
   182    }
   183  }
   184  ```
   185  
   186  ### Flexible schema
   187  
   188  Dgraph doesn't enforce a structure or a schema. Instead, you can start entering
   189  your data immediately and add constraints as needed.
   190  
   191  Let's look at this mutation.
   192  
   193  ```json
   194  {
   195    "set": [
   196      {
   197        "name": "Balaji",
   198        "age": 23,
   199        "country": "India"
   200      },
   201      {
   202        "name": "Daniel",
   203        "age": 25,
   204        "city": "San Diego"
   205      }
   206    ]
   207  }
   208  ```
   209  
   210  We are creating two nodes, while the first node has predicates `name`, `age`, and `country`,
   211  the second one has `name`, `age`, and `city`.
   212  
   213  Schemas are not needed initially. Dgraph creates
   214  new predicates as they appear in your mutations.
   215  This flexibility can be beneficial, but if you prefer to force your
   216  mutations to follow a given schema there are options available that
   217  we will explore in an next tutorial.
   218  
   219  ## Wrapping up
   220  
   221  In this tutorial, we learned the basics of Dgraph, including how to
   222  run the database, add new nodes and predicates, and query them
   223  back.
   224  
   225  Before we wrap, here are some quick bits about the next tutorial.
   226  Did you know that the nodes can also be fetched given their UID?
   227  They also can be used to create an edge between existing nodes!
   228  
   229  Sounds interesting?
   230  
   231  Check out our next tutorial of the getting started series [here]({{< relref "tutorial-2/index.md" >}}).
   232  
   233  ## Need Help
   234  
   235  * Please use [discuss.dgraph.io](https://discuss.dgraph.io) for questions, feature requests and discussions.
   236  * Please use [Github Issues](https://github.com/dgraph-io/dgraph/issues) if you encounter bugs or have feature requests.
   237  * You can also join our [Slack channel](http://slack.dgraph.io).