aboutsummaryrefslogtreecommitdiffstats
path: root/scalding/src/main/scala/sandcrawler/HBaseCrossrefScoreJob.scala
blob: 1360af0b4fff8258b1f537f33b8993d2985e8bb4 (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package sandcrawler

import java.util.Properties

import scala.util.parsing.json.JSON

import cascading.tuple.Fields
import com.twitter.scalding._
import com.twitter.scalding.typed.TDsl._
import org.apache.hadoop.hbase.io.ImmutableBytesWritable
import org.apache.hadoop.hbase.util.Bytes
import parallelai.spyglass.base.JobBase
import parallelai.spyglass.hbase.HBaseConstants.SourceMode
import parallelai.spyglass.hbase.HBasePipeConversions
import parallelai.spyglass.hbase.HBaseSource

class HBaseCrossrefScoreJob(args: Args) extends JobBase(args) with HBasePipeConversions {

  // key is SHA1
  val grobidSource = HBaseCrossrefScore.getHBaseSource(
    args("hbase-table"),
    args("zookeeper-hosts"))
  val grobidPipe = grobidSource
    .read
    .map('tei_json -> 'slug) {
      json : ImmutableBytesWritable => {
        HBaseCrossrefScore.grobidToSlug(json.toString) match {
          case Some(slug) => slug
          case None => "nothing"
        }
      }
    }
    .debug
    .map('key -> 'sha1) { sha1 : String => sha1 }

  val crossrefSource = TextLine(args("crossref-input"))
  val crossrefPipe = crossrefSource
    .read
    .map('line -> 'slug) {
      json : String => HBaseCrossrefScore.crossrefToSlug(json)}
    .debug

  val innerJoinPipe = grobidPipe.joinWithSmaller('slug -> 'slug, crossrefPipe)
  innerJoinPipe
    .mapTo(('tei_json, 'line, 'sha1) -> ('sha1, 'doi, 'score)) {
      x : (String, String, String) => HBaseCrossrefScore.performJoin(x._1, x._2, x._3)}
    .write(TypedTsv[(String, String, String)](args("output")))
}

object HBaseCrossrefScore {
  def getHBaseSource(hbaseTable: String, zookeeperHosts: String) : HBaseSource = HBaseBuilder.build(
    hbaseTable,      // HBase Table Name
    zookeeperHosts,  // HBase Zookeeper server (to get runtime config info; can be array?)
    List("grobid0:tei_json"),
    SourceMode.SCAN_ALL)

  def performJoin(grobidJson : String, crossRefJson : String, sha1 : String) : (String, String, String) = {
    (sha1, "1.2.3.4", "100")
  }

  def jsonToMap(json : String) : Map[String, Any] = {
    // https://stackoverflow.com/a/32717262/631051
    val jsonObject = JSON.parseFull(json)
    if (jsonObject == None) {
      // Empty map for malformed JSON
      Map[String, Any]("foo" -> json)
    } else {
      jsonObject.get.asInstanceOf[Map[String, Any]]
    }
  }

  def grobidToSlug(json : String) : Option[String] = {
    throw new Exception(json)
    val map = jsonToMap(json)
    if (map contains "title") {
      titleToSlug(map("title").asInstanceOf[String])
    } else {
      Some("grobidToSlug None: " + map("foo"))
    }
  }

  def crossrefToSlug(json : String) : Option[String] = {
    val map = jsonToMap(json)
    if (map contains "title") {
      // TODO: Don't ignore titles after the first.
      titleToSlug(map("title").asInstanceOf[List[String]](0))
    } else {
      Some("crossRefToSlug None")
    }
  }

  def titleToSlug(title : String) : Option[String] = {
    Some(title)
    /*
    val slug = title.split(":")(0).toLowerCase()
    println("title: " + title + ", slug: " + slug)
    if (slug.isEmpty) {
      None
    } else {
      Some(slug)
    }
     */
  }
}