rdblue commented on a change in pull request #374: Migrate spark table to
iceberg table
URL: https://github.com/apache/incubator-iceberg/pull/374#discussion_r317710578
##########
File path: spark/src/main/scala/org/apache/iceberg/spark/SparkTableUtil.scala
##########
@@ -297,5 +302,81 @@ object SparkTableUtil {
)
}
}
+
+ private def buildManifest(table: Table,
+ sparkDataFiles: Seq[SparkDataFile],
+ partitionSpec: PartitionSpec): ManifestFile = {
+ val outputFile = table.io
+ .newOutputFile(FileFormat.AVRO.addExtension("/tmp/" +
UUID.randomUUID.toString))
+ val writer = ManifestWriter.write(partitionSpec, outputFile)
+ try {
+ sparkDataFiles.foreach { file =>
+ writer.add(file.toDataFile(partitionSpec))
+ }
+ } finally {
+ writer.close()
+ }
+
+ writer.toManifestFile
+ }
+
+ /**
+ * Import a spark table to a iceberg table.
+ *
+ * The import uses the spark session to get table metadata. It assumes no
+ * operation is going on original table and target table and thus is not
+ * thread-safe.
+ *
+ * @param source the database name of the table to be import
+ * @param location the location used to store table metadata
+ *
+ * @return table the imported table
+ */
+ def importSparkTable(source: TableIdentifier, location: String): Table = {
+ val sparkSession = SparkSession.builder().getOrCreate()
+ import sparkSession.sqlContext.implicits._
+
+ val dbName = source.database.getOrElse("default")
+ val tableName = source.table
+
+ if (!sparkSession.catalog.tableExists(dbName, tableName)) {
+ throw new NoSuchTableException(s"Table $dbName.$tableName does not
exist")
+ }
+
+ val partitionSpec = SparkSchemaUtil.specForTable(sparkSession,
s"$dbName.$tableName")
+ val conf = sparkSession.sparkContext.hadoopConfiguration
+ val tables = new HadoopTables(conf)
+ val schema = SparkSchemaUtil.schemaForTable(sparkSession,
s"$dbName.$tableName")
+ val table = tables.create(schema, partitionSpec, ImmutableMap.of(),
location)
+ val appender = table.newAppend()
+
+ if (partitionSpec == PartitionSpec.unpartitioned) {
+ val tableMetadata =
sparkSession.sessionState.catalog.getTableMetadata(source)
+ val format = tableMetadata.provider.getOrElse("none")
+
+ if (format != "avro" && format != "parquet" && format != "orc") {
+ throw new UnsupportedOperationException(s"Unsupported format: $format")
+ }
+ listPartition(Map.empty[String, String], tableMetadata.location.toString,
+ format).foreach{f =>
appender.appendFile(f.toDataFile(PartitionSpec.unpartitioned))}
+ appender.commit()
+ } else {
+ val partitions = partitionDF(sparkSession, s"$dbName.$tableName")
+ partitions.flatMap { row =>
+ listPartition(row.getMap[String, String](0).toMap, row.getString(1),
row.getString(2))
+ }.coalesce(1).mapPartitions {
Review comment:
Why `coalesce(1)` here?
In our version of this, we add a sort by file name and build manifests in
parallel:
```scala
val tempPath = new
Path(s"hdfs:/tmp/iceberg-conversions/$applicationId")
val manifests: Seq[ManifestFile] = files
.repartition(100) // repartition to shuffle the data and not
list partitions twice
.orderBy($"path")
.mapPartitions(writeManifest) // writes manifests to tempPath
.collect()
.map(_.toManifestFile)
val append = table.newAppend
manifests.foreach(append.appendManifest)
append.commit()
```
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]