You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Flink流应用中解码含Unicode转义的Twitter JSON推文

Hey there! Let's figure out how to get those Greek and special characters showing up correctly instead of those \u03c6-style Unicode escapes in your Flink output. The issue here is that the TwitterSource is emitting raw JSON strings with escaped Unicode characters, and Flink's default print() and writeAsText() just spit that raw string out without parsing the escapes. Here's how to fix it:

Step 1: Parse the JSON to Resolve Escapes

The easiest way to handle this is to use a JSON parsing library to convert the raw string into a structured object, which automatically resolves the Unicode escapes. Jackson is a great choice here (it's commonly used with Flink, so you might already have it in your dependencies).

Add Jackson Dependency (If Needed)

If your project doesn't already include Jackson, add this to your build.sbt:

libraryDependencies += "com.fasterxml.jackson.core" % "jackson-databind" % "2.15.2"

Update your stream processing logic to parse the JSON and extract the properly rendered text:

import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment
import org.apache.flink.streaming.connectors.twitter.TwitterSource
import com.fasterxml.jackson.databind.ObjectMapper

object TwitterStreamProcessor {
  def main(args: Array[String]): Unit = {
    val env = StreamExecutionEnvironment.getExecutionEnvironment
    val props = // Your existing properties for Twitter API credentials

    val source = new TwitterSource(props)
    source.setCustomEndpointInitializer(myCustomEndpoint)
    val streamSource = env.addSource(source)

    // Initialize Jackson's ObjectMapper to parse JSON
    val objectMapper = new ObjectMapper()

    // Parse the raw JSON string and extract the rendered tweet text
    val parsedTweets = streamSource.map { rawJson =>
      val jsonNode = objectMapper.readTree(rawJson)
      // Extract just the tweet text (use this if you only want the text)
      jsonNode.get("text").asText()
      // OR, if you want the full JSON with properly rendered characters:
      // objectMapper.writeValueAsString(jsonNode)
    }

    // Print rendered tweets to stdout
    parsedTweets.print()

    // Write rendered tweets to a TXT file (note the Windows path format)
    parsedTweets.writeAsText(
      "file:///C:/test.txt", 
      org.apache.flink.core.fs.FileSystem.WriteMode.OVERWRITE
    )

    env.execute("Twitter Stream Processing")
  }
}

Step 2: Verify IntelliJ Console Encoding

If you still see garbled characters in the IntelliJ console after parsing, make sure your IDE is using UTF-8 encoding:

  • Go to File → Settings → Editor → File Encodings
  • Set IDE Encoding, Project Encoding, and Console Encoding all to UTF-8

Alternative: Use Commons Lang3 for String Unescaping

If you don't want to parse the full JSON structure and just need to unescape the entire string, you can use Apache Commons Lang3's StringEscapeUtils:

Add Commons Lang3 Dependency

libraryDependencies += "org.apache.commons" % "commons-lang3" % "3.12.0"

Update the Map Function

import org.apache.commons.lang3.StringEscapeUtils

val parsedTweets = streamSource.map { rawJson =>
  StringEscapeUtils.unescapeJson(rawJson)
}

This will convert all Unicode escape sequences in the raw JSON string to their actual characters, though it doesn't give you the flexibility to extract specific fields like Jackson does.

Key Notes for Windows Environments

  • When specifying the output file path, use file:///C:/test.txt (three forward slashes) or file:\\C:\\test.txt (double backslashes) to ensure Flink recognizes the Windows path correctly.
  • Ensure your output file is opened with a text editor that supports UTF-8 (like Notepad++) to see the special characters properly.

内容的提问来源于stack exchange,提问作者orestisf

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 07:47:15