In [1]:
sqlContext.sql("CREATE TEMPORARY TABLE students_table USING com.stratio.datasource.mongodb OPTIONS (host 'mongo:27017', database 'highschool', collection 'students')")


Out[1]:
DataFrame[]

In [4]:
from pymongo import MongoClient
client = MongoClient('mongo', 27017)
db = client.highschool

In [14]:
db.create_collection('students')


Out[14]:
Collection(Database(MongoClient(host=['mongo:27017'], document_class=dict, tz_aware=False, connect=True), u'highschool'), u'students')

In [16]:
from csv import DictReader
rows = []
with open('student-por.csv') as stu:
    dr = DictReader(stu,delimiter=';')
    for row in dr:
        rows.append(row)

In [17]:
rows[0]


Out[17]:
{'Dalc': '1',
 'Fedu': '4',
 'Fjob': 'teacher',
 'G1': '0',
 'G2': '11',
 'G3': '11',
 'Medu': '4',
 'Mjob': 'at_home',
 'Pstatus': 'A',
 'Walc': '1',
 'absences': '4',
 'activities': 'no',
 'address': 'U',
 'age': '18',
 'failures': '0',
 'famrel': '4',
 'famsize': 'GT3',
 'famsup': 'no',
 'freetime': '3',
 'goout': '4',
 'guardian': 'mother',
 'health': '3',
 'higher': 'yes',
 'internet': 'no',
 'nursery': 'yes',
 'paid': 'no',
 'reason': 'course',
 'romantic': 'no',
 'school': 'GP',
 'schoolsup': 'yes',
 'sex': 'F',
 'studytime': '2',
 'traveltime': '2'}

In [18]:
coll = db.students

In [19]:
coll.insert_many(rows)


Out[19]:
<pymongo.results.InsertManyResult at 0x7f84340fce10>

In [20]:
coll.find_one({'sex':'F'})


Out[20]:
{u'Dalc': u'1',
 u'Fedu': u'4',
 u'Fjob': u'teacher',
 u'G1': u'0',
 u'G2': u'11',
 u'G3': u'11',
 u'Medu': u'4',
 u'Mjob': u'at_home',
 u'Pstatus': u'A',
 u'Walc': u'1',
 u'_id': ObjectId('574e5fd1f8374e03e66d4f43'),
 u'absences': u'4',
 u'activities': u'no',
 u'address': u'U',
 u'age': u'18',
 u'failures': u'0',
 u'famrel': u'4',
 u'famsize': u'GT3',
 u'famsup': u'no',
 u'freetime': u'3',
 u'goout': u'4',
 u'guardian': u'mother',
 u'health': u'3',
 u'higher': u'yes',
 u'internet': u'no',
 u'nursery': u'yes',
 u'paid': u'no',
 u'reason': u'course',
 u'romantic': u'no',
 u'school': u'GP',
 u'schoolsup': u'yes',
 u'sex': u'F',
 u'studytime': u'2',
 u'traveltime': u'2'}

In [ ]: