mirror of
https://github.com/apache/zeppelin
synced 2026-05-24 09:38:26 +00:00
ZEPPELIN-198 Added completion functionality to HDFSInterpreter
This commit is contained in:
parent
32ed7cb3a6
commit
b505391326
3 changed files with 125 additions and 36 deletions
|
|
@ -91,7 +91,7 @@ public abstract class FileInterpreter extends Interpreter {
|
|||
|
||||
// Combine paths, takes care of arguments such as ..
|
||||
|
||||
private String getNewPath(String argument){
|
||||
protected String getNewPath(String argument){
|
||||
Path arg = Paths.get(argument);
|
||||
Path ret = arg.isAbsolute() ? arg : Paths.get(currentDir, argument);
|
||||
return ret.normalize().toString();
|
||||
|
|
@ -125,12 +125,12 @@ public abstract class FileInterpreter extends Interpreter {
|
|||
} else if (args.command.equals("ls")) {
|
||||
|
||||
String newPath = !args.args.isEmpty() ? getNewPath(args.args.get(0)) : currentDir;
|
||||
if (!isDirectory(newPath))
|
||||
return new InterpreterResult(Code.ERROR, Type.TEXT,
|
||||
newPath + ": No such file or directory");
|
||||
|
||||
String results = listAll(newPath);
|
||||
return new InterpreterResult(Code.SUCCESS, Type.TEXT, results);
|
||||
try {
|
||||
String results = listAll(newPath);
|
||||
return new InterpreterResult(Code.SUCCESS, Type.TEXT, results);
|
||||
} catch (Exception e) {
|
||||
return new InterpreterResult(Code.ERROR, Type.TEXT, e.getMessage());
|
||||
}
|
||||
|
||||
} else if (args.command.equals("pwd")) {
|
||||
|
||||
|
|
|
|||
|
|
@ -19,9 +19,10 @@
|
|||
package org.apache.zeppelin.file;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Properties;
|
||||
import java.util.*;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.zeppelin.interpreter.Interpreter;
|
||||
import org.apache.zeppelin.interpreter.InterpreterException;
|
||||
import org.apache.zeppelin.interpreter.InterpreterPropertyBuilder;
|
||||
|
|
@ -83,21 +84,21 @@ public class HDFSFileInterpreter extends FileInterpreter {
|
|||
public int storagePolicy;
|
||||
public String type;
|
||||
public String toString() {
|
||||
String str = "";
|
||||
str += "\nAccessTime = " + accessTime;
|
||||
str += "\nBlockSize = " + blockSize;
|
||||
str += "\nChildrenNum = " + childrenNum;
|
||||
str += "\nFileId = " + fileId;
|
||||
str += "\nGroup = " + group;
|
||||
str += "\nLength = " + length;
|
||||
str += "\nModificationTime = " + modificationTime;
|
||||
str += "\nOwner = " + owner;
|
||||
str += "\nPathSuffix = " + pathSuffix;
|
||||
str += "\nPermission = " + permission;
|
||||
str += "\nReplication = " + replication;
|
||||
str += "\nStoragePolicy = " + storagePolicy;
|
||||
str += "\nType = " + type;
|
||||
return str;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("\nAccessTime = " + accessTime);
|
||||
sb.append("\nBlockSize = " + blockSize);
|
||||
sb.append("\nChildrenNum = " + childrenNum);
|
||||
sb.append("\nFileId = " + fileId);
|
||||
sb.append("\nGroup = " + group);
|
||||
sb.append("\nLength = " + length);
|
||||
sb.append("\nModificationTime = " + modificationTime);
|
||||
sb.append("\nOwner = " + owner);
|
||||
sb.append("\nPathSuffix = " + pathSuffix);
|
||||
sb.append("\nPermission = " + permission);
|
||||
sb.append("\nReplication = " + replication);
|
||||
sb.append("\nStoragePolicy = " + storagePolicy);
|
||||
sb.append("\nType = " + type);
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -198,28 +199,46 @@ public class HDFSFileInterpreter extends FileInterpreter {
|
|||
return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
|
||||
}
|
||||
|
||||
public String listFile(String filePath) {
|
||||
try {
|
||||
String str = cmd.runCommand(cmd.getFileStatus, filePath, null);
|
||||
SingleFileStatus sfs = gson.fromJson(str, SingleFileStatus.class);
|
||||
if (sfs != null) {
|
||||
return ListOne(filePath, sfs.FileStatus);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("listFile: " + filePath, e);
|
||||
}
|
||||
return "No such File or directory";
|
||||
}
|
||||
|
||||
public String listAll(String path) {
|
||||
String all = "";
|
||||
if (exceptionOnConnect != null)
|
||||
return all;
|
||||
return "Error connecting to provided endpoint.";
|
||||
try {
|
||||
String sfs = listDir(path);
|
||||
if (sfs != null) {
|
||||
AllFileStatus allFiles = gson.fromJson(sfs, AllFileStatus.class);
|
||||
//see if directory.
|
||||
if (isDirectory(path)) {
|
||||
String sfs = listDir(path);
|
||||
if (sfs != null) {
|
||||
AllFileStatus allFiles = gson.fromJson(sfs, AllFileStatus.class);
|
||||
|
||||
if (allFiles != null &&
|
||||
allFiles.FileStatuses != null &&
|
||||
allFiles.FileStatuses.FileStatus != null)
|
||||
{
|
||||
for (OneFileStatus fs : allFiles.FileStatuses.FileStatus)
|
||||
all = all + ListOne(path, fs) + '\n';
|
||||
if (allFiles != null &&
|
||||
allFiles.FileStatuses != null &&
|
||||
allFiles.FileStatuses.FileStatus != null)
|
||||
{
|
||||
for (OneFileStatus fs : allFiles.FileStatuses.FileStatus)
|
||||
all = all + ListOne(path, fs) + '\n';
|
||||
}
|
||||
}
|
||||
return all;
|
||||
} else {
|
||||
return listFile(path);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("listall: listDir " + path, e);
|
||||
throw new InterpreterException("Could not find file or directory:\t" + path);
|
||||
}
|
||||
return all;
|
||||
}
|
||||
|
||||
public boolean isDirectory(String path) {
|
||||
|
|
@ -234,8 +253,78 @@ public class HDFSFileInterpreter extends FileInterpreter {
|
|||
} catch (Exception e) {
|
||||
logger.error("IsDirectory: " + path, e);
|
||||
return false;
|
||||
// throw new InterpreterException(path + " is not a directory.");
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<String> completion(String buf, int cursor) {
|
||||
logger.info("Completion request at position\t" + cursor + " in string " + buf);
|
||||
final List<String> suggestions = new ArrayList<>();
|
||||
if (StringUtils.isEmpty(buf)) {
|
||||
suggestions.add("ls");
|
||||
suggestions.add("cd");
|
||||
suggestions.add("pwd");
|
||||
return suggestions;
|
||||
}
|
||||
|
||||
//part of a command == no spaces
|
||||
if (buf.split(" ").length == 1){
|
||||
if ("cd".contains(buf)) suggestions.add("cd");
|
||||
if ("ls".contains(buf)) suggestions.add("ls");
|
||||
if ("pwd".contains(buf)) suggestions.add("pwd");
|
||||
|
||||
return suggestions;
|
||||
}
|
||||
|
||||
|
||||
// last word will contain the path we're working with.
|
||||
String lastToken= buf.substring(buf.lastIndexOf(" ") + 1);
|
||||
if (lastToken.startsWith("-")) { //flag not path
|
||||
return null;
|
||||
}
|
||||
|
||||
String localPath = ""; //all things before the last '/'
|
||||
String unfinished = lastToken; //unfished filenames or directories
|
||||
if (lastToken.contains("/")) {
|
||||
localPath = lastToken.substring(0, lastToken.lastIndexOf('/') + 1);
|
||||
unfinished = lastToken.substring(lastToken.lastIndexOf('/') + 1);
|
||||
}
|
||||
String globalPath = getNewPath(localPath); //adjust for cwd
|
||||
|
||||
if (isDirectory(globalPath)){
|
||||
try {
|
||||
String fileStatusString = listDir(globalPath);
|
||||
if (fileStatusString != null) {
|
||||
AllFileStatus allFiles = gson.fromJson(fileStatusString, AllFileStatus.class);
|
||||
|
||||
if (allFiles != null &&
|
||||
allFiles.FileStatuses != null &&
|
||||
allFiles.FileStatuses.FileStatus != null)
|
||||
{
|
||||
for (OneFileStatus fs : allFiles.FileStatuses.FileStatus) {
|
||||
if (fs.pathSuffix.contains(unfinished)) {
|
||||
|
||||
//only suggest the text after the last .
|
||||
String beforeLastPeriod = unfinished.substring(0, unfinished.lastIndexOf('.') + 1);
|
||||
//beforeLastPeriod should be the start of fs.pathSuffix, so take the end of it.
|
||||
String suggestedFinish = fs.pathSuffix.substring(beforeLastPeriod.length());
|
||||
suggestions.add(suggestedFinish);
|
||||
}
|
||||
}
|
||||
return suggestions;
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("listall: listDir " + globalPath, e);
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
logger.info("path is not a directory. No values suggested.");
|
||||
}
|
||||
|
||||
//Error in string.
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue