· 7 years ago · Oct 10, 2018, 11:46 AM
1from pymongo import *
2import sys
3#---------------------------Connection Settings-----------------------#
4# Connecting to MongoDB i.e. Connection String
5# client = MongoClient('52.41.110.155',27020)
6# localhost is the location where mongodb is running.
7# 27017 is the default port number for mongodb
8#---------------------------Connection Settings-----------------------#
9
10
11#---------------------------DataBase Settings-----------------------#
12# MongoStore is the name of Database. Since it is nosql store so if the database
13# exist then it will read else it will create
14# collectionName is the name of document (table in sql) of MongoDB. If it exists then
15# it will read else it will create
16#---------------------------DataBase Settings-----------------------#
17
18# Reading Data from File for feeding the Table (Dimension Table)
19
20# MongoDB Work
21MongoStore1 = 'Cars_11459'
22MongoStore2 = 'Text_11459'
23MongoStore3 = 'Array_11459'
24MongoIP = 'localhost'
25MonngoPort = 27017
26
27Cars = ['Cars1', 'Cars2', 'Cars3', 'Cars4']
28Text = ['Text1', 'Text2', 'Text3', 'Text4']
29
30
31def WriteValue(mongoStore, collectionName, KN, Value):
32 try:
33
34 client = MongoClient(MongoIP, MonngoPort)
35 db = client[str(mongoStore)]
36 collection = db[collectionName]
37 post_data = {
38 'Key': str(KN),
39 'Data': str(Value)
40 }
41 collection.insert_one(post_data)
42 except IOError as e:
43 print("I/O error({0}): {1}".format(e.errno, e.strerror))
44 except ValueError:
45 print("Could not convert data to an integer.")
46 except:
47 print("Unexpected error:", sys.exc_info()[0])
48
49
50def ReadValue(mongoStore, collectionName, KN):
51 try:
52 print(mongoStore, collectionName, KN, MongoIP, MonngoPort)
53 client = MongoClient(MongoIP, MonngoPort)
54 db = client[str(mongoStore)]
55 collection = db[collectionName]
56 value = collection.find_one({'_id': str(KN)})
57 return value
58 except IOError as e:
59 print("I/O error({0}): {1}".format(e.errno, e.strerror))
60 except ValueError:
61 print("Could not convert data to an integer.")
62 except:
63 print("Unexpected error:", sys.exc_info()[0])
64
65
66def ReadAll(mongoStore, collectionName):
67 try:
68 client = MongoClient(MongoIP, MonngoPort)
69 db = client[str(mongoStore)]
70 collection = db[collectionName]
71 value = collection.find({})
72 return value
73 except IOError as e:
74 print("I/O error({0}): {1}".format(e.errno, e.strerror))
75 except ValueError:
76 print("Could not convert data to an integer.")
77 except:
78 print("Unexpected error:", sys.exc_info()[0])
79
80
81# We have 4x files of having miles per galon of consumption of cars (various manufacturers)
82
83# simple way of reading a file
84for m in range(1, 4):
85 file = open('cars' + str(m) + '.csv', 'r')
86 print('>>', file)
87 data4table1 = file.readlines()
88 print(m)
89 print('--------------Complete Data of file----------------------')
90 print(data4table1)
91 print('--------------Complete Data of file----------------------')
92
93 splitval = (data4table1[0])
94 print(splitval)
95 splitval = splitval.split(',')
96 for n in range(0, len(splitval)):
97 print(splitval[n])
98
99 for n in range(1, len(data4table1)-1):
100 row = data4table1[n]
101 row = row.split(',')
102 dic4tabl1 = {
103 'mpg': row[0],
104 'cylinders': row[1],
105 'displacement': row[2],
106 'horsepower': row[3],
107 'weight': row[4],
108 'acceleration': row[5],
109 'year': row[6],
110 'origin': row[7],
111 'name': row[8]
112 }
113
114 WriteValue(MongoStore1, Cars[m], str(n)+':11459', dic4tabl1)
115 print(Cars[m], str(n)+':11459', dic4tabl1)
116 if str(dic4tabl1).__contains__('amc rebel sst'):
117 print('Got Data')
118 print('Completed')
119
120
121# text file
122
123file = open('lorem_ipsum.txt', 'r')
124
125data_text = file.readlines()
126print(data_text)
127
128for n in range(0, len(data_text)):
129 row = data_text[n]
130 dic4tabl1 = {
131 'text': row
132 }
133 WriteValue(MongoStore2, Text[0], str(n)+':11459', dic4tabl1)
134 print(Text[0], str(n)+':11459', dic4tabl1)
135
136# Arrays
137Array1 = [12, 12, 12, 12, 12, 12, 12]
138Array2 = [12, 12, 12, 12, 12, 12, 12]
139
140List1 = [Array1, Array2]
141
142counter = 1
143
144for n in range(0, len(List1)):
145 WriteValue(MongoStore3, "Array", str(counter)+':11459', List1[n])
146 print(Text[0], str(n)+':11459', dic4tabl1)
147 counter = counter + 1
148
149
150# Creating Star Schema on Read without Query
151# def readwithoutquery():
152# Var_Table=[]
153# for n in range (0,3):
154# collection = CollectionName[n]
155# print ('Reading from Collection : ', collection)
156# Var_Table[n]=ReadAll(collection)
157#
158# print(Var_Table)
159#
160#
161# readwithoutquery()
162#
163#
164# Creating Star Schema on Read with Query
165# def readwithquery():
166# Var_Table = []
167# for n in range (1,4):
168# collection=CollectionName[n]
169# print ('Reading from Collection : ', collection)
170#
171# # Var_Table[n]=ReadValue(collection, str(2)+':11459')
172# Var_Table[n] = ReadValue(collection, '5bb48a938969ef1f8e65b36a')
173# WriteValue('Fact_Table', 'K1', Var_Table)
174# return Var_Table
175#
176#
177#
178# tabletemp = readwithquery()
179# print (tabletemp)