Source: https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/in-out/export/ Documentation version: v5.3.2 # EXPORT statement The `EXPORT` statement exports query results to a table, a file, or a persistent graph in NebulaGraph Database Developer Edition. ## Usage scenarios The `EXPORT` statement can be used in the following three forms within a [procedure](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/procedures/create/), depending on the execution environment and the graph created: | Execution environment | Graph created | Usage | Graph service | Graph | Supports `MATCH ... EXPORT ...` and `TABLE ... EXPORT ...` | Graph service | Temporary graph | Supports all three usage forms (`MATCH ... EXPORT ...`, `MATCH COMPUTE ... EXPORT ...`, `TABLE ... EXPORT ...`) - `MATCH ... EXPORT ...`: The `EXPORT` statement follows a `MATCH` statement as one of the top-level statements in the query and exports the query results of that `MATCH` statement. - `TABLE ... EXPORT ...`: The `EXPORT` statement follows a `TABLE` variable as one of the top-level statements in the query and exports the results stored in that `TABLE` variable. - `MATCH COMPUTE ... EXPORT ...`: The `EXPORT` statement is included in the `PER NODE` or `PER PATH` block of a match compute statement and exports the query results of that match compute statement. For the difference between graphs and temporary graphs, see [Graph vs Temporary Graph](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/overview/versus/). ## Syntax diagramsource export_statement ```` export_statement ::= 'EXPORT' ( (value_expression ('AS' identifier)? ) ( ',' (value_expression ('AS' identifier)? ) )* ) 'INTO' variable ```` ## Parameters ### `value_expression` The value expression that evaluates to a value or constant to be exported. Commonly used value expressions include [property value expressions](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/fe/expressions/property/), [function value expressions](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/fe/expressions/function/), [constant value expressions](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/fe/expressions/constant/), and [variable value expressions](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/fe/expressions/binding-variable/). ### `identifier` An alias for the field to be exported. The alias must be an identifier. For more information about identifiers, see [Rules for naming identifiers](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/overview/keywords-identifiers/#rules_for_naming_identifiers). ### `variable` The table variable or file variable that receives the data to be exported. The variable must be defined before it is used in the EXPORT statement. For more information, see [Table variable](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/variable-definition/table-variable/) and [File variable](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/variable-definition/file-variable/). The number of fields in the table variable or file variable must be equal to the number of value expressions specified in the EXPORT statement. In addition, the data types of the fields must meet the following requirements: - If the value expression is a variable value expression representing nodes or edges, the data type of the corresponding field must be `STRING`. - Otherwise, the data type of the corresponding field must be compatible with whatever data type the value expression evaluates to. ## Examples ### Graph The following examples are based on the [example dataset](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/overview/example-dataset/). #### Export function execution results into a table ```` ''' USE movie { TABLE t TYPED TABLE { user_id INT, random_rate DOUBLE } MATCH (v@`User`) WHERE v.id IN [605, 606] EXPORT v.id, rand() INTO t FOR r IN t RETURN r.user_id AS user_id, r.random_rate AS random_rate } ''' ```` The preceding example matches users whose IDs are `605` and `606`, and exports their IDs and random rating values into the table specified by table variable `t`. Return: ```` +---------+--------------------+ | user_id | random_rate | +---------+--------------------+ | 605 | 0.5253821211646783 | | 606 | 0.9054484414067815 | +---------+--------------------+ ```` #### Export function execution results into a file ```` ''' USE movie { FILE f { birth_year INT, count INT } = DATAFILE { PATH:"file:///tmp/test/birth_year_count", FORMAT:"CSV", INCLUDE_HEADER: true } MATCH (v@`Actor`) WHERE v.birthDate >= date("1990-01-01") AND v.birthDate < date("2000-01-01") RETURN year(v.birthDate) AS birth_year, count(*) AS count NEXT EXPORT birth_year, count INTO f } ''' ```` The preceding example matches actors with birth dates between `1990-01-01` and `2000-01-01`, and exports the actors' birth year and the count of occurrences into the file specified by file variable `f`. In the path `/tmp/test/birth_year_count` specified by file variable `f`, a CSV file is created as follows: ```` $ cat /tmp/test/birth_year_count/xxx.csv "birth_year","count" 1993,2 1992,3 1990,4 1998,1 1997,2 1991,3 1994,3 ```` ### Temporary graph The following examples are based on the [example dataset](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/overview/example-dataset/) and assume the temporary graph `#movie_temp` has been created and populated. For more information about creating temporary graphs, see [CREATE TEMPORARY GRAPH](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/ddl/graph-projection/create/). #### Export data to a table ```` ''' USE #movie_temp { TABLE t TYPED TABLE { user_id INT, random_rate DOUBLE } MATCH (v@`User`) WHERE v.id IN [605, 606] EXPORT v.id, rand() INTO t FOR r IN t RETURN r.user_id AS user_id, r.random_rate AS random_rate } ''' ```` For information about the preceding example, see the example Export function execution results into a table above. #### Export data to a file ```` ''' USE #movie_temp { FILE f { birth_year INT, count INT } = DATAFILE { PATH:"file:///tmp/test/birth_year_count", FORMAT:"CSV", INCLUDE_HEADER: true } MATCH (v@`Actor`) WHERE v.birthDate >= date("1990-01-01") AND v.birthDate < date("2000-01-01") RETURN year(v.birthDate) AS birth_year, count(*) AS count NEXT EXPORT birth_year, count INTO f } ''' ```` For information about the preceding example, see the example Export function execution results into a file above. #### Export data to a graph ```` ''' -- Create a target graph to store the exported data CREATE GRAPH IF NOT EXISTS movie_export_test TYPED movie_type ''' ''' -- Export data from the temporary graph and insert it into the target graph USE #movie_temp { TABLE t {id INT64, name STRING} MATCH (a@Actor) PER NODE (a) { EXPORT a.id, a.name INTO t } USE movie_export_test FOR r IN t INSERT (@Actor{id: r.id, name: r.name}) } ''' ```` The preceding example exports the ID and name of all `Actor` nodes from the temporary graph `#movie_temp` into the table variable `t`. It then switches to the local graph `movie_export_test` using the `USE` statement and inserts the data as `Actor` nodes. #### Export data to a remote NebulaGraph Database Developer Edition cluster If the target graph is on a different NebulaGraph Database Developer Edition instance, you can use the `nebula_exec` procedure to send the exported data to the remote cluster in batches. For more information about `nebula_exec`, see [nebula_exec](https://dev-docs.nebula-graph.io/v5.3.2/database-gql-reference/in-out/nebula-exec/). ```` ''' USE #movie_temp { TABLE t {id INT64, name STRING} VALUE fmt_query = " TABLE t TYPED TABLE {id INT64, name STRING} = %s USE movie FOR r IN t MATCH (a@Actor) WHERE a.id = r.id SET a.name = r.name" MATCH (a@Actor) PER NODE (a) { EXPORT a.id, "Actor_" || CAST(a.id AS STRING) INTO t } FOR split IN table_split(t, CAST(256 AS UINT)) CALL nebula_exec("nebula://:@:", fmt(fmt_query, to_literal(split))) FINISH } ''' ```` The preceding example exports the ID and a generated name for all `Actor` nodes from the temporary graph into the table variable `t`. It then splits the data into batches of up to 256 rows and sends each batch to the remote NebulaGraph Database Developer Edition cluster through the `nebula_exec` procedure to update the `name` property of the corresponding `Actor` nodes in the `movie` graph.