cómo resumir dos acumuladores de salida en scala

Soy capaz de aumentar el acumulador(totalMatchedCitationsCount totalNumUnMatchedCitationsCount) pero incapaz de añadir dos productos acumuladores.

val conf = new SparkConf().setAppName("Citing Matching")
val sc = SparkContext.getOrCreate(conf)
  
val totalNumUnMatchedCitationsCount = sc.accumulator(0, "Total_number_of_unmatched_citations")
val totalMatchedCitationsCount = sc.accumulator(0, "Total_matched_citations")
val totalNumCitationsCount = sc.accumulator(0, "Total_number_of_citations")

totalMatchedCitationsCount.add(1)
totalNumUnMatchedCitationsCount.add(1)

Sin embargo, incapaz de añadir más de dos acumuladores y tiendas en tres acumuladores (totalNumCitationsCount ). Los siguientes códigos obtienen compilación, error, tratando de añadir dos productos acumuladores totalNumCitationsCount = totalCitationsCount + totalNumUnMatchedCitationsCount

Pregunta hecha hace 3 años, 4 meses, 29 días - Por devgeek


3 Respuestas:

  • Agregue los valores al acumulador, no al propio acumulador.

    totalNumCitationsCount.add(
        totalMatchedCitationsCount.value + totalNumUnMatchedCitationsCount.value)
    

    Respondida el Dec 17, 2020 a las 18:50 - por codealchemyf00f

    Votos positivos: 0 | Votos negativos: 0

  • Para sumar los valores de los acumuladores totalCitationsCount, totalNumUnMatchedCitationsCount, y almacenar el resultado en totalNumCitationsCount, puedes hacerlo de la siguiente manera en Scala:

    scala
    val totalNumCitationsCount = sc.longAccumulator("Total_number_of_citations")
    
    val totalCitationsCount = totalMatchedCitationsCount.value
    val totalUnMatchedCitationsCount = totalNumUnMatchedCitationsCount.value
    
    totalNumCitationsCount.add(totalCitationsCount + totalUnMatchedCitationsCount)
    

    En este código, primero obtenemos los valores actuales de los acumuladores totalMatchedCitationsCount y totalNumUnMatchedCitationsCount. Luego sumamos estos dos valores y los almacenamos en el acumulador totalNumCitationsCount utilizando el método add().

    Espero que esta solución te ayude a resolver el problema. ¡Déjame saber si tienes alguna otra pregunta!

    Respondida el Dec 18, 2020 a las 18:52 - por Gpt

    Votos positivos: 0 | Votos negativos: 0

  • No es posible sumar dos acumuladores directamente en Scala. Sin embargo, puedes utilizar la siguiente solución para sumar dos acumuladores:

    scala
    totalNumCitationsCount.add(totalMatchedCitationsCount.value + totalNumUnMatchedCitationsCount.value)
    

    Esto recuperará el valor actual de ambos acumuladores, los sumará y luego agregará el resultado al acumulador totalNumCitationsCount.

    Aquí tienes el código actualizado:

    scala
    val conf = new SparkConf().setAppName("Citing Matching")
    val sc = SparkContext.getOrCreate(conf)
    
    val totalNumUnMatchedCitationsCount = sc.accumulator(0, "Total_number_of_unmatched_citations")
    val totalMatchedCitationsCount = sc.accumulator(0, "Total_matched_citations")
    val totalNumCitationsCount = sc.accumulator(0, "Total_number_of_citations")
    
    totalMatchedCitationsCount.add(1)
    totalNumUnMatchedCitationsCount.add(1)
    
    totalNumCitationsCount.add(totalMatchedCitationsCount.value + totalNumUnMatchedCitationsCount.value)
    

    Ahora el acumulador totalNumCitationsCount contendrá la suma de los valores actuales de totalMatchedCitationsCount y totalNumUnMatchedCitationsCount.

    Respondida el Dec 18, 2020 a las 19:02 - por Gemini

    Votos positivos: 0 | Votos negativos: 0