本文整理汇总了Python中kafka.consumer.SimpleConsumer.get_messages方法的典型用法代码示例。如果您正苦于以下问题:Python SimpleConsumer.get_messages方法的具体用法?Python SimpleConsumer.get_messages怎么用?Python SimpleConsumer.get_messages使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类kafka.consumer.SimpleConsumer
的用法示例。
在下文中一共展示了SimpleConsumer.get_messages方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Consumer
# 需要导入模块: from kafka.consumer import SimpleConsumer [as 别名]
# 或者: from kafka.consumer.SimpleConsumer import get_messages [as 别名]
class Consumer(object):
def __init__(self, addr):
self.client = KafkaClient(addr)
self.topic = "steps_data_part4"
self.consumer_group = 's3_consumer'
self.consumer = SimpleConsumer(self.client, self.consumer_group, self.topic)
def consume_message(self):
while True:
timestamp = time.strftime('%Y%m%d%H%M%S')
temp_file_name = "%s_%s_%s.dat" %(self.topic, self.consumer_group, timestamp)
temp_file = open("/home/ubuntu/rankMyStep/kafka/"+temp_file_name,"w")
messages = self.consumer.get_messages(count=1000, block=False)
for msg in messages:
print msg.message.value + "\n"
temp_file.write(msg.message.value + "\n")
self.save_to_s3(temp_file_name)
def save_to_s3(self, file_name):
mybucket = "anurag-raw-data-store"
aws_access_key = os.getenv('AWS_ACCESS_KEY_ID', 'default')
aws_secret_access_key = os.getenv('AWS_SECRET_ACCESS_KEY', 'default')
s3_client = boto3.client('s3')
s3_client.upload_file("/home/ubuntu/rankMyStep/kafka/"+file_name,
mybucket,"rankmysteps/"+file_name)
os.remove("/home/ubuntu/rankMyStep/kafka/"+file_name)
示例2: Consumer
# 需要导入模块: from kafka.consumer import SimpleConsumer [as 别名]
# 或者: from kafka.consumer.SimpleConsumer import get_messages [as 别名]
class Consumer(object):
def __init__(self, addr, group, topic):
"""Initialize Consumer with kafka broker IP, group, and topic."""
self.client = KafkaClient(addr)
self.consumer = SimpleConsumer(self.client, group, topic,
max_buffer_size=1310720000)
self.temp_file_path = None
self.temp_file = None
self.hadoop_path = "/insight/artsy/geo"
self.topic = topic
self.group = group
self.block_cnt = 0
def consume_topic(self, output_dir):
"""Consumes a stream of messages from the "post_geo_activity" topic.
Code template from https://github.com/ajmssc/bitcoin-inspector.git
"""
timestamp = time.strftime('%Y%m%d%H%M%S')
# open file for writing
self.temp_file_path = "%s/kafka_%s_%s_%s.dat" % (output_dir,self.topic,self.group,timestamp)
self.temp_file = open(self.temp_file_path,"w")
while True:
try:
# get 1000 messages at a time, non blocking
messages = self.consumer.get_messages(count=1000, block=False)
for message in messages:
self.temp_file.write(message.message.value + "\n")
# file size > 20MB
if self.temp_file.tell() > 20000000:
self.flush_to_hdfs(output_dir)
self.consumer.commit()
except:
# move to tail of kafka topic if consumer is referencing
# unknown offset
self.consumer.seek(0, 2)
def flush_to_hdfs(self, output_dir):
"""Flushes the 20MB file into HDFS."""
self.temp_file.close()
timestamp = time.strftime('%Y%m%d%H%M%S')
hadoop_fullpath = "%s/%s_%s_%s.dat" % (self.hadoop_path, self.group,self.topic, timestamp)
print "Block {}: Flushing data file to HDFS => {}".format(str(self.block_cnt),hadoop_fullpath)
self.block_cnt += 1
os.system("hdfs dfs -put %s %s" % (self.temp_file_path, hadoop_fullpath)) # save from local to hdfs
os.remove(self.temp_file_path) # remove temp local file
timestamp = time.strftime('%Y%m%d%H%M%S')
self.temp_file_path = "%s/kafka_%s_%s_%s.dat" % (output_dir,self.topic,self.group,timestamp)
self.temp_file = open(self.temp_file_path, "w")
示例3: Consumer
# 需要导入模块: from kafka.consumer import SimpleConsumer [as 别名]
# 或者: from kafka.consumer.SimpleConsumer import get_messages [as 别名]
class Consumer(object):
def __init__(self, addr, group, topic):
self.client = KafkaClient(addr)
self.consumer = SimpleConsumer(self.client, group, topic, max_buffer_size=1310720000, auto_offset_reset='smallest')
self.temp_file_path = None
self.temp_file = None
self.topic = topic
self.group = group
self.block_cnt = 0
def consume_topic(self):
timestamp = time.strftime('%Y%m%d%H%M%S')
#open file for writing
self.temp_file_path = "/home/ubuntu/datamill/kafka_%s_%s_%s.dat" % (self.topic, self.group, timestamp)
self.temp_file = open(self.temp_file_path,"w")
header = 'experiment_id,job_id,results_file,package_id,package_name,worker_id,config_id,replicate_no,setup_time,run_time,collect_time,hw_cpu_arch,hw_cpu_mhz,hw_gpu_mhz,hw_num_cpus,hw_page_sz,hw_ram_mhz,hw_ram_sz,sw_address_randomization,sw_autogroup,sw_compiler,sw_drop_caches,sw_env_padding,sw_filesystem,sw_freq_scaling,sw_link_order,sw_opt_flag,sw_swap,sw_sys_time'
self.temp_file.write(header)
while True:
try:
messages = self.consumer.get_messages(count=100, block=False)
for message in messages:
self.temp_file.write(message.message.value + "\n")
if self.temp_file.tell() > 20000:
self.save_to_hdfs()
self.consumer.commit()
except:
self.consumer.seek(0, 2)
self.consumer.commit()
def save_to_hdfs(self):
self.temp_file.close()
timestamp = time.strftime('%Y%m%d%H%M%S')
hadoop_path = "/datamill/%s_%s_%s.csv" % (self.group, self.topic, timestamp)
print "Block " + str(self.block_cnt) + ": Saving file to HDFS " + hadoop_path
self.block_cnt += 1
# place blocked messages into history and cached folders on hdfs
os.system("hdfs dfs -put %s %s" % (self.temp_file_path, hadoop_path))
os.remove(self.temp_file_path)
timestamp = time.strftime('%Y%m%d%H%M%S')
self.temp_file_path = "/home/ubuntu/datamill/kafka_%s_%s_%s.dat" % (self.topic, self.group, timestamp)
self.temp_file = open(self.temp_file_path, "w")
示例4: KafkaDatawakeLookaheadSpout
# 需要导入模块: from kafka.consumer import SimpleConsumer [as 别名]
# 或者: from kafka.consumer.SimpleConsumer import get_messages [as 别名]
class KafkaDatawakeLookaheadSpout(Spout):
group = 'datawake-crawler-out-consumer'.encode()
def __init__(self):
Spout.__init__(self)
self.queue = None
def initialize(self, stormconf, context):
try:
settings = all_settings.get_settings(stormconf['topology.deployment'])
self.topic = settings['crawler-out-topic'].encode()
self.conn_pool = settings['conn_pool'].encode()
self.log('KafkaDatawakeLookaheadSpout initialized with topic =' + self.topic + ' conn_pool=' + self.conn_pool)
self.kafka = KafkaClient(self.conn_pool)
self.consumer = SimpleConsumer(self.kafka, self.group, self.topic, max_buffer_size=None)
self.consumer.seek(0, 2) # move to the tail of the queue
except:
self.log("KafkaDatawakeLookaheadSpout initialize error", level='error')
self.log(traceback.format_exc(), level='error')
raise
def next_tuple(self):
"""
input message:
dict(
id = input['id'],
appid = input['appid'],
url = url,
status_code = response.getcode(),
status_msg = 'Success',
timestamp = response.info()['date'],
links_found = links,
raw_html = html,
attrs = input['attrs']
)
:return: (url, status, headers, flags, body, timestamp, source,context)
"""
offsetAndMessage = self.consumer.get_messages(timeout=None)[0]
message = offsetAndMessage.message.value
crawled = json.loads(message)
safeurl = crawled['url'].encode('utf-8', 'ignore')
self.log("Lookahead spout received id: " + crawled['id'] + " url: " + safeurl)
context = {
'source': 'datawake-lookahead',
'userId': crawled['attrs']['userId'],
'org': crawled['attrs']['org'],
'domain': crawled['attrs']['domain'],
'url': crawled['url']
}
self.emit([crawled['url'], crawled['status_code'], '', '', crawled['raw_html'], crawled['timestamp'], context['source'], context])
示例5: spiderIdle
# 需要导入模块: from kafka.consumer import SimpleConsumer [as 别名]
# 或者: from kafka.consumer.SimpleConsumer import get_messages [as 别名]
def spiderIdle(self, spider):
consumer = SimpleConsumer(self.kafka_conn, "test", "commands")
for msg in consumer.get_messages():
print msg.message.value
if msg.message.value == spider.name + "_stop":
print "stop"
spider.spider_pause()
# spider.close(spider,'ok')
# self.scrapy.engine.close_spider(spider, 'closespider_itemcount')
if msg.message.value == spider.name + "_start":
# self.scrapy.engine.scraper.open_spider(spider)
spider.spider_resume()
示例6: __init__
# 需要导入模块: from kafka.consumer import SimpleConsumer [as 别名]
# 或者: from kafka.consumer.SimpleConsumer import get_messages [as 别名]
class KafkaConsumer:
group = "python-lookahead-consumer"
def __init__(self,conn_pool,topic,group):
self.conn_pool = conn_pool
self.topic = topic
self.group = group
self.kafka = KafkaClient(self.conn_pool)
self.consumer = SimpleConsumer(self.kafka,self.group,self.topic,max_buffer_size=None)
self.consumer.seek(0,2) # move to the tail of the queue
def next(self):
offsetAndMessage = self.consumer.get_messages(timeout=None)[0]
message = offsetAndMessage.message.value
return message
示例7: KafkaDatawakeVisitedSpout
# 需要导入模块: from kafka.consumer import SimpleConsumer [as 别名]
# 或者: from kafka.consumer.SimpleConsumer import get_messages [as 别名]
class KafkaDatawakeVisitedSpout(Spout):
group = 'datawake-visited-consumer'.encode()
def __init__(self):
Spout.__init__(self)
self.queue = None
def initialize(self, stormconf, context):
try:
settings = all_settings.get_settings(stormconf['topology.deployment'])
self.topic = settings['visited-topic'].encode()
self.conn_pool = settings['conn_pool'].encode()
self.log('KafkaDatawakeVisitedSpout initialized with topic =' + self.topic + ' conn_pool=' + self.conn_pool)
self.kafka = KafkaClient(self.conn_pool)
self.consumer = SimpleConsumer(self.kafka, self.group, self.topic, max_buffer_size=None)
self.consumer.seek(0, 2) # move to the tail of the queue
except:
self.log("KafkaDatawakeVisitedSpout initialize error", level='error')
self.log(traceback.format_exc(), level='error')
raise
def next_tuple(self):
"""
input: (timestamp,org,domain,user_id,url,html)
:return: (url, status, headers, flags, body, timestamp, source,context)
"""
offsetAndMessage = self.consumer.get_messages(timeout=None)[0]
message = offsetAndMessage.message.value
message = message.decode('utf-8')
message = message.split('\0')
(timestamp, org, domain, userId, url, html) = message
context = {
'source': 'datawake-visited',
'userId': userId,
'org': org,
'domain': domain,
'url': url
}
self.emit([url, '', '', '', html, timestamp, context['source'], context])
示例8: PerfConsumerSync
# 需要导入模块: from kafka.consumer import SimpleConsumer [as 别名]
# 或者: from kafka.consumer.SimpleConsumer import get_messages [as 别名]
class PerfConsumerSync ( threading.Thread ):
running = True
def __init__(self, factory, destination):
self.factory = factory
self.destination = destination
self.consumer = SimpleConsumer(self.factory, "test-group", self.destination)
self.rate = PerfRate()
threading.Thread.__init__ ( self )
def run (self):
while (self.running):
textMessage = self.consumer.get_messages(block=True, timeout=1000000)
if (textMessage != None):
self.rate.increment()
def stop(self):
self.running = False
def start(self):
threading.Thread.start(self)
示例9: __init__
# 需要导入模块: from kafka.consumer import SimpleConsumer [as 别名]
# 或者: from kafka.consumer.SimpleConsumer import get_messages [as 别名]
class KafkaMonitor:
def __init__(self, settings):
# dynamic import of settings file
# remove the .py from the filename
self.settings = importlib.import_module(settings[:-3])
# only need kafka for both uses
self.kafka_conn = KafkaClient(self.settings.KAFKA_HOSTS)
def get_method(self, key):
if key == 'handle_crawl_request':
return self.handle_crawl_request
elif key == 'handle_action_request':
return self.handle_action_request
raise AttributeError(key)
def setup(self):
self.redis_conn = redis.Redis(host=self.settings.REDIS_HOST,
port=self.settings.REDIS_PORT)
self.kafka_conn.ensure_topic_exists(self.settings.KAFKA_INCOMING_TOPIC)
self.consumer = SimpleConsumer(self.kafka_conn,
self.settings.KAFKA_GROUP,
self.settings.KAFKA_INCOMING_TOPIC,
auto_commit=True,
iter_timeout=1.0)
self.result_method = self.get_method(self.settings.SCHEMA_METHOD)
self.validator = self.extend_with_default(Draft4Validator)
def extend_with_default(self, validator_class):
'''
Method to add default fields to our schema validation
( From the docs )
'''
validate_properties = validator_class.VALIDATORS["properties"]
def set_defaults(validator, properties, instance, schema):
for error in validate_properties(
validator, properties, instance, schema,
):
yield error
for property, subschema in properties.iteritems():
if "default" in subschema:
instance.setdefault(property, subschema["default"])
return validators.extend(
validator_class, {"properties": set_defaults},
)
def handle_crawl_request(self, dict):
'''
Processes a vaild crawl request
@param dict: a valid dictionary object
'''
# format key
key = "{sid}:queue".format(sid=dict['spiderid'])
val = pickle.dumps(dict, protocol=-1)
# shortcut to shove stuff into the priority queue
self.redis_conn.zadd(key, val, -dict['priority'])
# if timeout crawl, add value to redis
if 'expires' in dict:
key = "timeout:{sid}:{appid}:{crawlid}".format(
sid=dict['spiderid'],
appid=dict['appid'],
crawlid=dict['crawlid'])
self.redis_conn.set(key, dict['expires'])
def handle_action_request(self, dict):
'''
Processes a vaild action request
@param dict: The valid dictionary object
'''
# format key
key = "{action}:{spiderid}:{appid}".format(
action=dict['action'],
spiderid=dict['spiderid'],
appid=dict['appid'])
if "crawlid" in dict:
key = key + ":" + dict['crawlid']
self.redis_conn.set(key, dict['uuid'])
def _main_loop(self):
'''
Continuous loop that reads from a kafka topic and tries to validate
incoming messages
'''
while True:
start = time.time()
try:
for message in self.consumer.get_messages():
#.........这里部分代码省略.........
示例10: ZKConsumer
# 需要导入模块: from kafka.consumer import SimpleConsumer [as 别名]
# 或者: from kafka.consumer.SimpleConsumer import get_messages [as 别名]
#.........这里部分代码省略.........
return
self.consumer = SimpleConsumer(self.client, self.group, self.topic,
partitions=my_partitions,
**self.consumer_kwargs)
self.consumer.provide_partition_info()
self.logger.info("Consumer connected to Kafka: %s", self.consumer.offsets)
def stop(self):
if self.consumer is not None:
self.logger.info('Stopping Kafka consumer')
self.consumer.stop()
self.consumer = None
if self.client is not None:
self.logger.info('Stopping Kafka client')
self.client.close()
self.client = None
if self.zk is not None:
self.logger.info('Stopping ZooKeeper client')
if self.zkp is not None and not self.zkp.failed:
self.zkp.finish()
self.zk.stop()
self.zkp = None
self.zk = None
def commit(self, partitions=None):
"""
Commit offsets for this consumer
partitions: list of partitions to commit, default is to commit
all of them
"""
if self.consumer is None:
return
self.logger.debug('Begin committing offsets for partitions: %s',
partitions if partitions else 'All')
self.consumer.commit(partitions)
self.logger.debug('End committing offsets for partitions: %s',
partitions if partitions else 'All')
def pending(self, partitions=None):
"""
Gets the pending message count
partitions: list of partitions to check for, default is to check all
"""
return self.consumer.pending(partitions)
def provide_partition_info(self):
"""
Indicates that partition info must be returned by the consumer
"""
self.consumer.provide_partition_info()
def seek(self, offset, whence):
"""
Alter the current offset in the consumer, similar to fseek
offset: how much to modify the offset
whence: where to modify it from
0 is relative to the earliest available offset (head)
1 is relative to the current offset
2 is relative to the latest known offset (tail)
"""
self.consumer.seek(offset, whence)
def get_messages(self, count=1, block=True, timeout=0.1):
"""
Fetch the specified number of messages
count: Indicates the maximum number of messages to be fetched
block: If True, the API will block till some messages are fetched.
timeout: If block is True, the function will block for the specified
time (in seconds) until count messages is fetched. If None,
it will block forever.
"""
if self.consumer is None:
return []
else:
try:
messages = self.consumer.get_messages(count, block, timeout)
if not messages and self.zkp.failed:
raise FailedPayloadsError
return messages
except FailedPayloadsError as err:
msg = 'Failed to retrieve payload, restarting consumer'
self.logger.exception(msg)
raise err
def get_message(self, block=True, timeout=0.1, get_partition_info=None):
return self.consumer.get_message(block, timeout, get_partition_info)
def _get_message(self, block=True, timeout=0.1, get_partition_info=None,
update_offset=True):
return self.consumer._get_message(block, timeout, get_partition_info,
update_offset)
def __iter__(self):
for msg in self.consumer:
yield msg
示例11: Consumer
# 需要导入模块: from kafka.consumer import SimpleConsumer [as 别名]
# 或者: from kafka.consumer.SimpleConsumer import get_messages [as 别名]
class Consumer(object):
"""Kafka consumer class with functions to consume messages to HDFS.
Messages are blocked into 20MB files and transferred to HDFS
Attributes:
client: string representing IP:port of the kafka broker
consumer: Consumer object specifying the client group, and topic
temp_file_path: location of the 20MB file to be appended to before
transfer to HDFS
temp_file: File object opened from temp_file_path
topic: String representing the topic on Kafka
group: String representing the Kafka consumer group to be associated
with
block_cnt: integer representing the block count for print statements
"""
def __init__(self, addr, group, topic):
"""Initialize Consumer with kafka broker IP, group, and topic."""
self.client = KafkaClient(addr)
self.consumer = SimpleConsumer(self.client, group, topic, max_buffer_size=1310720000)
self.temp_file_path = None
self.temp_file = None
self.hadoop_path = "/user/parking_data/history"
self.topic = topic
self.group = group
self.block_cnt = 0
def consume_topic(self, output_dir):
"""Consumes a stream of messages from the "messages" topic.
Code template from https://github.com/ajmssc/bitcoin-inspector.git
Args:
output_dir: string representing the directory to store the 20MB
before transferring to HDFS
Returns:
None
"""
timestamp = time.strftime("%Y%m%d%H%M%S")
# open file for writing
self.temp_file_path = "%s/kafka_%s_%s_%s.dat" % (output_dir, self.topic, self.group, timestamp)
self.temp_file = open(self.temp_file_path, "w")
# while True:
for ii in range(0, 2):
try:
# get 1000 messages at a time, non blocking
messages = self.consumer.get_messages(count=1000, block=False)
# OffsetAndMessage(offset=43, message=Message(magic=0,
# attributes=0, key=None, value='some message'))
for message in messages:
self.temp_file.write(message.message.value + "\n")
# file size > 20MB
if self.temp_file.tell() > 20000000:
self.flush_to_hdfs(output_dir)
self.consumer.commit()
except:
# move to tail of kafka topic if consumer is referencing
# unknown offset
self.consumer.seek(0, 2)
def flush_to_hdfs(self, output_dir):
"""Flushes the 20MB file into HDFS.
Code template from https://github.com/ajmssc/bitcoin-inspector.git
Flushes the file into HDFS folders
Args:
output_dir: string representing the directory to store the 20MB
before transferring to HDFS
Returns:
None
"""
self.temp_file.close()
timestamp = time.strftime("%Y%m%d%H%M%S")
hadoop_fullpath = "%s/%s_%s_%s.dat" % (self.hadoop_path, self.group, self.topic, timestamp)
print "Block {}: Flushing 20MB file to HDFS => {}".format(str(self.block_cnt), hadoop_fullpath)
self.block_cnt += 1
# place blocked messages into history and cached folders on hdfs
print ("hdfs dfs -put %s %s" % (self.temp_file_path, hadoop_fullpath))
os.system("sudo hdfs dfs -put %s %s" % (self.temp_file_path, hadoop_fullpath))
# os.system("sudo -u hdfs hdfs dfs -put %s %s" % (self.temp_file_path,
# cached_fullpath))
os.remove(self.temp_file_path)
timestamp = time.strftime("%Y%m%d%H%M%S")
self.temp_file_path = "%s/kafka_%s_%s_%s.dat" % (output_dir, self.topic, self.group, timestamp)
self.temp_file = open(self.temp_file_path, "w")
示例12: __init__
# 需要导入模块: from kafka.consumer import SimpleConsumer [as 别名]
# 或者: from kafka.consumer.SimpleConsumer import get_messages [as 别名]
#.........这里部分代码省略.........
for error in validate_properties(
validator, properties, instance, schema,
):
yield error
for property, subschema in properties.iteritems():
if "default" in subschema:
instance.setdefault(property, subschema["default"])
return validators.extend(
validator_class, {"properties": set_defaults},
)
def _main_loop(self):
'''
Continuous loop that reads from a kafka topic and tries to validate
incoming messages
'''
self.logger.debug("Processing messages")
old_time = 0
while True:
self._process_messages()
if self.settings['STATS_DUMP'] != 0:
new_time = int(time.time() / self.settings['STATS_DUMP'])
# only log every X seconds
if new_time != old_time:
self._dump_stats()
old_time = new_time
time.sleep(.01)
def _process_messages(self):
try:
for message in self.consumer.get_messages():
if message is None:
self.logger.debug("no message")
break
try:
self._increment_total_stat(message.message.value)
the_dict = json.loads(message.message.value)
found_plugin = False
for key in self.plugins_dict:
obj = self.plugins_dict[key]
instance = obj['instance']
schema = obj['schema']
try:
self.validator(schema).validate(the_dict)
found_plugin = True
self._increment_plugin_stat(
instance.__class__.__name__,
the_dict)
ret = instance.handle(the_dict)
# break if nothing is returned
if ret is None:
break
except ValidationError:
pass
if not found_plugin:
extras = {}
extras['parsed'] = True
extras['valid'] = False
extras['data'] = the_dict
self.logger.warn("Did not find schema to validate "
"request", extra=extras)
self._increment_fail_stat(the_dict)
示例13: Consumer
# 需要导入模块: from kafka.consumer import SimpleConsumer [as 别名]
# 或者: from kafka.consumer.SimpleConsumer import get_messages [as 别名]
class Consumer(object):
def __init__(self, addr, group, topic):
self.client = KafkaClient(addr)
self.consumer = SimpleConsumer(self.client, group, topic, max_buffer_size=1310720000)
self.temp_file_path = None
self.temp_file = None
self.hadoop_path = "/user/AdReport/%s/history" %(topic)
self.cached_path = "/user/AdReport/%s/cached" % (topic)
self.topic = topic
self.group = group
self.block_cnt = 0
def consume_topic(self, output_dir):
timestamp = time.strftime('%Y%m%d%H%M%S')
#open file for writing
self.temp_file_path = "%s/kafka_%s_%s_%s.dat" % (output_dir,
self.topic,
self.group,
timestamp)
self.temp_file = open(self.temp_file_path,"w")
print ( self.temp_file)
#one_entry = False
while True:
try:
messages = self.consumer.get_messages(count=10, block=False)
#OffsetAndMessage(offset=43, message=Message(magic=0,
# attributes=0, key=None, value='some message'))
for message in messages:
print (message)
#one_entry = True
#print (self.temp_file.tell())
self.temp_file.write(message.message.value + "\n")
if self.temp_file.tell() > 2000000:
self.save_to_hdfs(output_dir)
self.consumer.commit()
except:
self.consumer.seek(0, 2)
#if one_entry:
#print ("sending to hdfs")
#self.save_to_hdfs(output_dir, self.topic)
#self.consumer.commit()
def save_to_hdfs(self, output_dir):
print ("Saving file to hdfs")
self.temp_file.close()
print ("Closed open file")
timestamp = time.strftime('%Y%m%d%H%M%S')
hadoop_fullpath = "%s/%s_%s_%s.dat" % (self.hadoop_path, self.group,
self.topic, timestamp)
cached_fullpath = "%s/%s_%s_%s.dat" % (self.cached_path, self.group,
self.topic, timestamp)
#print ("Block " + str(self.block_cnt) + ": Saving file to HDFS " + hadoop_fullpath)
self.block_cnt += 1
# place blocked messages into history and cached folders on hdfs
os.system("sudo -u ubuntu /usr/local/hadoop/bin/hdfs dfs -put %s %s" % (self.temp_file_path,
hadoop_fullpath))
os.system("sudo -u ubuntu /usr/local/hadoop/bin/hdfs dfs -put %s %s" % (self.temp_file_path,
cached_fullpath))
os.remove(self.temp_file_path)
timestamp = time.strftime('%Y%m%d%H%M%S')
self.temp_file_path = "%s/kafka_%s_%s_%s.dat" % (output_dir,
self.topic,
self.group,
timestamp)
self.temp_file = open(self.temp_file_path, "w")
示例14: Consumer
# 需要导入模块: from kafka.consumer import SimpleConsumer [as 别名]
# 或者: from kafka.consumer.SimpleConsumer import get_messages [as 别名]
class Consumer(object):
def __init__(self, addr, group, topic):
self.client = KafkaClient(addr)
self.consumer = SimpleConsumer(self.client, group, topic, max_buffer_size=1310720000)
self.temp_file_path = None
self.temp_file = None
self.topic = topic
self.group = group
self.block_cnt = 0
def consume_topic(self, output_dir):
timestamp = time.strftime('%Y%m%d%H%M%S')
#open file for writing
self.temp_file_path = "/home/ubuntu/FantasyFootball/ingestion/kafka_%s_%s_%s.dat" % (self.topic, self.group, timestamp)
self.temp_file = open(self.temp_file_path,"w")
one_entry = False
while True:
try:
messages = self.consumer.get_messages(count=100, block=False)
#OffsetAndMessage(offset=43, message=Message(magic=0,
# attributes=0, key=None, value='some message'))
for message in messages:
one_entry = True
self.tempfile.write(message.message.value + "\n")
if self.tempfile.tell() > 2000:
self.save_to_hdfs(output_dir)
self.consumer.commit()
except:
self.consumer.seek(0, 2)
if one_entry:
self.save_to_hdfs(output_dir, self.topic)
self.consumer.commit()
def save_to_hdfs(self, output_dir):
self.tempfile.close()
timestamp = time.strftime('%Y%m%d%H%M%S')
hadoop_path = "/user/solivero/playerpoints/history/%s_%s_%s.dat" % (self.group, self.topic, timestamp)
cached_path = "/user/solivero/playerpoints/cached/%s_%s_%s.dat" % (self.group, self.topic, timestamp)
print "Block " + str(self.block_cnt) + ": Saving file to HDFS " + hadoop_path
self.block_cnt += 1
# place blocked messages into history and cached folders on hdfs
os.system("sudo -u hdfs hdfs dfs -put %s %s" % (self.temp_file_path,hadoop_path))
os.system("sudo -u hdfs hdfs dfs -put %s %s" % (self.temp_file_path,cached_path))
os.remove(self.temp_file_path)
timestamp = time.strftime('%Y%m%d%H%M%S')
self.temp_file_path = "/home/ubuntu/fantasyfootball/ingestion/kafka_%s_%s_%s.dat" % (self.topic, self.group, timestamp)
self.temp_file = open(self.temp_file_path, "w")
示例15: Consumer
# 需要导入模块: from kafka.consumer import SimpleConsumer [as 别名]
# 或者: from kafka.consumer.SimpleConsumer import get_messages [as 别名]
class Consumer(object):
def __init__(self, addr, group, topic):
self.client = KafkaClient(addr)
self.consumer = SimpleConsumer(self.client, group, topic,
max_buffer_size=1310720000)
self.temp_file_path = None
self.temp_file = None
self.topic = topic
self.group = group
self.block_cnt = 0
os.system ( "hdfs dfs -mkdir /data2" )
def consume_topic(self, output_dir):
if not os.path.isdir ( output_dir ): os.makedirs ( output_dir )
timestamp = time.strftime('%Y%m%d%H%M%S')
self.temp_file_path = "%s/kafka_%s_%s_%s.dat" % (output_dir,
self.topic,
self.group,
timestamp)
self.temp_file = open(self.temp_file_path,"w")
while True:
try:
# get 1000 messages at a time, non blocking
messages = self.consumer.get_messages(count=1000, block=False)
# OffsetAndMessage(offset=43, message=Message(magic=0,
# attributes=0, key=None, value='some message'))
for message in messages:
self.temp_file.write(message.message.value + "\n")
# file size > 40MB
if self.temp_file.tell() > 40000000:
self.flush_to_hdfs(output_dir)
self.consumer.commit()
except:
# move to tail of kafka topic if consumer is referencing
# unknown offset
self.consumer.seek(0, 2)
def flush_to_hdfs(self, output_dir):
self.temp_file.close()
timestamp = time.strftime('%Y%m%d%H%M%S')
print "Block {}: Flushing 40MB file to HDFS => /data2".format(str(self.block_cnt))
self.block_cnt += 1
# place blocked messages into history and cached folders on hdfs
os.system("hdfs dfs -copyFromLocal %s %s" % (self.temp_file_path,
"/data2"))
os.remove(self.temp_file_path)
timestamp = time.strftime('%Y%m%d%H%M%S')
self.temp_file_path = "%s/kafka_%s_%s_%s.dat" % (output_dir,
self.topic,
self.group,
timestamp)
self.temp_file = open(self.temp_file_path, "w")