没有找到可以转换成java.lang.String的值

时间:2018-01-07 06:57:33

标签: json scala hbase

我有一个JSON输入,要写入Hbase

这方面的一个例子是

 {
  "contributors": null,
  "truncated": false,
  "text": "#Care #MichelleNunn #Stop #Pimping #Haiti #HurricaneMatthew #Give #Answers #Blacklivesmatter https:\/\/t.co\/mmlOawLdCy",
  "is_quote_status": false,
  "in_reply_to_status_id": null,
  "id": 8.4878269282513e+17,
  "favorite_count": 0,
  "entities": {
    "symbols": [

    ],
    "user_mentions": [

    ],
    "hashtags": [
      {
        "indices": [
          0,
          5
        ],
        "text": "Care"
      },
      {
        "indices": [
          6,
          19
        ],
        "text": "MichelleNunn"
      },
      {
        "indices": [
          20,
          25
        ],
        "text": "Stop"
      },
      {
        "indices": [
          26,
          34
        ],
        "text": "Pimping"
      },
      {
        "indices": [
          35,
          41
        ],
        "text": "Haiti"
      },
      {
        "indices": [
          42,
          59
        ],
        "text": "HurricaneMatthew"
      },
      {
        "indices": [
          60,
          65
        ],
        "text": "Give"
      },
      {
        "indices": [
          66,
          74
        ],
        "text": "Answers"
      },
      {
        "indices": [
          75,
          92
        ],
        "text": "Blacklivesmatter"
      }
    ],
    "urls": [

    ],
    "media": [
      {
        "expanded_url": "https:\/\/twitter.com\/skri_ar\/status\/848782692825128961\/photo\/1",
        "display_url": "pic.twitter.com\/mmlOawLdCy",
        "url": "https:\/\/t.co\/mmlOawLdCy",
        "media_url_https": "https:\/\/pbs.twimg.com\/media\/C8d7XE2XsAAiIsY.jpg",
        "id_str": "848782691151622144",
        "sizes": {
          "large": {
            "h": 618,
            "resize": "fit",
            "w": 986
          },
          "small": {
            "h": 426,
            "resize": "fit",
            "w": 680
          },
          "medium": {
            "h": 618,
            "resize": "fit",
            "w": 986
          },
          "thumb": {
            "h": 150,
            "resize": "crop",
            "w": 150
          }
        },
        "indices": [
          93,
          116
        ],
        "type": "photo",
        "id": 8.4878269115162e+17,
        "media_url": "http:\/\/pbs.twimg.com\/media\/C8d7XE2XsAAiIsY.jpg"
      }
    ]
  },
  "retweeted": false,
  "coordinates": null,
  "source": "<a href=\"https:\/\/ifttt.com\" rel=\"nofollow\">IFTTT<\/a>",
  "in_reply_to_screen_name": null,
  "in_reply_to_user_id": null,
  "retweet_count": 0,
  "id_str": "848782692825128961",
  "favorited": false,
  "user": {
    "follow_request_sent": false,
    "has_extended_profile": false,
    "profile_use_background_image": true,
    "default_profile_image": false,
    "id": 7.8840475360896e+17,
    "profile_background_image_url_https": null,
    "verified": false,
    "translator_type": "none",
    "profile_text_color": "333333",
    "profile_image_url_https": "https:\/\/pbs.twimg.com\/profile_images\/788412708530061313\/gqwMR-0g_normal.jpg",
    "profile_sidebar_fill_color": "DDEEF6",
    "entities": {
      "description": {
        "urls": [

        ]
      }
    },
    "followers_count": 15,
    "profile_sidebar_border_color": "C0DEED",
    "id_str": "788404753608957956",
    "profile_background_color": "F5F8FA",
    "listed_count": 15,
    "is_translation_enabled": false,
    "utc_offset": null,
    "statuses_count": 25793,
    "description": "",
    "friends_count": 0,
    "location": "",
    "profile_link_color": "1DA1F2",
    "profile_image_url": "http:\/\/pbs.twimg.com\/profile_images\/788412708530061313\/gqwMR-0g_normal.jpg",
    "following": false,
    "geo_enabled": false,
    "profile_banner_url": "https:\/\/pbs.twimg.com\/profile_banners\/788404753608957956\/1476807236",
    "profile_background_image_url": null,
    "screen_name": "skri_ar",
    "lang": "en",
    "profile_background_tile": false,
    "favourites_count": 0,
    "name": "Ar Skri",
    "notifications": false,
    "url": null,
    "created_at": "Tue Oct 18 15:41:56 +0000 2016",
    "contributors_enabled": false,
    "time_zone": null,
    "protected": false,
    "default_profile": true,
    "is_translator": false
  },
  "geo": null,
  "in_reply_to_user_id_str": null,
  "possibly_sensitive": false,
  "lang": "und",
  "created_at": "Mon Apr 03 06:22:18 +0000 2017",
  "in_reply_to_status_id_str": null,
  "place": null,
  "extended_entities": {
    "media": [
      {
        "expanded_url": "https:\/\/twitter.com\/skri_ar\/status\/848782692825128961\/photo\/1",
        "display_url": "pic.twitter.com\/mmlOawLdCy",
        "url": "https:\/\/t.co\/mmlOawLdCy",
        "media_url_https": "https:\/\/pbs.twimg.com\/media\/C8d7XE2XsAAiIsY.jpg",
        "id_str": "848782691151622144",
        "sizes": {
          "large": {
            "h": 618,
            "resize": "fit",
            "w": 986
          },
          "small": {
            "h": 426,
            "resize": "fit",
            "w": 680
          },
          "medium": {
            "h": 618,
            "resize": "fit",
            "w": 986
          },
          "thumb": {
            "h": 150,
            "resize": "crop",
            "w": 150
          }
        },
        "indices": [
          93,
          116
        ],
        "type": "photo",
        "id": 8.4878269115162e+17,
        "media_url": "http:\/\/pbs.twimg.com\/media\/C8d7XE2XsAAiIsY.jpg"
      }
    ]
  },
  "metadata": {
    "iso_language_code": "und",
    "result_type": "recent"
  }
}

现在我编写了一个scala脚本,它接受单个值并将其存储到Hbase中。但是,如果使用“text”值,则会抛出类似org.json4s.package$MappingException: Did not find value which can be converted into java.lang.String的错误

解析JSON文件的scala脚本如下:

def parse_json(json_string: String, collection_name: String, collection_Id: String ):(ImmutableBytesWritable, Put) = {

    val json_object = parse(json_string)
    //printIn(json_object)
    val id = (json_object \ "id_str").extract[String]
    val favorite_count = (json_object \ "favorite_count").extract[String]
    val retweet_count = (json_object \ "retweet_count").extract[String]
    val text = (json_object \ "text").extract[String]
put.add(Bytes.toBytes("tweet"),Bytes.toBytes("text"), Bytes.toBytes(text))

请帮我解决此错误,因为如果我评论文字行,所有其他字段都已正确插入HBase

1 个答案:

答案 0 :(得分:0)

最佳extract[],其类型与JSON完全相同。如果您需要其他类型,您仍然可以转换它。看到favorite_count是一个Int(没有引号):

  "favorite_count": 0,
...
  "retweet_count": 0,

最好用

val favorite_count = (json_object \ "favorite_count").extract[Int]
val retweet_count = (json_object \ "retweet_count").extract[Int]

或者如果您确实需要它们作为字符串添加.toString

希望这会有所帮助。 根据@SergGr的建议,添加try / catch总是一个好主意。