我正在尝试将数据保存为' csv'格式化文件或从solr查询中提取为数据帧,类似于:
http://localhost:8983/solr/techproducts/select?q=ipod&fl=id,cat,name,popularity,price,score&wt=csv
我可以手动从html文件(网页solr输出)中显示的csv输出中复制,粘贴,保存和检索csv格式的数据。但是,我无法使用R自动执行该过程。我无法通过unlist
解析并获取整个内容 - 以正确的顺序输出 - 也不提取并保存网页中显示的内容作为csv文件。我徒劳地尝试了solrium
和httr
个套餐。我还尝试在Windows PowerShell中使用Export-Csv
选项以csv文件格式保存数据而没有任何运气。
请求提出解决问题的建议。
答案 0 :(得分:2)
根据BenH的评论,它已经是CSV格式,因此无需使用Export-CSV
。只需使用-OutFile
上的Invoke-WebRequest
参数:
Invoke-RestMethod "http://localhost:8983/solr/techproducts/select?q=ipod&fl=id,cat,name,popularity,price,score&wt=csv" -OutFile YourFile.csv
答案 1 :(得分:1)
这并未解决有关powershell的任何具体内容,但作为使用solrium
的开始,这是否有效,如果没有,它是如何失败的:
(完全可重现),设置Solr
cd solr-6.6.0
bin/solr start -e cloud -noprompt
bin/solr create -c techproducts -d sample_techproducts_configs
bin/post -c techproducts example/exampledocs/*.xml
在R:
solr_connect()
(df <- solr_search(
name = "techproducts",
q = "ipod",
fl = c("id", "cat", "name", "popularity", "price", "score"),
wt = "csv"))
#> # A tibble: 3 x 6
#> id cat name popularity price score
#> * <chr> <chr> <chr> <int> <dbl> <dbl>
#> 1 IW-02 electronics,connector iPod & iPod Mini USB 2.0 Cable 1 11.50 3.2388113
#> 2 F8V7067-APL-KIT electronics,connector Belkin Mobile Power Cord for iPod w/ Dock 1 19.95 2.3162508
#> 3 MA147LL/A electronics,music Apple 60 GB iPod with Video Playback Black 10 399.00 0.9044058
write.csv(df, "myfile.csv", row.names = FALSE)
readLines("myfile.csv")
#> [1] "\"id\",\"cat\",\"name\",\"popularity\",\"price\",\"score\""
#> [2] "\"IW-02\",\"electronics,connector\",\"iPod & iPod Mini USB 2.0 Cable\",1,11.5,3.2388113"
#> [3] "\"F8V7067-APL-KIT\",\"electronics,connector\",\"Belkin Mobile Power Cord for iPod w/ Dock\",1,19.95,2.3162508"
#> [4] "\"MA147LL/A\",\"electronics,music\",\"Apple 60 GB iPod with Video Playback Black\",10,399,0.9044058"
答案 2 :(得分:0)
为了像我这样的新手的利益,我正在分享解决方案。
PowerShell方法:
第1步:我使用以下保存为.ps1文件的PowerShell代码。
curl 'https://some_site.com/solr/some_folder/select?q=*:*&wt=csv' -L -u username:password --location-trusted -b cookie-jar.txt > OutputFilename.csv
第2步:我使用以下R脚本运行PowerShell文件,并将PowerShell生成的csv文件作为数据框读取,以便进一步处理。
system2("PowerShell", args=c("-file", "C:\\FolderName\\FileName.ps1"))
df <- as.data.frame(read.csv("OutputFilename.csv", header = TRUE, sep = ",", fill = TRUE, fileEncoding="UTF-16LE"))
使用Scott Chamberlain方法的solrium
方法:
library(solrium)
solr_connect("https://site.organization.com", errors = "complete")
solr_search(name = "studies", q = "metadata:value", rows=1000, wt = "csv", callopts = httr::authenticate("usrname", "pwd"))