Source: https://dev-docs.yueshu.com.cn/v5.3.2/clients/spark-connector/read/ Documentation version: v5.3.2 # 从悦数图数据库开发版读数据 Spark 连接器支持从点、边或 GQL 查询结果中读取数据,并将其加载到 Spark DataFrame 中。本文演示了如何通过 Reader 参数和配置对象从悦数图数据库开发版读取数据。 ## 读取点 以下示例从图`movie`中读取点类型为`Movie`的点,并将点属性加载到 Spark DataFrame 中。 ### 通过 Reader 参数读取 将``替换为登录账号的当前密码。若已修改密码,请使用修改后的密码。 ```` val df = spark .read .format("com.vesoft.nebula.spark.connector.NebulaDataSource") .option("type", "node") .option("operate_type", "read") .option("graph_address", "127.0.0.1:9669") .option("label", "User") .option("user", "root") .option("passwd", "") .option("graph_name", "movie") .option("batch_size", "10") .load() ```` ### 通过配置对象读取 ```` val connectionConfig: NebulaConnectionConfig = NebulaConnectionConfig .builder() .withGraphAddress("127.0.0.1:9669") .withUser("root") .withPasswd("") .build() val nebulaNodeReadConfig: ReadNebulaConfig = ReadNebulaConfig .builder() .withGraphName("movie") .withTypeName("Movie") .withReturnCols(List("id")) .withBatchSize(10) .withPartitionNum(1) .build() val df = spark.read.nebula(connectionConfig, nebulaNodeReadConfig).loadNode() ```` ## 读取边 以下示例从图`movie`中读取边类型为`Act`的边,并将边属性加载到 Spark DataFrame 中。 ### 通过 Reader 参数读取 ```` val df = spark.read .format("com.vesoft.nebula.spark.connector.NebulaDataSource") .option("type", "edge") .option("operate_type", "read") .option("graph_address", "127.0.0.1:9669") .option("user", "root") .option("passwd", "") .option("graph_name", "movie") .option("label", "Watch") .option("batch_size", "10") .load() ```` ### 通过配置对象读取 ```` val connectionConfig: NebulaConnectionConfig = NebulaConnectionConfig .builder() .withGraphAddress("127.0.0.1:9669") .withUser("root") .withPasswd("") .build() val nebulaReadEdgeConfig: ReadNebulaConfig = ReadNebulaConfig .builder() .withGraphName("movie") .withTypeName("Act") .withReturnCols(null) .withBatchSize(1000) .withPartitionNum(1) .build() val df = spark.read.nebula(connectionConfig, nebulaReadEdgeConfig).loadEdge() ```` ## 读取查询结果 以下示例从图`movie`中读取 GQL 查询结果,并将结果加载到 Spark DataFrame 中。 ### 通过 Reader 参数读取 ```` val df = spark .read .format("com.vesoft.nebula.spark.connector.NebulaDataSource") .option("type", "gql") .option("operate_type", "read") .option("graph_address", "127.0.0.1:9669") .option("user", "root") .option("passwd", "") .option("gql","use movie match(v:Movie) return v.id, v.name") .load() ```` ### 通过配置对象读取 ```` val connectionConfig: NebulaConnectionConfig = NebulaConnectionConfig .builder() .withGraphAddress("127.0.0.1:9669") .withUser("root") .withPasswd("") .build() val nebulaGqlConfig:GqlNebulaConfig = GqlNebulaConfig .builder() .withGql("USE movie MATCH(v:Movie) RETURN v.id, v.name") .build() val df = spark.read.gql(connectionConfig, nebulaGqlConfig).load() ```` ## 更多信息 - [Reader 参数](https://dev-docs.yueshu.com.cn/v5.3.2/clients/spark-connector/config/reader-options/) - [配置类](https://dev-docs.yueshu.com.cn/v5.3.2/clients/spark-connector/config/)