-
Notifications
You must be signed in to change notification settings - Fork 513
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add IoCommands #72
add IoCommands #72
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
/* | ||
* Copyright 2016 Spotify AB. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.spotify.scio.repl | ||
|
||
import java.io._ | ||
import java.nio.channels.Channels | ||
|
||
import com.google.cloud.dataflow.sdk.options.PipelineOptions | ||
import com.google.cloud.dataflow.sdk.util.GcsUtil | ||
import com.google.cloud.dataflow.sdk.util.GcsUtil.GcsUtilFactory | ||
import com.google.cloud.dataflow.sdk.util.gcsfs.GcsPath | ||
import com.spotify.scio.util.ScioUtil | ||
import kantan.csv.{RowDecoder, RowEncoder} | ||
import org.apache.avro.file.{DataFileStream, DataFileWriter} | ||
import org.apache.avro.generic.{GenericDatumReader, GenericDatumWriter, GenericRecord} | ||
import org.apache.avro.specific.{SpecificDatumReader, SpecificDatumWriter, SpecificRecordBase} | ||
import org.apache.commons.io.{Charsets, IOUtils} | ||
import org.apache.hadoop.conf.Configuration | ||
import org.apache.hadoop.fs.{FileSystem, Path} | ||
|
||
import scala.collection.JavaConverters._ | ||
import scala.reflect.ClassTag | ||
|
||
/** Commands for simple file I/O in the REPL. */ | ||
// scalastyle:off regex | ||
class IoCommands(options: PipelineOptions) { | ||
|
||
private val TEXT = "text/plain" | ||
private val BINARY = "application/octet-stream" | ||
|
||
private val gcsUtil: GcsUtil = new GcsUtilFactory().create(options) | ||
private val fs = FileSystem.get(new Configuration()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something to keep in mind - probably not a problem here - but if someone was to use this functions in a long running service, configuration (on disk/resources) may change - thus you may need to refresh configuration. Just something to remember. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a problem here since it's in scio-repl, not intended for a pipeline job. |
||
|
||
// ======================================================================= | ||
// Read operations | ||
// ======================================================================= | ||
|
||
/** Read from an Avro file on local filesystem, GCS, or HDFS. */ | ||
def readAvro[T : ClassTag](path: String): Iterator[T] = { | ||
val cls = ScioUtil.classOf[T] | ||
val reader = if (classOf[SpecificRecordBase] isAssignableFrom cls) { | ||
new SpecificDatumReader[T]() | ||
} else { | ||
new GenericDatumReader[T]() | ||
} | ||
new DataFileStream[T](inputStream(path), reader).iterator().asScala | ||
} | ||
|
||
/** Read from a text file on local filesystem, GCS, or HDFS. */ | ||
def readText(path: String): Iterator[String] = | ||
IOUtils.lineIterator(inputStream(path), Charsets.UTF_8).asScala | ||
|
||
/** Read from a CSV file on local filesystem, GCS, or HDFS. */ | ||
def readCsv[T: RowDecoder](path: String, | ||
sep: Char = ',', | ||
header: Boolean = false): Iterator[T] = { | ||
import kantan.csv.ops._ | ||
implicit val codec = scala.io.Codec.UTF8 | ||
inputStream(path).asUnsafeCsvReader[T](sep, header).toIterator | ||
} | ||
|
||
/** Read from a TSV file on local filesystem, GCS, or HDFS. */ | ||
def readTsv[T: RowDecoder](path: String, | ||
sep: Char = '\t', | ||
header: Boolean = false): Iterator[T] = { | ||
import kantan.csv.ops._ | ||
implicit val codec = scala.io.Codec.UTF8 | ||
inputStream(path).asUnsafeCsvReader[T](sep, header).toIterator | ||
} | ||
|
||
// ======================================================================= | ||
// Write operations | ||
// ======================================================================= | ||
|
||
private def plural[T](data: Seq[T]): String = if (data.size > 1) "s" else "" | ||
|
||
/** Write to an Avro file on local filesystem, GCS, or HDFS. */ | ||
def writeAvro[T: ClassTag](path: String, data: Seq[T]): Unit = { | ||
val cls = ScioUtil.classOf[T] | ||
val (writer, schema) = if (classOf[SpecificRecordBase] isAssignableFrom cls) { | ||
(new SpecificDatumWriter[T](cls), data.head.asInstanceOf[SpecificRecordBase].getSchema) | ||
} else { | ||
(new GenericDatumWriter[T](), data.head.asInstanceOf[GenericRecord].getSchema) | ||
} | ||
val fileWriter = new DataFileWriter[T](writer).create(schema, outputStream(path, BINARY)) | ||
data.foreach(fileWriter.append) | ||
fileWriter.close() | ||
println(s"${data.size} record${plural(data)} written to $path") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Log? |
||
} | ||
|
||
/** Write to a text file on local filesystem, GCS, or HDFS. */ | ||
def writeText(path: String, data: Seq[String]): Unit = { | ||
IOUtils.writeLines(data.asJava, IOUtils.LINE_SEPARATOR, outputStream(path, TEXT)) | ||
println(s"${data.size} line${plural(data)} written to $path") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be a log? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Log will also print out a bunch of junk we don't need. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But log you can control, can't see a reason for |
||
} | ||
|
||
/** Write to a CSV file on local filesystem, GCS, or HDFS. */ | ||
def writeCsv[T: RowEncoder](path: String, data: Seq[T], | ||
sep: Char = ',', | ||
header: Seq[String] = Seq.empty): Unit = { | ||
import kantan.csv.ops._ | ||
IOUtils.write(data.asCsv(sep, header), outputStream(path, TEXT)) | ||
} | ||
|
||
/** Write to a TSV file on local filesystem, GCS, or HDFS. */ | ||
def writeTsv[T: RowEncoder](path: String, data: Seq[T], | ||
sep: Char = '\t', | ||
header: Seq[String] = Seq.empty): Unit = { | ||
import kantan.csv.ops._ | ||
IOUtils.write(data.asCsv(sep, header), outputStream(path, TEXT)) | ||
} | ||
|
||
// ======================================================================= | ||
// Utilities | ||
// ======================================================================= | ||
|
||
private def inputStream(path: String): InputStream = | ||
if (path.startsWith("hdfs://")) { | ||
fs.open(new Path(path)) | ||
} else if (path.startsWith("gs://")) { | ||
Channels.newInputStream(gcsUtil.open(GcsPath.fromUri(path))) | ||
} else { | ||
new FileInputStream(path) | ||
} | ||
|
||
private def outputStream(path: String, contentType: String): OutputStream = | ||
if (path.startsWith("hdfs://")) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have seen a couple of these - here and there - should we have a function that checks if paths is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason not to support all hadoop supported schemas via https://hadoop.apache.org/docs/r2.7.2/api/org/apache/hadoop/fs/FileSystem.html#get(java.net.URI, org.apache.hadoop.conf.Configuration) ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I should do that but I'm putting this PR on hold since adding hdfs dep may cause other problems. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sisidra the only problem with that is that then you need to depend on hadoop deps - and that is something we mostly want to stay away from (in like |
||
fs.create(new Path(path), false) | ||
} else if (path.startsWith("gs://")) { | ||
Channels.newOutputStream(gcsUtil.create(GcsPath.fromUri(path), contentType)) | ||
} else { | ||
new FileOutputStream(path) | ||
} | ||
|
||
} | ||
// scalastyle:on regex |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -227,13 +227,22 @@ class ScioILoop(scioClassLoader: ScioReplClassLoader, | |
} | ||
} | ||
|
||
private def loadIoCommands(): IR.Result = { | ||
intp.interpret( | ||
""" | ||
|val _ioCommands = new com.spotify.scio.repl.IoCommands(sc.options) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So if I would like to use IOCommands with different opts - i have to manually create it myself right? And by default you get the IOCommands from your startup opts? |
||
|import _ioCommands._ | ||
""".stripMargin) | ||
} | ||
|
||
override def createInterpreter(): Unit = { | ||
super.createInterpreter() | ||
welcome() | ||
intp.beQuietDuring { | ||
addImports() | ||
createBigQueryClient() | ||
newScioCmdImpl("sc") | ||
loadIoCommands() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we should think about doing some of this stuff async to save on startup time? Probably doesn't make sense here (yet) - but I think spark and scala repls is doing some stuff in async fashion to help with startup time. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't be a huge difference? Let's not over optimize for now? |
||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm kinda on the edge on this one - let's merge but - I think we will remove it next time we have an issue with repl and all the HDFS deps.