[HUDI-708] Add temps show and unit test for TempViewCommand (#1770)
This commit is contained in:
@@ -115,4 +115,16 @@ public class HoodieCLI {
|
||||
return tempViewProvider;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close tempViewProvider.
|
||||
* <p/>
|
||||
* For test, avoid multiple SparkContexts.
|
||||
*/
|
||||
public static synchronized void closeTempViewProvider() {
|
||||
if (tempViewProvider != null) {
|
||||
tempViewProvider.close();
|
||||
tempViewProvider = null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -20,36 +20,55 @@ package org.apache.hudi.cli.commands;
|
||||
|
||||
import org.apache.hudi.cli.HoodieCLI;
|
||||
|
||||
import org.apache.hudi.exception.HoodieException;
|
||||
import org.springframework.shell.core.CommandMarker;
|
||||
import org.springframework.shell.core.annotation.CliCommand;
|
||||
import org.springframework.shell.core.annotation.CliOption;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* CLI command to query/delete temp views.
|
||||
*/
|
||||
@Component
|
||||
public class TempViewCommand implements CommandMarker {
|
||||
|
||||
private static final String EMPTY_STRING = "";
|
||||
public static final String QUERY_SUCCESS = "Query ran successfully!";
|
||||
public static final String QUERY_FAIL = "Query ran failed!";
|
||||
public static final String SHOW_SUCCESS = "Show all views name successfully!";
|
||||
|
||||
@CliCommand(value = "temp_query", help = "query against created temp view")
|
||||
@CliCommand(value = {"temp_query", "temp query"}, help = "query against created temp view")
|
||||
public String query(
|
||||
@CliOption(key = {"sql"}, mandatory = true, help = "select query to run against view") final String sql)
|
||||
throws IOException {
|
||||
@CliOption(key = {"sql"}, mandatory = true, help = "select query to run against view") final String sql) {
|
||||
|
||||
try {
|
||||
HoodieCLI.getTempViewProvider().runQuery(sql);
|
||||
return QUERY_SUCCESS;
|
||||
} catch (HoodieException ex) {
|
||||
return QUERY_FAIL;
|
||||
}
|
||||
|
||||
HoodieCLI.getTempViewProvider().runQuery(sql);
|
||||
return EMPTY_STRING;
|
||||
}
|
||||
|
||||
@CliCommand(value = "temp_delete", help = "Delete view name")
|
||||
public String delete(
|
||||
@CliOption(key = {"view"}, mandatory = true, help = "view name") final String tableName)
|
||||
throws IOException {
|
||||
@CliCommand(value = {"temps_show", "temps show"}, help = "Show all views name")
|
||||
public String showAll() {
|
||||
|
||||
HoodieCLI.getTempViewProvider().deleteTable(tableName);
|
||||
return EMPTY_STRING;
|
||||
try {
|
||||
HoodieCLI.getTempViewProvider().showAllViews();
|
||||
return SHOW_SUCCESS;
|
||||
} catch (HoodieException ex) {
|
||||
return "Show all views name failed!";
|
||||
}
|
||||
}
|
||||
|
||||
@CliCommand(value = {"temp_delete", "temp delete"}, help = "Delete view name")
|
||||
public String delete(
|
||||
@CliOption(key = {"view"}, mandatory = true, help = "view name") final String tableName) {
|
||||
|
||||
try {
|
||||
HoodieCLI.getTempViewProvider().deleteTable(tableName);
|
||||
return String.format("Delete view %s successfully!", tableName);
|
||||
} catch (HoodieException ex) {
|
||||
return String.format("Delete view %s failed!", tableName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,6 +101,17 @@ public class SparkTempViewProvider implements TempViewProvider {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showAllViews() {
|
||||
try {
|
||||
sqlContext.sql("SHOW TABLES").show(Integer.MAX_VALUE, false);
|
||||
} catch (Throwable ex) {
|
||||
// log full stack trace and rethrow. Without this its difficult to debug failures, if any
|
||||
LOG.error("unable to get all views ", ex);
|
||||
throw new HoodieException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteTable(String tableName) {
|
||||
try {
|
||||
@@ -112,6 +123,13 @@ public class SparkTempViewProvider implements TempViewProvider {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
if (sqlContext != null) {
|
||||
sqlContext.sparkSession().stop();
|
||||
}
|
||||
}
|
||||
|
||||
private DataType getDataType(Comparable comparable) {
|
||||
if (comparable instanceof Integer) {
|
||||
return DataTypes.IntegerType;
|
||||
|
||||
@@ -18,12 +18,17 @@
|
||||
|
||||
package org.apache.hudi.cli.utils;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.util.List;
|
||||
|
||||
public interface TempViewProvider {
|
||||
public interface TempViewProvider extends Closeable {
|
||||
void createOrReplace(String tableName, List<String> headers, List<List<Comparable>> rows);
|
||||
|
||||
void runQuery(String sqlText);
|
||||
|
||||
void showAllViews();
|
||||
|
||||
void deleteTable(String tableName);
|
||||
|
||||
void close();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user