Package ziggy :: Package GraphReduce :: Module GraphLoader
[hide private]
[frames] | no frames]

Source Code for Module ziggy.GraphReduce.GraphLoader

 1  ''' 
 2  Created on Aug 11, 2010 
 3   
 4  @author: dwmclary 
 5  ''' 
 6  import networkx as nx 
 7  from .. hdmc import hdfs 
8 -class GraphLoader():
9 ''' 10 Provides graph handles in Hadoop MapReduce 11 ''' 12 13
14 - def __init__(self, G = None, graph_handle=None):
15 ''' 16 Constructor 17 ''' 18 19 if G != None: 20 self.G = G 21 #sets a string pointing to an flat-file adjacency list 22 if graph_handle != None: 23 self.graph_handle = graph_handle
24
25 - def read_edgelist(self, filename):
26 self.G = nx.read_edgelist(filename)
27
28 - def read_adjlist(self, filename, undirected=True):
29 if undirected: 30 self.G = nx.read_adjlist(filename).to_undirected() 31 else: 32 self.G = nx.read_adjlist(filename) 33 self.graph_handle = filename
34
35 - def write_adjlist(self, filename=None):
36 if filename: 37 nx.write_adjlist(self.G.to_directed(), filename) 38 self.graph_handle = filename 39 hdfs.mkdir(filename) 40 hdfs.copyToHDFS(filename, filename+"/page_rank") 41 #hdfs.copyToHDFS(filename, filename+"/shortest_path") 42 hdfs.copyToHDFS(filename, filename+"/degree") 43 else: 44 if self.graph_handle: 45 nx.write_adjlist(self.G.to_directed(), self.graph_handle)
46
47 - def write_edgelist(self, filename):
48 nx.write_edgelist(self.G,filename)
49
50 - def in_hdfs(self):
51 listing = hdfs.ls(self.graph_handle)["stdout"].split("\n") 52 found = False 53 disk_tail = self.graph_handle.split("/")[-1] 54 for line in listing: 55 hdfs_tail = line.split("/")[-1] 56 if disk_tail == hdfs_tail: 57 found = True 58 break 59 return found
60