MatchTask.scala 2.25 KB
Newer Older
wang-jinfeng committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
package mobvista.dmp.runner

import java.io._
import java.util.concurrent.CountDownLatch

import mobvista.prd.datasource.util.{GsonUtil, HttpUtil}
import org.apache.log4j.Logger

/**
  * match trackId by ios bundleId
  * Created by fl on 2017/7/13.
  */
class MatchTask(inputPath: String, outputPath: String, doneSignal: CountDownLatch) extends Thread {
  val dataSplit = "\t"
  val reqUrl = "http://itunes.apple.com/lookup?bundleId=%s&country=US"
  val log = Logger.getLogger(classOf[MatchTask])

  override def run(): Unit = {
    var reader: BufferedReader = null
    var writer: BufferedWriter = null

    try {
      var count = 0
      writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputPath)))
      reader = new BufferedReader(new FileReader(inputPath))
      var line = reader.readLine()
      while (line != null) {
        try {
          val result = HttpUtil.getRequest(String.format(reqUrl, line), null, log)
          val json = GsonUtil.String2JsonObject(result)
          if (json.get("results") != null) {
            val jsonArray = json.get("results").getAsJsonArray
            if (jsonArray.size() > 0) {
              writer.write(line)
              writer.write(dataSplit)
              writer.write(jsonArray.get(0).getAsJsonObject.get("trackId").getAsString)
              writer.newLine()
            } else {
              count = count + 1
              log.info(line)
              log.debug("results jsonArray.size() == 0 ; bundle " + line)
            }
          }

          if (count % 10 == 0) {
            log.info(Thread.currentThread().getName + " has processed " + count + " bundles")
          }

        } catch {
          case e: Exception => {
            e.printStackTrace()
          }
        }

        Thread.sleep(2000)
        line = reader.readLine()
      }
      log.info(Thread.currentThread().getName + " input file : " + inputPath)
      log.info(Thread.currentThread().getName + " has " + count + " packages result JsonArray.size() == 0")
    } catch {
      case e: Exception => {
        e.printStackTrace()
      }
    } finally {
      doneSignal.countDown()
      if (reader != null) {
        reader.close()
      }
      if (writer != null) {
        writer.flush()
        writer.close()
      }
    }
  }
}