Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
xs4s/example/src/main/scala/xs4s/example/FindMostPopularWikipediaKeywordsZIOApp.scala
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
46 lines (40 sloc)
1.38 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package xs4s.example | |
import zio._ | |
import zio.console._ | |
import zio.stream._ | |
import zio.blocking.Blocking | |
import java.io.IOException | |
import java.io.InputStream | |
import java.net.URL | |
import java.net.URLConnection | |
import java.util.zip.GZIPInputStream | |
import scala.collection.JavaConverters._ | |
import xs4s.ziocompat._ | |
import xs4s.syntax.zio._ | |
object FindMostPopularWikipediaKeywordsZIOApp extends zio.App { | |
def run(args: List[String]): URIO[ZEnv, ExitCode] = { | |
wikipediaXmlBytes | |
.via(byteStream => byteStreamToXmlEventStream()(byteStream)) | |
.via(anchorExtractor.toZIOPipeThrowError(_)) | |
.via(stream => // by default, get a sub-set of the stream | |
if (args.contains("full")) stream else stream.take(500) | |
) | |
.map(_.text) | |
.via(countTopItemsZIO(_)) | |
.flatMap(counts => ZStream.fromIterable(counts)) | |
.foreach{ case (elem: String, count: Int) => putStrLn((count, elem).toString) } | |
.as(ExitCode.success) | |
.orDie | |
} | |
/** | |
* Can be any of your own XML byte stream | |
*/ | |
private val wikipediaXmlBytes: ZStream[Blocking, IOException, Byte] = { | |
val is: ZIO[Blocking, IOException, InputStream] = blocking.effectBlockingIO { | |
// TODO: Use ZIO gzip transducer after it ships: | |
// https://github.com/zio/zio/issues/3759 | |
new GZIPInputStream(wikipediaAbstractURL.openStream()) | |
} | |
Stream.fromInputStreamEffect(is, 1024) | |
} | |
} |