【问题标题】:multidplyr with database connection具有数据库连接的 multidplyr
【发布时间】:2016-05-11 15:17:31
【问题描述】:

我正在尝试使用 multidplyr 运行 do 命令,该命令运行自定义函数,该函数使用 RJDBC 查询 Vertica 数据库。我运行multidplyr 示例或直接查询数据库没有问题,但是当我尝试连接到multidplyr 中的数据库时出现错误:

checkForRemoteErrors(lapply(cl, recvResult)) 中的错误:3 个节点 产生错误;第一个错误:未检测到正在运行的 JVM。也许 .jiit() 会有帮助的。

我尝试了评论here 中的建议手动创建集群,传递vertica 数据库连接对象,但我仍然收到“未检测到正在运行的JVM”的错误。我猜这是因为我需要告诉每个节点启动一个 JVM,但我现在不知道该怎么做。

我的代码,很抱歉因为我无法共享数据库,所以它无法重现:

# set up DB connection
vertica <- dbConnect(vDriver, ...connection info...)

# create cluster
cluster3 <- create_cluster(3)
parallel::clusterExport(cl = cluster3, c("getData", "vertica"))

# run function in parallel using multidplyr
accounts_part <- multidplyr::partition(accounts, accountId, cluster = cluster3)

accounts_data <- accounts_part %>% 
  group_by(accountId) %>%
  do(getData(ac = .$accountId, vertica = vertica))

【问题讨论】:

    标签: r foreach parallel-processing dplyr


    【解决方案1】:

    我无法重现您的代码,因此我的回答将基于我的经验(我在为每个集群分配(不同)随机数时遇到了类似的问题)和 thisthis。希望它有效。

    我认为您的主要问题是您的连接是为了将您的数据库与实际会话链接起来,因此您不能在每个节点上“复制”它,因为它们需要自己的“方式”到数据库。

    另一方面,您仍然需要为每个连接使用相同的名称,以便能够在单个(并行)调用中管理它们。因此,我认为您只需直接在每个节点中创建连接,而不是在实际会话中创建一个连接,然后将其复制到节点中。

    # create cluster
    cluster3 <- create_cluster(3)
    
    # export data, function, ...
    cluster_assign_value(cluster, 'getData', getData)
    
    # setup DB connection in each cluster (evaluated in each cluster)
    cluster_eval(cluster, vertica <- dbConnect(vDriver, ...connection info...))
    # not
    # cluster_assign_value(cluster,
    #     'vertica', dbConnect(vDriver, ...connection info...)
    # )
    # which is evaluate locally and so it is the same as your code
    
    # run function in parallel using multidplyr
    accounts_part <- multidplyr::partition(accounts, accountId,
        cluster = cluster3
    )
    
    accounts_data <- accounts_part %>% 
        # group_by(accountId) %>% # partition() have already grouped them
        do(getData(ac = .$accountId, vertica = vertica)) # %>%
        # collect() # if you have no more computations to do and want get back
                    # the results locally
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-20
      • 2022-11-11
      • 2011-12-16
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-13
      相关资源
      最近更新 更多