我们说:我的位置类似smb:// cluster / something /我想连接到该位置,并希望将文件从该文件夹加载到spark中。
是否有可能,我可以做,或者Spark仅支持hdfs系统?
String user = "username";
String password = "password";
String path = "smb://cluster14/g/SNBFIle.xlsx";
NtlmPasswordAuthentication auth = new
NtlmPasswordAuthentication("",user,password);
SmbFile smbFile = new SmbFile(path, auth);
InputStream is = smbFile.getInputStream();
File targetFile = new File("test/resources/data/file.xlsx");
FileUtils.copyInputStreamToFile(is, targetFile);
Workbook workbook = WorkbookFactory.create(targetFile);
for(Sheet sheet : workbook) {
String sheetName = sheet.getSheetName();
System.out.println(sheetName);
Dataset<Row> excelDataSet =
dataLoaderContext.getSparkSession().read()
.format("com.crealytics.spark.excel")
.option("dataAddress", sheetName.trim() + "!A1")
.option("header", "true")
.option("useHeader", "true")
.option("treatEmptyValuesAsNulls", "true")
.option("addColorColumns", "false")`enter code here`
.load("test/resources/data/file.xlsx");
datasetList.add(excelDataSet);
}
datasetList.forEach(dataset -> System.out.println(dataset.count()));
您可以在Spark应用程序中执行任何Java,Scala,Python或R代码,因此可以使用可用于这些语言的任何常见SMB库。
例如,以下使用pysmb库的Python代码段也将在Spark中运行:
from smb.SMBConnection import SMBConnection
username = "..."
password = "..."
client_name = "..."
server_name = "..."
server_ip = "..."
share_name = "..."
conn = SMBConnection(username, password, client_name, server_name, use_ntlm_v2 = True)
assert conn.connect(server_ip, 139)
for f in conn.listPath(share_name, "."):
print(f.filename)
该代码将直接在驱动程序上执行,因此您不能真正将这种方法用于大量数据,但是对于较小的文件,这是一个完全可行的选择。您可以先将文件读入驱动程序内存,然后将它们并行化,以便它们可以与您的其他数据集结合在一起。