因爲咱們使用了多進程的方式作 spiker,因此就不能直接使用以下方式來入庫了python
python spiker.py | python xxx | python yyy && python redisToMysql.py
複製代碼
只能在經過整個主進程結束以後,經過獲取主進程的返回結果來決定是否作入庫操做:mysql
python spiker.py | python format.py | python writeToRedis.py
if [ "$?" == "0" ]; then
python redisToMysql.py
fi
複製代碼
入庫後的格式儘可能簡單:git
mysql> select * from respage02;
+-----+------------+------------+-------------+--------------+-------+------+
| id | bikeid | day | lat | lng | time | type |
+-----+------------+------------+-------------+--------------+-------+------+
| 1 | 8620750656 | 2018-11-07 | 29.04729065 | 119.64855231 | 14:56 | mb |
| 2 | 8620762418 | 2018-11-07 | 29.04842459 | 119.64730693 | 14:56 | mb |
| 3 | 8620663052 | 2018-11-07 | 29.08465714 | 119.66206422 | 16:53 | mb |
複製代碼
仍是在 respage01 的 django 項目上直接新增接口即可。github
urlpatterns = [
url(r'v1/respage01/$', views.Respage01.as_view(), name='Respage01'),
url(r'v1/respage02/$', views.Respage02.as_view(), name='Respage02'),
]
複製代碼
class Respage02Info(models.Model):
""" respage 02 相關的數據 """
time = models.CharField(max_length=100)
day = models.CharField(max_length=100)
bikeid = models.CharField(max_length=200)
lat = models.FloatField()
lng = models.FloatField()
type = models.CharField(max_length=100)
class Meta:
db_table = "respage02"
複製代碼
class Respage02Serializer(serializers.HyperlinkedModelSerializer):
""" 序列化Respage02相關的數據 """
class Meta:
model = Respage02Info
fields = ('time', 'lat', 'lng', 'bikeid', 'type', 'day')
複製代碼
class Respage02(APIView):
""" 獲取respage02相關的數據 """
authentication_classes = []
permission_classes = []
def get(self, request, format=None):
req = request.query_params
if 'type' not in req:
return Response({}, status=status.HTTP_400_BAD_REQUEST)
// 獲取某一天內最近時間點的最新數據
if req['type'] == 'now':
if 'day' not in req:
return Response({}, status=status.HTTP_400_BAD_REQUEST)
timelist = Respage02Info.objects.distinct().values("time").filter(day=req['day']).order_by('-time').all()
now = timelist[0]['time']
queryset = Respage02Info.objects.filter(day=req['day']).filter(time=now)
serializer = Respage02Serializer(queryset, many=True)
// 獲取某一天內的全部時間點列表
if req['type'] == 'timelist':
if 'day' not in req:
return Response({}, status=status.HTTP_400_BAD_REQUEST)
timelist = Respage02Info.objects.distinct().values("time").filter(day=req['day']).order_by('time').all()
return Response(timelist, status=status.HTTP_200_OK)
// 獲取某一天內某一時間點的單車分佈數據
if req['type'] == 'location':
if 'day' not in req or 'time' not in req:
return Response({}, status=status.HTTP_400_BAD_REQUEST)
queryset = Respage02Info.objects.filter(day=req['day']).filter(time=req['time'])
serializer = Respage02Serializer(queryset, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
複製代碼
接口相關代碼: GitHub - roubo/rouboApi: 基於Django REST framework 實現一些業務api 採集相關代碼: GitHub - roubo/roubospiker: 一些解決生活問題的小爬蟲redis