Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,22 @@ public static void main(String[] args) {
final HoodieExampleDataGenerator<HoodieAvroPayload> dataGen = new HoodieExampleDataGenerator<>();

insertData(spark, jsc, tablePath, tableName, dataGen);
queryData(spark, jsc, tablePath, tableName, dataGen);

updateData(spark, jsc, tablePath, tableName, dataGen);
queryData(spark, jsc, tablePath, tableName, dataGen);

incrementalQuery(spark, tablePath, tableName);
pointInTimeQuery(spark, tablePath, tableName);

delete(spark, tablePath, tableName);
queryData(spark, jsc, tablePath, tableName, dataGen);

insertOverwriteData(spark, jsc, tablePath, tableName, dataGen);
queryData(spark, jsc, tablePath, tableName, dataGen);

deleteByPartition(spark, tablePath, tableName);
queryData(spark, jsc, tablePath, tableName, dataGen);
}
}

Expand All @@ -77,6 +85,7 @@ public static void insertData(SparkSession spark, JavaSparkContext jsc, String t
String commitTime = Long.toString(System.currentTimeMillis());
List<String> inserts = dataGen.convertToStringList(dataGen.generateInserts(commitTime, 20));
Dataset<Row> df = spark.read().json(jsc.parallelize(inserts, 1));

df.write().format("org.apache.hudi")
.options(QuickstartUtils.getQuickstartWriteConfigs())
.option(HoodieWriteConfig.PRECOMBINE_FIELD_NAME.key(), "ts")
Expand All @@ -87,6 +96,27 @@ public static void insertData(SparkSession spark, JavaSparkContext jsc, String t
.save(tablePath);
}

/**
* Generate new records, load them into a {@link Dataset} and insert-overwrite it into the Hudi dataset
*/
public static void insertOverwriteData(SparkSession spark, JavaSparkContext jsc, String tablePath, String tableName,
HoodieExampleDataGenerator<HoodieAvroPayload> dataGen) {
String commitTime = Long.toString(System.currentTimeMillis());
List<String> inserts = dataGen.convertToStringList(dataGen.generateInserts(commitTime, 20));
Dataset<Row> df = spark.read().json(jsc.parallelize(inserts, 1));

df.write().format("org.apache.hudi")
.options(QuickstartUtils.getQuickstartWriteConfigs())
.option("hoodie.datasource.write.operation", WriteOperationType.INSERT_OVERWRITE.name())
.option(HoodieWriteConfig.PRECOMBINE_FIELD_NAME.key(), "ts")
.option(KeyGeneratorOptions.RECORDKEY_FIELD_NAME.key(), "uuid")
.option(KeyGeneratorOptions.PARTITIONPATH_FIELD_NAME.key(), "partitionpath")
.option(TBL_NAME.key(), tableName)
.mode(Append)
.save(tablePath);
}


/**
* Load the data files into a DataFrame.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,21 @@
import java.io.File;
import java.nio.file.Paths;

import static org.apache.hudi.examples.quickstart.HoodieSparkQuickstart.delete;
import static org.apache.hudi.examples.quickstart.HoodieSparkQuickstart.deleteByPartition;
import static org.apache.hudi.examples.quickstart.HoodieSparkQuickstart.incrementalQuery;
import static org.apache.hudi.examples.quickstart.HoodieSparkQuickstart.insertData;
import static org.apache.hudi.examples.quickstart.HoodieSparkQuickstart.insertOverwriteData;
import static org.apache.hudi.examples.quickstart.HoodieSparkQuickstart.pointInTimeQuery;
import static org.apache.hudi.examples.quickstart.HoodieSparkQuickstart.queryData;
import static org.apache.hudi.examples.quickstart.HoodieSparkQuickstart.updateData;

public class TestHoodieSparkQuickstart implements SparkProvider {
protected static transient HoodieSparkEngineContext context;
protected static HoodieSparkEngineContext context;

private static transient SparkSession spark;
private static transient SQLContext sqlContext;
private static transient JavaSparkContext jsc;
private static SparkSession spark;
private static SQLContext sqlContext;
private static JavaSparkContext jsc;

/**
* An indicator of the initialization status.
Expand All @@ -50,8 +59,6 @@ public class TestHoodieSparkQuickstart implements SparkProvider {
@TempDir
protected java.nio.file.Path tempDir;

private static final HoodieExampleDataGenerator<HoodieAvroPayload> DATA_GEN = new HoodieExampleDataGenerator<>();

@Override
public SparkSession spark() {
return spark;
Expand Down Expand Up @@ -100,15 +107,25 @@ public void testHoodieSparkQuickstart() {
String tablePath = tablePath(tableName);

try {
HoodieSparkQuickstart.insertData(spark, jsc, tablePath, tableName, DATA_GEN);
HoodieSparkQuickstart.updateData(spark, jsc, tablePath, tableName, DATA_GEN);
final HoodieExampleDataGenerator<HoodieAvroPayload> dataGen = new HoodieExampleDataGenerator<>();

insertData(spark, jsc, tablePath, tableName, dataGen);
queryData(spark, jsc, tablePath, tableName, dataGen);

updateData(spark, jsc, tablePath, tableName, dataGen);
queryData(spark, jsc, tablePath, tableName, dataGen);

incrementalQuery(spark, tablePath, tableName);
pointInTimeQuery(spark, tablePath, tableName);

delete(spark, tablePath, tableName);
queryData(spark, jsc, tablePath, tableName, dataGen);

HoodieSparkQuickstart.queryData(spark, jsc, tablePath, tableName, DATA_GEN);
HoodieSparkQuickstart.incrementalQuery(spark, tablePath, tableName);
HoodieSparkQuickstart.pointInTimeQuery(spark, tablePath, tableName);
insertOverwriteData(spark, jsc, tablePath, tableName, dataGen);
queryData(spark, jsc, tablePath, tableName, dataGen);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Looks the same as the main method of HoodieSparkQuickstart. Could the same test flow as HoodieSparkQuickstart (insert, update, query, delete, etc.) be extracted? (can be a follow-up PR)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call!


HoodieSparkQuickstart.delete(spark, tablePath, tableName);
HoodieSparkQuickstart.deleteByPartition(spark, tablePath, tableName);
deleteByPartition(spark, tablePath, tableName);
queryData(spark, jsc, tablePath, tableName, dataGen);
} finally {
Utils.deleteRecursively(new File(tablePath));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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.hudi

class TestDeleteFromTable extends HoodieSparkSqlTestBase {

test("Test deleting from table") {
withTempDir { tmp =>
Seq("cow", "mor").foreach { tableType =>
val tableName = generateTableName
spark.sql(
s"""
|CREATE TABLE $tableName (
| id int,
| dt string,
| name string,
| price double,
| ts long
|) USING hudi
| tblproperties (
| primaryKey = 'id',
| tableType = '$tableType'
| )
| PARTITIONED BY (dt)
| LOCATION '${tmp.getCanonicalPath}'
""".stripMargin)

// NOTE: Do not write the field alias, the partition field must be placed last.
spark.sql(
s"""
| INSERT INTO $tableName VALUES
| (1, 'a1', 10, 1000, "2021-01-05"),
| (2, 'a2', 20, 2000, "2021-01-06"),
| (3, 'a3', 30, 3000, "2021-01-07")
""".stripMargin)

checkAnswer(s"SELECT id, name, price, ts, dt FROM $tableName")(
Seq(1, "a1", 10.0, 1000, "2021-01-05"),
Seq(2, "a2", 20.0, 2000, "2021-01-06"),
Seq(3, "a3", 30.0, 3000, "2021-01-07")
)

// Delete single row
spark.sql(s"DELETE FROM $tableName WHERE id = 1")

checkAnswer(s"SELECT id, name, price, ts, dt FROM $tableName")(
Seq(2, "a2", 20.0, 2000, "2021-01-06"),
Seq(3, "a3", 30.0, 3000, "2021-01-07")
)

// Try deleting non-existent row
spark.sql(s"DELETE FROM $tableName WHERE id = 1")

checkAnswer(s"SELECT id, name, price, ts, dt FROM $tableName")(
Seq(2, "a2", 20.0, 2000, "2021-01-06"),
Seq(3, "a3", 30.0, 3000, "2021-01-07")
)

// Delete record identified by some field other than the primary-key
spark.sql(s"DELETE FROM $tableName WHERE name = 'a2'")

checkAnswer(s"SELECT id, name, price, ts, dt FROM $tableName")(
Seq(3, "a3", 30.0, 3000, "2021-01-07")
)
}
}
}
}
35 changes: 35 additions & 0 deletions hudi-utilities/src/test/resources/hive-site.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<!--
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.
-->

<configuration>
<property>
<name>hive.server2.enable.doAs</name>
<value>false</value>
</property>

<property>
<name>hive.metastore.schema.verification</name>
<value>false</value>
</property>

<property>
<name>datanucleus.schema.autoCreateTables</name>
<value>true</value>
</property>
</configuration>
Comment on lines +20 to +35
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these changes needed in hudi-utilities module?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without them Hive tries to run DS tests on behalf of the user running them and fails (on Mac)