删除分区

使用 dropPartition 函数删除指定分区的所有数据。

通过指定 dropPartition 函数的的第二个参数 partitionPaths,可以灵活删除分区数据。

  • dropPartition 可以删除指定的分区,此时 partitionPaths 是以 “/” 开头的字符串标量或向量,表示数据库目录下要删除的单个或多个分区的路径。

  • dropPartition 可以删除符合指定条件的分区,此时 partitionPaths 表示过滤条件。对于单层分区的数据库,partitionPaths 是一个值或向量,系统会删除包含该值或向量中元素的分区。对于组合分区的数据库,partitionPaths 是多个过滤条件组成的元组,每层分区对应元组中的一个元素,如果某层分区不需要过滤,那么对应的过滤条件为空。

$ n=1000000
$ ID=rand(150, n)
$ dates=2017.08.07..2017.08.11
$ date=rand(dates, n)
$ x=rand(10.0, n)
$ t=table(ID, date, x)
$ dbDate = database(, VALUE, 2017.08.07..2017.08.11)
$ dbID = database(, RANGE, 0 50 100 150)
$ db = database("dfs://compoDB", COMPO, [dbDate, dbID])
$ pt = db.createPartitionedTable(t, `pt, `date`ID)
$ pt.append!(t);

上面的代码创建了组合分区的数据库,第一层分区为基于日期的值分区,第二层分区为基于值的范围分区。

例 1. 删除某个分区

例如,删除表 pt 的 “/20170807/0_50” 分区,有以下两种方法:

(1) 指定路径:

$ dropPartition(db, "/20170807/0_50", tableName=`pt);

(2) 指定条件:

$ dropPartition(db, [2017.08.07, 0], tableName=`pt);

请注意,”/20170807/0_50” 分区中的 ID 的可取值范围是从 0 到 49,不包括 50。以上脚本中,可以使用 0 到 49 的任一数字来代表此分区。

例 2. 删除一级分区

例如,删除表 pt 的一级分区 2017.08.08,有以下两种方法:

(1) 使用向量指定该一级分区之下所有分区的路径:

$ partitions=["/20170808/0_50","/20170808/50_100","/20170808/100_150"]
$ dropPartition(db, partitions, tableName=`pt);

(2) 指定条件:

$ dropPartition(db, 2017.08.08, tableName=`pt);

例 3. 删除二级分区

例如,删除表 pt 的二级分区 [0,50),有以下两种方法:

(1) 使用向量指定含有该二级分区的所有分区的路径:

$ partitions=["/20170807/0_50","/20170808/0_50","/20170809/0_50","/20170810/0_50","/20170811/0_50"]
$ dropPartition(db, partitions, tableName=`pt);

(2) 指定条件:

$ dropPartition(db, [,[0]], tableName=`pt);

例 4. 删除跨多个分区的数据

例如,删除表 pt 的二级分区 [0,50) 和[100,150):

$ dropPartition(db, [,[0,100]], tableName=`pt);

existsPartition 函数可以检查某个分区是否存在。例如,检查数据库 “dfs://compoDB” 中表 pt 对应的 “/20170807/0_50” 分区是否存在:

$ listTables("dfs://compoDB")

tableName

physicalIndex

pt

408

$ existsPartition("dfs://compoDB/20170807/0_50/408")
true