[GitHub] [carbondata] ravipesala commented on a change in pull request #3408: [CARBONDATA-3543] Supporting segment move command in carbon

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[GitHub] [carbondata] ravipesala commented on a change in pull request #3408: [CARBONDATA-3543] Supporting segment move command in carbon

GitBox
ravipesala commented on a change in pull request #3408: [CARBONDATA-3543] Supporting segment move command in carbon
URL: https://github.com/apache/carbondata/pull/3408#discussion_r334739825
 
 

 ##########
 File path: integration/spark2/src/main/scala/org/apache/spark/sql/execution/command/management/CarbonMoveExternalLoadCommand.scala
 ##########
 @@ -0,0 +1,108 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.spark.sql.execution.command.management
+
+import scala.collection.JavaConverters._
+
+import org.apache.commons.lang3.StringUtils
+import org.apache.spark.rdd.CarbonCopyFilesRDD
+import org.apache.spark.sql.{AnalysisException, CarbonEnv, Row, SparkSession}
+import org.apache.spark.sql.execution.command.{Checker, DataCommand}
+
+import org.apache.carbondata.common.exceptions.sql.MalformedCarbonCommandException
+import org.apache.carbondata.core.datamap.Segment
+import org.apache.carbondata.core.datastore.impl.FileFactory
+import org.apache.carbondata.core.exception.ConcurrentOperationException
+import org.apache.carbondata.core.metadata.SegmentFileStore
+import org.apache.carbondata.core.statusmanager.{SegmentStatus, SegmentStatusManager}
+import org.apache.carbondata.core.util.path.CarbonTablePath
+
+
+/**
+ * User can add external data folder as a segment to a transactional table.
+ * In case of external carbon data folder user no need to specify the format in options. But for
+ * other formats like parquet user must specify the format=parquet in options.
+ */
+case class CarbonMoveExternalLoadCommand(
+    databaseNameOp: Option[String],
+    tableName: String,
+    segmentName: String)
+  extends DataCommand {
+
+  override def processData(sparkSession: SparkSession): Seq[Row] = {
+    Checker.validateTableExists(databaseNameOp, tableName, sparkSession)
+    val carbonTable = CarbonEnv.getCarbonTable(databaseNameOp, tableName)(sparkSession)
+    setAuditTable(carbonTable)
+    if (!carbonTable.getTableInfo.isTransactionalTable) {
+      throw new MalformedCarbonCommandException("Unsupported operation on non transactional table")
+    }
+
+    // if insert overwrite in progress, do not allow add segment
+    if (SegmentStatusManager.isOverwriteInProgressInTable(carbonTable)) {
+      throw new ConcurrentOperationException(carbonTable, "insert overwrite", "move segment")
+    }
+
+    val details =
+      SegmentStatusManager.readLoadMetadata(
+        CarbonTablePath.getMetadataPath(carbonTable.getTablePath))
+    val detail = details.find(_.getLoadName.equalsIgnoreCase(segmentName))
+      .getOrElse(throw new AnalysisException(s"Segment name $segmentName doesn't exist"))
+
+    if (StringUtils.isEmpty(detail.getPath)) {
+      throw new AnalysisException(s"Segment $segmentName is not a external segment")
+    }
+
+    val store = new SegmentFileStore(carbonTable.getTablePath, detail.getSegmentFile)
+    val location = store.getLocationMap.asScala.head._1
+    val destLocation = CarbonTablePath.getSegmentPath(carbonTable.getTablePath, segmentName)
+    val configuration = sparkSession.sessionState.newHadoopConf()
+    FileFactory.mkdirs(destLocation, configuration)
+    val segment = new Segment(segmentName,
+      detail.getSegmentFile,
+      location,
+      store.getSegmentFile.getOptions)
+
+    new CarbonCopyFilesRDD(sparkSession, segment, destLocation).collect()
+    store.getSegmentFile.getOptions.put("path", destLocation)
+    val segmentDest = new Segment(segmentName,
+      SegmentFileStore.genSegmentFileName(detail.getLoadName,
+        System.nanoTime().toString) + CarbonTablePath.SEGMENT_EXT,
+      destLocation,
+      store.getSegmentFile.getOptions)
+
+    val isSuccess =
+      SegmentFileStore.writeSegmentFile(carbonTable, segmentDest)
 
 Review comment:
   Segment file always writes to a new file, it doesn't overwrite the existing file.

----------------------------------------------------------------
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:
[hidden email]


With regards,
Apache Git Services