将数据框从R studio复制到SAP HANA

时间:2018-04-03 13:12:57

标签: r odbc sap hana

我已经在列表中提取了twitter数据,并将其转换为data.frame,并安装了必要的基本软件包。

我使用以下代码从Twitter中提取数据:

library(twitteR)
setup_twitter_oauth("consumer-key", "consumer-secret",
                    "access-token", "access-secret")

search.string <- "#Karnataka"
no.of.tweets <- 10

tweets <- searchTwitter(search.string, n=no.of.tweets, lang="en")

tweet_df <- twListToDF(tweets)

我想将data.frame tweet_df加载到我的架构中的HANA数据库中的表。有人可以帮我这个吗?

2 个答案:

答案 0 :(得分:0)

这是实现此目的的一种方法

CAST

写入csv文件并导入HANA

CEIL

答案 1 :(得分:0)

通常,我建议将数据保存在SAP HANA中并从那里对其进行操作,但您当然也可以&#34;保存&#34; R数据帧。

这是一个简单的例子:

# load the ODBC driver
library("RODBC")
# open a ODBC "channel" - since I use the SAP HANA secure storage all I have to 
# specify here is the name of the DSN entry. 
ch<-odbcConnect("S12"

# this is just the string for the name of the table the data will be inserted into
table.for.save <- 'AIRQUALITY'
# get the data frame for the AIRQUALITY sample data 
aqdata <- airquality
# that's a data frame
str(aqdata)

# now "save" the data frame to SAP HANA via sqlSave
sqlSave(ch,dat = aqdata, tablename = table.for.save, verbose = TRUE, rownames =  FALSE)


> sqlSave(ch,dat = aqdata, tablename = table.for.save, verbose = TRUE, rownames =  FALSE)

Query: CREATE TABLE "AIRQUALITY"  ("Ozone" INTEGER, "SolarR" INTEGER, "Wind" DOUBLE, "Temp" INTEGER, "Month" INTEGER, "Day" INTEGER)
Query: INSERT INTO "AIRQUALITY" ( "Ozone", "SolarR", "Wind", "Temp", "Month", "Day" ) VALUES ( ?,?,?,?,?,? )
Binding: 'Ozone' DataType 4, ColSize 10
Binding: 'SolarR' DataType 4, ColSize 10
Binding: 'Wind' DataType 8, ColSize 15
Binding: 'Temp' DataType 4, ColSize 10
Binding: 'Month' DataType 4, ColSize 10
Binding: 'Day' DataType 4, ColSize 10
Parameters:
no: 1: Ozone 41/***/no: 2: SolarR 190/***/no: 3: Wind 7.4/***/no: 4: Temp 67/***/no: 5: Month 5/***/no: 6: Day 1/***/
no: 1: Ozone 36/***/no: 2: SolarR 118/***/no: 3: Wind 8/***/no: 4: Temp 72/***/no: 5: Month 5/***/no: 6: Day 2/***/
no: 1: Ozone 12/***/no: 2: SolarR 149/***/no: 3: Wind 12.6/***/no: 4: Temp 74/***/no: 5: Month 5/***/no: 6: Day 3/***/
...

在SAP HANA中,您现在可以从表中SELECT

select top 10 * from "DEVDUDE"."AIRQUALITY";

Ozone   SolarR  Wind    Temp    Month   Day
41      190     7.4     67      5       1  
36      118     8.0     72      5       2  
12      149     12.6    74      5       3  
18      313     11.5    62      5       4  
?       ?       14.3    56      5       5  
28      ?       14.9    66      5       6  
23      299     8.6     65      5       7  
19      99      13.8    59      5       8  
8       19      20.1    61      5       9  
?       194     8.6     69      5       10 

由于这是标准的RODBC,并非特定于SAP HANA,因此我建议您阅读这些技术。