{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "send message succeed.\n"
     ]
    }
   ],
   "source": [
    "import ssl\n",
    "from kafka import KafkaProducer\n",
    "from kafka.errors import KafkaError\n",
    "from sqlalchemy import false, true\n",
    "\n",
    "\n",
    "context = ssl.create_default_context()\n",
    "context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)\n",
    "## The new python(2.7.8+) may cannot ignore the hostname check,\n",
    "## you could set to ssl.CERT_NONE to walk around the problem,\n",
    "## or you can change the client to confluent-python-demo \n",
    "\n",
    "# context.verify_mode = ssl.CERT_NONE\n",
    "context.verify_mode = ssl.CERT_REQUIRED\n",
    "\n",
    "# context.check_hostname = True\n",
    "context.load_verify_locations(\"ca-cert\")\n",
    "\n",
    "producer = KafkaProducer(bootstrap_servers=['alikafka-pre-cn-8ed2kw57901x-1.alikafka.aliyuncs.com:9093',\n",
    "                                            'alikafka-pre-cn-8ed2kw57901x-2.alikafka.aliyuncs.com:9093',\n",
    "                                            'alikafka-pre-cn-8ed2kw57901x-3.alikafka.aliyuncs.com:9093'],\n",
    "                         sasl_mechanism=\"PLAIN\",\n",
    "                         ssl_context=context,\n",
    "                         security_protocol='SASL_SSL',\n",
    "                         api_version = (2,2),\n",
    "                         retries=5,\n",
    "                         sasl_plain_username='alikafka_pre-cn-8ed2kw57901x',\n",
    "                         sasl_plain_password='hlQsApgUUdUxcWEr1uQoM9BeuF8t8vMF')\n",
    "\n",
    "# partitions = producer.partitions_for(conf['topic_name'])\n",
    "# print('Topic下分区: %s' % partitions)\n",
    "\n",
    "try:\n",
    "    future = producer.send(\"test\", 'hello aliyun-kafka!'.encode('utf-8'))\n",
    "    future.get()\n",
    "    print('send message succeed.')\n",
    "except KafkaError as e:\n",
    "    print('send message failed.')\n",
    "    print(e)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 108045存储成功成功82存储成功197存储成功233存储成功456存储成功569存储成功838存储成功846存储成功1107存储成功1109存储成功1156存储成功1460存储成功1474存储成功1520存储成功1551存储成功1690存储成功1723存储成功1726存储成功1931存储成功2019存储成功2062存储成功2101存储成功2387存储成功2388存储成功2491存储成功2499存储成功2623存储成功2744存储成功2821存储成功2823存储成功2857存储成功2913存储成功3005存储成功3212存储成功3282存储成功3374存储成功3466存储成功3572存储成功3585存储成功3654存储成功3739存储成功3747存储成功3869存储成功4087存储成功4155存储成功4171存储成功4245存储成功4282存储成功4283存储成功4285存储成功4351存储成功4510存储成功4617存储成功4619存储成功4847存储成功5069存储成功5079存储成功5206存储成功5257存储成功5297存储成功5339存储成功5402存储成功5598存储成功5660存储成功5695存储成功5721存储成功5867存储成功5876存储成功5915存储成功5956存储成功6027存储成功6036存储成功6093存储成功6164存储成功6165存储成功6335存储成功6346存储成功6376存储成功6408存储成功6618存储成功6904存储成功7126存储成功7163存储成功7282存储成功7328存储成功7398存储成功7538存储成功7564存储成功7629存储成功7693存储成功7700存储成功7921存储成功7990存储成功8015存储成功8089存储成功8295存储成功8366存储成功8372存储成功8403存储成功8412存储成功8689存储成功8724存储成功8742存储成功8775存储成功8800存储成功8810存储成功8894存储成功8937存储成功9112存储成功9187存储成功9194存储成功9306存储成功9307存储成功9509存储成功9522存储成功9790存储成功9932存储成功9984存储成功10013存储成功10034存储成功10040存储成功10053存储成功10143存储成功10181存储成功10205存储成功10329存储成功10370存储成功10379存储成功10393存储成功10511存储成功10542存储成功10611存储成功10615存储成功10616存储成功10640存储成功10744存储成功10839存储成功10842存储成功10992存储成功11083存储成功11176存储成功11200存储成功11382存储成功11499存储成功11769存储成功12115存储成功12145存储成功12197存储成功12213存储成功12228存储成功12378存储成功12430存储成功12553存储成功12655存储成功12749存储成功12792存储成功12830存储成功12843存储成功12845存储成功12846存储成功12859存储成功12927存储成功12967存储成功13032存储成功13120存储成功13122存储成功13136存储成功13140存储成功13147存储成功13154存储成功13269存储成功13335存储成功13421存储成功13451存储成功13516存储成功13602存储成功13609存储成功13621存储成功13625存储成功13762存储成功13768存储成功13826存储成功13939存储成功13983存储成功14032存储成功14042存储成功14056存储成功14065存储成功14113存储成功14178存储成功14212存储成功14274存储成功14321存储成功14363存储成功14375存储成功14378存储成功14561存储成功14578存储成功14618存储成功14687存储成功14712存储成功14876存储成功14901存储成功15077存储成功15248存储成功15421存储成功15472存储成功15495存储成功15545存储成功15546存储成功15563存储成功15628存储成功15631存储成功15666存储成功15814存储成功15899存储成功15917存储成功15978存储成功15982存储成功16024存储成功16160存储成功16307存储成功16506存储成功16641存储成功16694存储成功16695存储成功17099存储成功17102存储成功17114存储成功17137存储成功17141存储成功17172存储成功17197存储成功17393存储成功17449存储成功17602存储成功17642存储成功17661存储成功17689存储成功17877存储成功17934存储成功17990存储成功18054存储成功18076存储成功18117存储成功18133存储成功18150存储成功18206存储成功18253存储成功18406存储成功18452存储成功18489存储成功18490存储成功18575存储成功18602存储成功18648存储成功18688存储成功18733存储成功18738存储成功18759存储成功18800存储成功18835存储成功18845存储成功19031存储成功19119存储成功19133存储成功19251存储成功19352存储成功19406存储成功19407存储成功19502存储成功19563存储成功19632存储成功19676存储成功19946存储成功20055存储成功20275存储成功20316存储成功20350存储成功20412存储成功20426存储成功20430存储成功20543存储成功20653存储成功20672存储成功21062存储成功21072存储成功21163存储成功21216存储成功21221存储成功21224存储成功21508存储成功21648存储成功21668存储成功21671存储成功21920存储成功21934存储成功22099存储成功22347存储成功22348存储成功22439存储成功22489存储成功22586存储成功22600存储成功22602存储成功22682存储成功22740存储成功22846存储成功22865存储成功22901存储成功23143存储成功23222存储成功23314存储成功23345存储成功23363存储成功23429存储成功23462存储成功23518存储成功23519存储成功23523存储成功23564存储成功23695存储成功23747存储成功23872存储成功23885存储成功23940存储成功24017存储成功24050存储成功24158存储成功24217存储成功24284存储成功24369存储成功24377存储成功24402存储成功24532存储成功24556存储成功24570存储成功24592存储成功24625存储成功24649存储成功24651存储成功24717存储成功24784存储成功24869存储成功24964存储成功24965存储成功24966存储成功24994存储成功25235存储成功25523存储成功25878存储成功25926存储成功25952存储成功25987存储成功26186存储成功26339存储成功26428存储成功26463存储成功26502存储成功26534存储成功26550存储成功26597存储成功26631存储成功26806存储成功26843存储成功26846存储成功26989存储成功27072存储成功27073存储成功27287存储成功27422存储成功27448存储成功27484存储成功27517存储成功27531存储成功27542存储成功27584存储成功27668存储成功27935存储成功27989存储成功27992存储成功28191存储成功28266存储成功28267存储成功28270存储成功28308存储成功28321存储成功28393存储成功28395存储成功28397存储成功28440存储成功28477存储成功28526存储成功28554存储成功28565存储成功28580存储成功28606存储成功28737存储成功28763存储成功28853存储成功29041存储成功29075存储成功29252存储成功29457存储成功29474存储成功29608存储成功29612存储成功29659存储成功29660存储成功29681存储成功29704存储成功29780存储成功29905存储成功30126存储成功30129存储成功30443存储成功30593存储成功30754存储成功30758存储成功30776存储成功30954存储成功30970存储成功31024存储成功31134存储成功31151存储成功31152存储成功31401存储成功31567存储成功31669存储成功31777存储成功31907存储成功31924存储成功31962存储成功31963存储成功32035存储成功32147存储成功32170存储成功32362存储成功32518存储成功32729存储成功32913存储成功32961存储成功32970存储成功33041存储成功33042存储成功33070存储成功33091存储成功33320存储成功33408存储成功33425存储成功33601存储成功33643存储成功33648存储成功33725存储成功34210存储成功34241存储成功34265存储成功34382存储成功34384存储成功34412存储成功34435存储成功34444存储成功34460存储成功34665存储成功34711存储成功34740存储成功34756存储成功34786存储成功34940存储成功34971存储成功34988存储成功35212存储成功35284存储成功35290存储成功35344存储成功35356存储成功35385存储成功35386存储成功35504存储成功35608存储成功35767存储成功35768存储成功35847存储成功35920存储成功36017存储成功36045存储成功36076存储成功36082存储成功36090存储成功36207存储成功36229存储成功36231存储成功36257存储成功36290存储成功36524存储成功36574存储成功36575存储成功36782存储成功36840存储成功36873存储成功36930存储成功37016存储成功37160存储成功37161存储成功37174存储成功37259存储成功37270存储成功37271存储成功37816存储成功37828存储成功37872存储成功37945存储成功38063存储成功38064存储成功38073存储成功38090存储成功38091存储成功38176存储成功38274存储成功38404存储成功38555存储成功38632存储成功39008存储成功39107存储成功39108存储成功39114存储成功39414存储成功39604存储成功39629存储成功39723存储成功39724存储成功39769存储成功39927存储成功39996存储成功40019存储成功40097存储成功40186存储成功40245存储成功40330存储成功40349存储成功40445存储成功40446存储成功40528存储成功40531存储成功40683存储成功40717存储成功40719存储成功40734存储成功40760存储成功40846存储成功40894存储成功41229存储成功41230存储成功41280存储成功41419存储成功41597存储成功41632存储成功41669存储成功41754存储成功41765存储成功41947存储成功41955存储成功41956存储成功42053存储成功42065存储成功42095存储成功42111存储成功42192存储成功42254存储成功42346存储成功42554存储成功42603存储成功42732存储成功42875存储成功43005存储成功43028存储成功43045存储成功43147存储成功43395存储成功43408存储成功43409存储成功43414存储成功43466存储成功43566存储成功43606存储成功43629存储成功43669存储成功43872存储成功43993存储成功44060存储成功44098存储成功44223存储成功44234存储成功44236存储成功44260存储成功44424存储成功44441存储成功44508存储成功44513存储成功44552存储成功44577存储成功44605存储成功44623存储成功44653存储成功44660存储成功44661存储成功44830存储成功44853存储成功45323存储成功45348存储成功45380存储成功45414存储成功45448存储成功45492存储成功45508存储成功45682存储成功45769存储成功45770存储成功45943存储成功46023存储成功46037存储成功46048存储成功46108存储成功46225存储成功46226存储成功46250存储成功46473存储成功46515存储成功46516存储成功46519存储成功46636存储成功46722存储成功46774存储成功46776存储成功46780存储成功46809存储成功46855存储成功46866存储成功46867存储成功46889存储成功47165存储成功47199存储成功47243存储成功47492存储成功47517存储成功47551存储成功47592存储成功47627存储成功47647存储成功47710存储成功47740存储成功47835存储成功48143存储成功48155存储成功48215存储成功48294存储成功48705存储成功48878存储成功48885存储成功48887存储成功48951存储成功49345存储成功49542存储成功49558存储成功49559存储成功49584存储成功49600存储成功49912存储成功49917存储成功49941存储成功49978存储成功50091存储成功50286存储成功50451存储成功50544存储成功50734存储成功50735存储成功50773存储成功50786存储成功50804存储成功50830存储成功51026存储成功51041存储成功51060存储成功51233存储成功51306存储成功51578存储成功51626存储成功51752存储成功51989存储成功52078存储成功52110存储成功52126存储成功52153存储成功52283存储成功52835存储成功52924存储成功52979存储成功53000存储成功53001存储成功53003存储成功53150存储成功53377存储成功53640存储成功53655存储成功53670存储成功53682存储成功53735存储成功53794存储成功54086存储成功54291存储成功54305存储成功54338存储成功54363存储成功54454存储成功54588存储成功54653存储成功54906存储成功54909存储成功54931存储成功55011存储成功55090存储成功55126存储成功55190存储成功55237存储成功55495存储成功55506存储成功55520存储成功55521存储成功55613存储成功55798存储成功56134存储成功56268存储成功56270存储成功56491存储成功56661存储成功56671存储成功56949存储成功56988存储成功57033存储成功57193存储成功57215存储成功57242存储成功57268存储成功57302存储成功57335存储成功57474存储成功57723存储成功57748存储成功57749存储成功57809存储成功57871存储成功57890存储成功57897存储成功58202存储成功58480存储成功58494存储成功58688存储成功58699存储成功58712存储成功58894存储成功58899存储成功58930存储成功58931存储成功58957存储成功58963存储成功59054存储成功59162存储成功59191存储成功59361存储成功59386存储成功59514存储成功59572存储成功59591存储成功59648存储成功59669存储成功59704存储成功59705存储成功59738存储成功59814存储成功59830存储成功59860存储成功60002存储成功60012存储成功60015存储成功60047存储成功60057存储成功60058存储成功60162存储成功60172存储成功60256存储成功60264存储成功60334存储成功60340存储成功60345存储成功60408存储成功60414存储成功60768存储成功60797存储成功60862存储成功60895存储成功60900存储成功60944存储成功60994存储成功61042存储成功61086存储成功61095存储成功61216存储成功61384存储成功61672存储成功61833存储成功61877存储成功61892存储成功61999存储成功62000存储成功62174存储成功62208存储成功62210存储成功62211存储成功62242存储成功62250存储成功62302存储成功62311存储成功62427存储成功62716存储成功62837存储成功62852存储成功62949存储成功62985存储成功63027存储成功63028存储成功63172存储成功63236存储成功63258存储成功63529存储成功63566存储成功63602存储成功63695存储成功63846存储成功63998存储成功64017存储成功64024存储成功64048存储成功64050存储成功64101存储成功64167存储成功64319存储成功64417存储成功64450存储成功64498存储成功64558存储成功64566存储成功64568存储成功64632存储成功64794存储成功64856存储成功64882存储成功64899存储成功65017存储成功65023存储成功65077存储成功65133存储成功65163存储成功65228存储成功65281存储成功65323存储成功65372存储成功65426存储成功65498存储成功65507存储成功65589存储成功65599存储成功65609存储成功65666存储成功65757存储成功65760存储成功66021存储成功66132存储成功66166存储成功66180存储成功66218存储成功66259存储成功66373存储成功66468存储成功66473存储成功66642存储成功66719存储成功66729存储成功66832存储成功67185存储成功67206存储成功67231存储成功67251存储成功67273存储成功67419存储成功67734存储成功67744存储成功67775存储成功67796存储成功67810存储成功67877存储成功68122存储成功68127存储成功68159存储成功68225存储成功68283存储成功68519存储成功68521存储成功68591存储成功68675存储成功68697存储成功68793存储成功68862存储成功68886存储成功68935存储成功69002存储成功69003存储成功69005存储成功69134存储成功69135存储成功69161存储成功69304存储成功69415存储成功69543存储成功69544存储成功69709存储成功69712存储成功69727存储成功69732存储成功69812存储成功69814存储成功69942存储成功69985存储成功70023存储成功70107存储成功70399存储成功70589存储成功70600存储成功70795存储成功71066存储成功71068存储成功71121存储成功71180存储成功71425存储成功71474存储成功71503存储成功71622存储成功71674存储成功71683存储成功71686存储成功71715存储成功71757存储成功71799存储成功71838存储成功71894存储成功72154存储成功72168存储成功72188存储成功72285存储成功72318存储成功72387存储成功72421存储成功72450存储成功72499存储成功72717存储成功72746存储成功72836存储成功72840存储成功72958存储成功73166存储成功73281存储成功73401存储成功73436存储成功73546存储成功73743存储成功73755存储成功73760存储成功73782存储成功73841存储成功74038存储成功74149存储成功74236存储成功74272存储成功74300存储成功74385存储成功74502存储成功74508存储成功74517存储成功74573存储成功74670存储成功74798存储成功74816存储成功74817存储成功74859存储成功74868存储成功74869存储成功74871存储成功74893存储成功74934存储成功74968存储成功74977存储成功75036存储成功75161存储成功75183存储成功75357存储成功75570存储成功75769存储成功75805存储成功75815存储成功75937存储成功75988存储成功75995存储成功76000存储成功76074存储成功76161存储成功76175存储成功76193存储成功76214存储成功76226存储成功76249存储成功76266存储成功76468存储成功76498存储成功76710存储成功76876存储成功77252存储成功77274存储成功77406存储成功77681存储成功77917存储成功77938存储成功78071存储成功78107存储成功78137存储成功78223存储成功78334存储成功78378存储成功78476存储成功78583存储成功78647存储成功78777存储成功78876存储成功79071存储成功79154存储成功79213存储成功79300存储成功79445存储成功79768存储成功79883存储成功80080存储成功80085存储成功80171存储成功80190存储成功80202存储成功80271存储成功80413存储成功80414存储成功80473存储成功80600存储成功80664存储成功80778存储成功80822存储成功80835存储成功80893存储成功80894存储成功80903存储成功81003存储成功81005存储成功81200存储成功81240存储成功81303存储成功81320存储成功81749存储成功81793存储成功81795存储成功81824存储成功81871存储成功81882存储成功81916存储成功81929存储成功81986存储成功82042存储成功82127存储成功82128存储成功82156存储成功82186存储成功82203存储成功82228存储成功82237存储成功82291存储成功82317存储成功82397存储成功82410存储成功82446存储成功82468存储成功82492存储成功82520存储成功82534存储成功82628存储成功82821存储成功82844存储成功82958存储成功82983存储成功83176存储成功83432存储成功83465存储成功83515存储成功83680存储成功83970存储成功83979存储成功84109存储成功84192存储成功84197存储成功84221存储成功84242存储成功84350存储成功84394存储成功84428存储成功84564存储成功84760存储成功84831存储成功85045存储成功85256存储成功85388存储成功85418存储成功85482存储成功85483存储成功85551存储成功85634存储成功85637存储成功85766存储成功85800存储成功85811存储成功85961存储成功86374存储成功86424存储成功86433存储成功86533存储成功86553存储成功86562存储成功86785存储成功86786存储成功86934存储成功86949存储成功86975存储成功87130存储成功87322存储成功87384存储成功87533存储成功87584存储成功87652存储成功87678存储成功87708存储成功87837存储成功87913存储成功87934存储成功87967存储成功88024存储成功88026存储成功88233存储成功88307存储成功88471存储成功88822存储成功88843存储成功89190存储成功89340存储成功89391存储成功89422存储成功89467存储成功89752存储成功89800存储成功89856存储成功89889存储成功89993存储成功90241存储成功90246存储成功90306存储成功90526存储成功90602存储成功90750存储成功90780存储成功90887存储成功90931存储成功90936存储成功90939存储成功91036存储成功91048存储成功91306存储成功91310存储成功91311存储成功91392存储成功91440存储成功91469存储成功91517存储成功91560存储成功91665存储成功91719存储成功91870存储成功91987存储成功92127存储成功92461存储成功92618存储成功92619存储成功92620存储成功92650存储成功92779存储成功92802存储成功92841存储成功92888存储成功92962存储成功93003存储成功93027存储成功93093存储成功93185存储成功93290存储成功93407存储成功93412存储成功93431存储成功93432存储成功93514存储成功93554存储成功93622存储成功93642存储成功93803存储成功93848存储成功93892存储成功93927存储成功94269存储成功94301存储成功94318存储成功94388存储成功94429存储成功94612存储成功94675存储成功94750存储成功95093存储成功95137存储成功95254存储成功95321存储成功95328存储成功95474存储成功95540存储成功95799存储成功95918存储成功95974存储成功96014存储成功96034存储成功96109存储成功96285存储成功96369存储成功96660存储成功96661存储成功96663存储成功96740存储成功96741存储成功96805存储成功96866存储成功96871存储成功97022存储成功97023存储成功97024存储成功97219存储成功97592存储成功97653存储成功97741存储成功98046存储成功98071存储成功98133存储成功98198存储成功98199存储成功98399存储成功98566存储成功98828存储成功98978存储成功99091存储成功99256存储成功99326存储成功99353存储成功99481存储成功99662存储成功99742存储成功99796存储成功99813存储成功99834存储成功99849存储成功99864存储成功99952存储成功99983存储成功100247存储成功100344存储成功100363存储成功100486存储成功100721存储成功100824存储成功100875存储成功101083存储成功101088存储成功101089存储成功101167存储成功101406存储成功101536存储成功101561存储成功101625存储成功101649存储成功101741存储成功101792存储成功101793存储成功101821存储成功101940存储成功102223存储成功102275存储成功102380存储成功102487存储成功102533存储成功102678存储成功102734存储成功102735存储成功102958存储成功103185存储成功103188存储成功103264存储成功103373存储成功103415存储成功103444存储成功103462存储成功103575存储成功103601存储成功103611存储成功103755存储成功104094存储成功104476存储成功104574存储成功104628存储成功104701存储成功104709存储成功104726存储成功104905存储成功105101存储成功105185存储成功105205存储成功105233存储成功105242存储成功105255存储成功105262存储成功105302存储成功105357存储成功105358存储成功105451存储成功105583存储成功105601存储成功105688存储成功105792存储成功105842存储成功105909存储成功106116存储成功106298存储成功106306存储成功106594存储成功106672存储成功106843存储成功106871存储成功106976存储成功107356存储成功107428存储成功107432存储成功107471存储成功107590存储成功107621存储成功107694存储成功107699存储成功107758存储成功107923存储成功107924存储成功"
     ]
    }
   ],
   "source": [
    "import ssl\n",
    "from kafka import KafkaProducer, KafkaConsumer\n",
    "from kafka.errors import kafka_errors\n",
    "import traceback\n",
    "import json\n",
    "import pdb\n",
    "import os\n",
    "import pandas as pd\n",
    "context = ssl.create_default_context()\n",
    "context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)\n",
    "## The new python(2.7.8+) may cannot ignore the hostname check,\n",
    "## you could set to ssl.CERT_NONE to walk around the problem,\n",
    "## or you can change the client to confluent-python-demo \n",
    "\n",
    "# context.verify_mode = ssl.CERT_NONE\n",
    "context.verify_mode = ssl.CERT_REQUIRED\n",
    "topic = 'sys_battery_pack_entity_storage_topic'\n",
    "# context.check_hostname = True\n",
    "context.load_verify_locations(\"ca-cert\")\n",
    "consumer = KafkaConsumer(topic, bootstrap_servers=['alikafka-pre-cn-8ed2kw57901x-1.alikafka.aliyuncs.com:9093',\n",
    "                                            'alikafka-pre-cn-8ed2kw57901x-2.alikafka.aliyuncs.com:9093',\n",
    "                                            'alikafka-pre-cn-8ed2kw57901x-3.alikafka.aliyuncs.com:9093'],\n",
    "                         sasl_mechanism=\"PLAIN\",\n",
    "                         ssl_context=context,\n",
    "                         security_protocol='SASL_SSL',\n",
    "                         api_version = (2,2),\n",
    "                         sasl_plain_username='alikafka_pre-cn-8ed2kw57901x',\n",
    "                         sasl_plain_password='hlQsApgUUdUxcWEr1uQoM9BeuF8t8vMF',\n",
    "                         consumer_timeout_ms= 100000000, # 如果10秒内kafka中没有可供消费的数据，自动退出\n",
    "                         client_id='consumer-python3',\n",
    "                         auto_offset_reset='earliest',# 消费kafka中最近的数据，如果设置为earliest则消费最早的数据，不管这些数据是否消费\n",
    "                         enable_auto_commit=True, # 自动提交消费者的offset\n",
    "                         auto_commit_interval_ms=3000, ## 自动提交消费者offset的时间间隔,\n",
    "\n",
    ")\n",
    "\n",
    "i = 1\n",
    "for message in consumer:\n",
    "    if i == 1:\n",
    "        i = i + 1\n",
    "        continue\n",
    "    if topic+'.csv' not in os.listdir():\n",
    "        pd.DataFrame(json.loads(str(message.value,'utf-8')),index=[0]).to_csv(topic+'.csv', index=False)\n",
    "    else:\n",
    "        pd.DataFrame(json.loads(str(message.value,'utf-8')),index=[0]).to_csv(topic+'.csv', mode='a', index=False, header=None)\n",
    "    i = i + 1\n",
    "    print(\"\\r\",str(i)+\"   存储成功\",end=\"\",flush=True)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>code</th>\n",
       "      <th>create_time</th>\n",
       "      <th>id</th>\n",
       "      <th>model_id</th>\n",
       "      <th>vin</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0CZPE007DN0111B2P0000010</td>\n",
       "      <td>2021-03-31 16:05:38</td>\n",
       "      <td>000038a9f8c745a19da116d82085e54c</td>\n",
       "      <td>2101TAB</td>\n",
       "      <td>LUZAGAAA4MA008178</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                       code          create_time  \\\n",
       "0  0CZPE007DN0111B2P0000010  2021-03-31 16:05:38   \n",
       "\n",
       "                                 id model_id                vin  \n",
       "0  000038a9f8c745a19da116d82085e54c  2101TAB  LUZAGAAA4MA008178  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.DataFrame(json.loads(str(message.value,'utf-8')),index=[0]).to_csv(topic+'.csv', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ConsumerRecord(topic='sys_battery_pack_entity_storage_topic', partition=0, offset=1, timestamp=1645586299071, timestamp_type=0, key=None, value=b'{\"code\":\"0CZPE007DN0111B2P0000010\",\"create_time\":\"2021-03-31 16:05:38\",\"id\":\"000038a9f8c745a19da116d82085e54c\",\"model_id\":\"2101TAB\",\"vin\":\"LUZAGAAA4MA008178\"}', headers=[], checksum=None, serialized_key_size=-1, serialized_value_size=158, serialized_header_size=-1)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "message"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "5ac93a79b26608c768d42fbd754dd4f69161017bfc4d38cb4b18d60e5198dbca"
  },
  "kernelspec": {
   "display_name": "Python 3.6.2 ('ana_py36')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
