# Import data from CSV files¶

This topic provides an example of how to use Exchange to import Nebula Graph data stored in HDFS or local CSV files.

To import a local CSV file to Nebula Graph, see Nebula Importer.

## Data set¶

This topic takes the basketballplayer dataset as an example.

## Environment¶

This example is done on MacOS. Here is the environment configuration information:

• Hardware specifications:
• CPU: 1.7 GHz Quad-Core Intel Core i7
• memory: 16 GB
• Spark: 2.4.7, Stand-alone

## Prerequisites¶

Before importing data, you need to confirm the following information:

• Nebula Graph has been installed and deployed with the following information:

• IP address and port of Graph and Meta services.
• User name and password with Nebula Graph write permission.
• Exchange has been compiled, or download the compiled .jar file directly.
• Spark has been installed.
• Learn about the Schema created in Nebula Graph, including Tag and Edge type names, properties, and more.
• If files are stored in HDFS, ensure that the Hadoop service is running properly.
• If files are stored locally and Nebula Graph is a cluster architecture, you need to place the files in the same directory locally on each machine in the cluster.

## Steps¶

### Step 1: Create the Schema in Nebula Graph¶

Analyze the data to create a Schema in Nebula Graph by following these steps:

1. Identify the Schema elements. The Schema elements in the Nebula Graph are shown in the following table.

Element name property
Tag player name string, age int
Tag team name string
Edge Type follow degree int
Edge Type serve start_year int, end_year int
2. Create a graph space basketballplayer in the Nebula Graph and create a Schema as shown below.

## create graph space
(partition_num = 10, \
replica_factor = 1, \
vid_type = FIXED_STRING(30));

## use the graph space basketballplayer

## create Tag player
nebula> CREATE TAG player(name string, age int);

## create Tag team
nebula> CREATE TAG team(name string);

## create Edge type follow
nebula> CREATE EDGE follow(degree int);

## create Edge type serve
nebula> CREATE EDGE serve(start_year int, end_year int);


### Step 2: Process CSV files¶

Confirm the following information:

1. Process CSV files to meet Schema requirements.

Note

2. Obtain the CSV file storage path.

### Step 3: Modify configuration file¶

After Exchange is compiled, copy the conf file target/classes/application.conf settings CSV data source configuration. In this case, the copied file is called csv_application.conf. For details on each configuration item, see Parameters in the configuration file.

{
# Spark configuration
spark: {
app: {
name: Nebula Exchange 2.5.0
}
driver: {
cores: 1
maxResultSize: 1G
}
executor: {
memory:1G
}

cores {
max: 16
}
}

# Nebula Graph configuration
nebula: {
# Specify the IP addresses and ports for Graph and all Meta services.
# If there are multiple addresses, the format is "ip1:port","ip2:port","ip3:port".
# Addresses are separated by commas.
graph:["127.0.0.1:9669"]
meta:["127.0.0.1:9559"]
}

# The account entered must have write permission for the Nebula Graph space.
user: root
pswd: nebula

# Fill in the name of the graph space you want to write data to in the Nebula Graph.
connection {
timeout: 3000
retry: 3
}
execution {
retry: 3
}
error: {
max: 32
output: /tmp/errors
}
rate: {
limit: 1024
timeout: 1000
}
}

