This is an automated email from the ASF dual-hosted git repository.
weibin pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-graphar.git
The following commit(s) were added to refs/heads/main by this push:
new 87d5eeb feat(spark): Add an example to generate ldbc sample data from
csv to graphar (#487)
87d5eeb is described below
commit 87d5eeb6a7f86574328da26f08d950e3c5bc3de8
Author: Weibin Zeng <[email protected]>
AuthorDate: Tue May 21 11:49:17 2024 +0800
feat(spark): Add an example to generate ldbc sample data from csv to
graphar (#487)
Reason for this PR
as #486 describe
What changes are included in this PR?
Add an example to generate ldbc sample data from csv to graphar
Are these changes tested?
yes, have added the example to CI/CD
Are there any user-facing changes?
no
---------
Signed-off-by: acezen <[email protected]>
---
.github/workflows/spark.yaml | 11 ++
maven-projects/spark/README.md | 17 +++
.../graphar/example/LdbcSample2GraphAr.scala | 114 +++++++++++++++++++++
.../spark/scripts/run-ldbc-sample2graphar.sh | 33 ++++++
4 files changed, 175 insertions(+)
diff --git a/.github/workflows/spark.yaml b/.github/workflows/spark.yaml
index c136121..4b8a842 100644
--- a/.github/workflows/spark.yaml
+++ b/.github/workflows/spark.yaml
@@ -149,3 +149,14 @@ jobs:
# run the importer
cd import
./neo4j.sh neo4j.json
+
+ - name: Run LdbcSample2GraphAr example
+ working-directory: maven-projects/spark
+ run: |
+ export JAVA_HOME=${JAVA_HOME_11_X64}
+ export SPARK_HOME="${HOME}/${{ matrix.spark-hadoop }}"
+ export PATH="${SPARK_HOME}/bin":"${PATH}"
+
+ scripts/build.sh ${{ matrix.mvn-profile }}
+
+ scripts/run-ldbc-sample2graphar.sh
diff --git a/maven-projects/spark/README.md b/maven-projects/spark/README.md
index 5e7e7bf..485c995 100644
--- a/maven-projects/spark/README.md
+++ b/maven-projects/spark/README.md
@@ -229,6 +229,23 @@ scripts/run-graphar2nebula.sh
The example will import the basketballplayer graph from GraphAr to NebulaGraph
and you can check the result in NebulaGraph Studio.
+## Running the local LDBC sample data to GraphAr example
+
+we provide a simple example to convert LDBC sample data to GraphAr data.
+this example is located in the directory
``spark/src/main/scala/org/apache/graphar/examples/``.
+
+To run the example, first build the project:
+
+```bash
+scripts/build.sh
+```
+
+Then run the example:
+
+```bash
+scripts/run-ldbc-sample2graphar.sh
+```
+
## How to use
Please refer to our [GraphAr Spark Library
Documentation](https://graphar.apache.org/docs/libraries/spark/).
diff --git
a/maven-projects/spark/graphar/src/main/scala/org/apache/graphar/example/LdbcSample2GraphAr.scala
b/maven-projects/spark/graphar/src/main/scala/org/apache/graphar/example/LdbcSample2GraphAr.scala
new file mode 100644
index 0000000..fe4a16b
--- /dev/null
+++
b/maven-projects/spark/graphar/src/main/scala/org/apache/graphar/example/LdbcSample2GraphAr.scala
@@ -0,0 +1,114 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.graphar.example
+
+import org.apache.graphar.graph.GraphWriter
+
+import org.apache.spark.sql.SparkSession
+import org.apache.spark.sql.types.{
+ IntegerType,
+ StringType,
+ StructField,
+ StructType
+}
+
+object LdbcSample2GraphAr {
+
+ def main(args: Array[String]): Unit = {
+ // initialize a Spark session
+ val spark = SparkSession
+ .builder()
+ .appName("LdbcSample Data to GraphAr")
+ .config("spark.master", "local")
+ .getOrCreate()
+
+ // initialize a graph writer
+ val writer: GraphWriter = new GraphWriter()
+
+ // person vertex csv input path
+ val personInputPath: String = args(0)
+ // person_knows_person edge csv input path
+ val personKnowsPersonInputPath: String = args(1)
+ // output directory
+ val outputPath: String = args(2)
+ // vertex chunk size
+ val vertexChunkSize: Long = args(3).toLong
+ // edge chunk size
+ val edgeChunkSize: Long = args(4).toLong
+ // file type
+ val fileType: String = args(5)
+
+ // put Ldbc sample graph data into writer
+ readAndPutDataIntoWriter(
+ writer,
+ spark,
+ personInputPath,
+ personKnowsPersonInputPath
+ )
+
+ // write in GraphAr format
+ writer.write(
+ outputPath,
+ spark,
+ "LdbcSample",
+ vertexChunkSize,
+ edgeChunkSize,
+ fileType
+ )
+ }
+
+ // read data from local ldbc sample csv files and put into writer
+ def readAndPutDataIntoWriter(
+ writer: GraphWriter,
+ spark: SparkSession,
+ personInputPath: String,
+ personKnowsPersonInputPath: String
+ ): Unit = {
+ // read vertices with label "Person" from given path as a DataFrame
+ val person_df = spark.read
+ .option("delimiter", "|")
+ .option("header", "true")
+ .option("inferSchema", "true")
+ .format("csv")
+ .load(personInputPath)
+ // put into writer, vertex label is "Person"
+ writer.PutVertexData("Person", person_df)
+
+ // read edges with type "Person"->"Knows"->"Person" from given path as a
DataFrame
+ // FIXME(@acezen): the schema should be inferred from the data, but
graphar spark
+ // library does not support timestamp type yet
+ val schema = StructType(
+ Array(
+ StructField("src", IntegerType, true),
+ StructField("dst", IntegerType, true),
+ StructField("creationDate", StringType, true)
+ )
+ )
+ val produced_edge_df = spark.read
+ .option("delimiter", "|")
+ .option("header", "true")
+ .schema(schema)
+ .format("csv")
+ .load(personKnowsPersonInputPath)
+ // put into writer, source vertex label is "Person", edge label is "Knows"
+ // target vertex label is "Person"
+ writer.PutEdgeData(("Person", "Knows", "Person"), produced_edge_df)
+ }
+}
diff --git a/maven-projects/spark/scripts/run-ldbc-sample2graphar.sh
b/maven-projects/spark/scripts/run-ldbc-sample2graphar.sh
new file mode 100755
index 0000000..79d47ab
--- /dev/null
+++ b/maven-projects/spark/scripts/run-ldbc-sample2graphar.sh
@@ -0,0 +1,33 @@
+#!/bin/bash
+
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+
+set -eu
+
+cur_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
+jar_file="${cur_dir}/../graphar/target/graphar-commons-0.1.0-SNAPSHOT-shaded.jar"
+person_input_file="${cur_dir}/../../../testing/ldbc_sample/person_0_0.csv"
+person_knows_person_input_file="${cur_dir}/../../../testing/ldbc_sample/person_knows_person_0_0.csv"
+output_dir="/tmp/graphar/ldbc_sample"
+
+vertex_chunk_size=100
+edge_chunk_size=1024
+file_type="parquet"
+spark-submit --class org.apache.graphar.example.LdbcSample2GraphAr ${jar_file}
\
+ ${person_input_file} ${person_knows_person_input_file} ${output_dir}
${vertex_chunk_size} ${edge_chunk_size} ${file_type}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]