天问

RHadoop数据分析(2)-数据导入导出

总结一下,各种数据导入R进行分析,R导出各种格式。

 

(1)RData

 

保存CSV:

a=c(1,2,3)
b=c(1,2,3)

write.csv(a,"test.csv",seq=",",row.names(=FALSE))

导入CSV:

aa=read.csv("test.csv",seq=",")

 

保存TEXT:

write.table(a,"test.ctxt",seq="\t")

导入TEXT

aa2=read.table("test.ctxt",seq="\t")

 

保存RData镜像:

save.image()

导入RData镜像:

load("test.RDATA")

保存rda:
save(a,b,file="test.rda")

load("test.rda")

 

(2)MySql
#install.packages("RMySQL")
library(RMySQL)
mysqldb=dbConnect(MySQL(),user="root",password="",dbname="testdb",host="localhost")
dbListTables(mysqldb)

rs1=dbSendQuery(mysqldb,"select * from fridge")
#n=-1表示所有行
dd=fetch(rs1,n=10)

表增删改就不需要了,直接mysql操作就可以了。。。

 

(3)Excel

es <- read.xlsx("D:/ga.xlsx",1)
r <- res[1:5,]
ress <- write.xlsx(r, "D:/ga1.xls")

(4)MongoDB
#install.packages (rmongodb)
library (rmongodb)
mongo <-mongo.create ()
mongo.is.connected (mongo)

# Create a BSON object cache
buf <- mongo.bson.buffer.create ()

# Add element to the object buf
mongo.bson.buffer.append (buf, "name", "Echo")
b <- mongo.bson.from.list(list(name="Fred", age=29, city="Boston"))
iter <- mongo.bson.iterator.create(b) # b is of class "mongo.bson"
while (mongo.bson.iterator.next(iter))
print(mongo.bson.iterator.value(iter))

if (mongo.is.connected(mongo)) {
ns <- "test.people"

# add bson objects to mongo
buf <- mongo.bson.buffer.create()
mongo.bson.buffer.append(buf, "name", "Joe")
criteria <- mongo.bson.from.buffer(buf)

buf <- mongo.bson.buffer.create()
mongo.bson.buffer.start.object(buf, "$inc")
mongo.bson.buffer.append(buf, "age", 1L)
mongo.bson.buffer.finish.object(buf)
objNew <- mongo.bson.from.buffer(buf)

# increment the age field of the first record matching name "Joe"
mongo.update(mongo, ns, criteria, objNew)

buf <- mongo.bson.buffer.create()
mongo.bson.buffer.append(buf, "name", "Jeff")
criteria <- mongo.bson.from.buffer(buf)

buf <- mongo.bson.buffer.create()
mongo.bson.buffer.append(buf, "name", "Jeff")
mongo.bson.buffer.append(buf, "age", 27L)
objNew <- mongo.bson.from.buffer(buf)

# update the entire record to { name: "Jeff", age: 27 }
# where name equals "Jeff"
# if such a record exists; otherwise, insert this as a new reord
mongo.update(mongo, ns, criteria, objNew,
mongo.update.upsert)

# do a shorthand update:
mongo.update(mongo, ns, list(name="John"), list(name="John", age=25))
}

(5)SQLite
#Install.packages("RSQLite")
library("RSQLite")
con <- dbConnect(SQLite(), dbname="data/first.db")
tables <- dbListTables(con)

# exclude sqlite_sequence (contains table information)
tables <- tables[tables != "sqlite_sequence"]
lDataFrames <- vector("list", length=length(tables))

# create a data.frame for each table
for (i in seq(along=tables)) {
lDataFrames[[i]] <- dbGetQuery(conn=con, statement=paste("SELECT * FROM '", tables[[i]], "'", sep=""))
}

# For firing Delete query in SQLite db
dbBeginTransaction(con)
rs <- dbSendQuery(con, "DELETE from candidates WHERE age > 50")

# For writing tavle from R dataframe
data(USArrests)
dbWriteTable(con, "USArrests", USArrests)

(6)PostgreSQL

 

(7)Hive

 

(8)HBase

 

博客地址:http://blog.yoqi.me/?p=3474
扫我捐助哦
喜欢 3002

这篇文章有1条评论

  1. wp (作者) 2017/7/5 #1 [REPLY]

    Excel操作有很多lib包。

发表评论