# Processing vertex
tags: [
# Set information about Tag player.
{
name: player
type: {
# Specify the data source file format, set to CSV.
source: csv

# Specifies how to import the data into Nebula Graph: Client or SST.
sink: client
}

# Specify the path to the CSV file.
# If the file is stored in HDFS, use double quotation marks to enclose the file path, starting with hdfs://, for example, "hdfs://ip:port/xx/xx".
# If the file is stored locally, use double quotation marks around the path, starting with file://, for example, "file:///tmp/xx.csv".
path: "hdfs://192.168.*.*:9000/data/vertex_player.csv"

# If the CSV file does not have a header, use [_c0, _c1, _c2, ..., _cn] to represent its header and indicate the columns as the source of the property values.
# If the CSV file has headers, use the actual column names.
fields: [_c1, _c2]

# Specify the column names in the player table in fields, and their corresponding values are specified as properties in the Nebula Graph.
# The sequence of fields and nebula.fields must correspond to each other.
# If multiple column names need to be specified, separate them by commas.
nebula.fields: [age, name]

# Specify a column of data in the table as the source of vertex VID in the Nebula Graph.
# Currently, Nebula Graph 2.5.0 supports only strings or integers of VID.
vertex: {
field:_c0
# policy:hash
}

# The delimiter specified. The default value is comma.
separator: ","

# If the CSV file have header, set the header to true.
# If the CSV file does not have header, set the header to false. The default value is false.

# Number of pieces of data written to Nebula Graph in a single batch.
batch: 256

# Number of Spark partitions
partition: 32
}

# Set Tag Team information.
{
name: team
type: {
source: csv
sink: client
}
path: "hdfs://192.168.*.*:9000/data/vertex_team.csv"
fields: [_c1]
nebula.fields: [name]
vertex: {
field:_c0
}
separator: ","
batch: 256
partition: 32
}

# If more vertexes need to be added, refer to the previous configuration to add them.
]
# Processing edge
edges: [
# Set information about Edge Type follow
{
# The corresponding Edge Type name in Nebula Graph.
name: follow
type: {
# Specify the data source file format, set to CSV.
source: csv

# Specifies how to import the data into Nebula Graph: Client or SST.
sink: client
}

# Specify the path to the CSV file.
# If the file is stored in HDFS, use double quotation marks to enclose the file path, starting with hdfs://, for example, "hdfs://ip:port/xx/xx".
# If the file is stored locally, use double quotation marks around the path, starting with file://, for example, "file:///tmp/xx.csv".
path: "hdfs://192.168.*.*:9000/data/edge_follow.csv"

# If the CSV file does not have a header, use [_c0, _c1, _c2, ..., _cn] to represent its header and indicate the columns as the source of the property values.
# If the CSV file has headers, use the actual column names.
fields: [_c2]

# Specify the column names in the edge table in fields, and their corresponding values are specified as properties in the Nebula Graph.
# The sequence of fields and nebula.fields must correspond to each other.
nebula.fields: [degree]

# Specify a column as the source for the starting and destination vertexes.
# Currently, Nebula Graph 2.5.0 supports only strings or integers of VID.
source: {
field: _c0
}
target: {
field: _c1
}

# The delimiter specified. The default value is comma.
separator: ","

# (optionally) Specify a column as the source of the rank.

#ranking: rank

# If the CSV file have header, set the header to true.
# If the CSV file does not have header, set the header to false. The default value is false.

# Number of pieces of data written to Nebula Graph in a single batch.
batch: 256

# Number of Spark partitions
partition: 32
}

# Set information about Edge Type serve.
{
name: serve
type: {
source: csv
sink: client
}
path: "hdfs://192.168.*.*:9000/data/edge_serve.csv"
fields: [_c2,_c3]
nebula.fields: [start_year, end_year]
source: {
field: _c0
}
target: {
field: _c1
}
separator: ","
batch: 256
partition: 32
}

]
# If more edges need to be added, refer to the previous configuration to add them.
}


### Step 4: Import data into Nebula Graph¶

Run the following command to import CSV data into Nebula Graph. For a description of the parameters, see Options for import.

${SPARK_HOME}/bin/spark-submit --master "local" --class com.vesoft.nebula.exchange.Exchange <nebula-exchange-2.5.0.jar_path> -c <csv_application.conf_path>  Note JAR packages are available in two ways: compiled them yourself, or download the compiled .jar file directly. Example: ${SPARK_HOME}/bin/spark-submit  --master "local" --class com.vesoft.nebula.exchange.Exchange  /root/nebula-spark-utils/nebula-exchange/target/nebula-exchange-2.5.0.jar  -c /root/nebula-spark-utils/nebula-exchange/target/classes/csv_application.conf


You can search for batchSuccess.<tag_name/edge_name> in the command output to check the number of successes. For example, batchSuccess.follow: 300.

### Step 5: (optional) Validation data¶

Users can verify that data has been imported by executing a query in the Nebula Graph client (for example, Nebula Graph Studio). Such as:

GO FROM "player100" OVER follow;


Users can also run the SHOW STATS command to view statistics.

### Step 6: (optional) Rebuild indexes in Nebula Graph¶

With the data imported, users can recreate and rebuild indexes in Nebula Graph. For details, see Index overview.

Last update: September 1, 2021