最新《Python机器学习算法应用教程》


# Application definition
#包含项目中启用的所有Django应用
INSTALLED_APPS = [
    'polls.apps.PollsConfig',#将创建的polls添加到项目中
    'NLP'
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]
二、编写视图

打开系统生成的NLP文件夹,打开文件views.py,输入如下代码,新建了一个名叫index的视图

from django.http import HttpResponse
 
 
def index(request):
    return HttpResponse("Hello, world. You're at the NLP index.")
三、编写URLconf

1、为了使得编写的index视图有一个URL映射,在同级目录下新建一个urls.py文件,在其中输入如下代码:

from django.urls import path
 
from . import views
 
urlpatterns = [
    path('', views.index, name='index'),
]
 path()函数的用法:

route(必须)

route 是一个匹配 URL 的准则(类似正则表达式)。当 Django 响应一个请求时,它会从 urlpatterns 的第一项开始,按顺序依次匹配列表中的项,直到找到匹配的项。

view(必须)

当 Django 找到了一个匹配的准则,就会调用这个特定的视图函数,并传入一个HttpRequest 对象作为第一个参数,被“捕获”的参数以关键字参数的形式传入。

name(可选)

为 URL 取名能使你在 Django 的任意地方唯一地引用它,尤其是在模板中。这个有用的特性允许你只改一个文件就能全局地修改某个 URL 模式。

kwargs(可选)

任意个关键字参数可以作为一个字典传递给目标视图函数.

2、在根URLconf中创建刚刚新建的NLP的urls模块,打开mysit/urls.py,并在其urlpatterns模块中插入一个include():

from django.contrib import admin
from django.urls import path,include
 
urlpatterns = [
    path('admin/', admin.site.urls),
        path('NLP',include('NLP.urls')),#包含app NLP的所有url
]
函数 inclde()允许引用其它 URLconfs。每当 Django 遇到 :func:~django.urls.include 时,它会截断与此项匹配的 URL 的部分,并将剩余的字符串发送到 URLconf 以供进一步处理。

四、运行查看

现在就可以看看效果了,运行:

py manage.py runserver
--------------------- 


json = {
    "metric": 16,
    "timestamp": 1551285963,
    "value": "sdfasd",
    "tags": {
       "operationValue": "HDFF88FFF",
       "gateMac": "00E2690CDFFD",
    }
}
发送上述的结构体,进行3分钟的测试:

java测试结果:1秒大概10万条数据

python测试结果: 1秒大概1万多条数据

java测试代码:
package com.flink;
 
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.mortbay.util.ajax.JSON;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
public class sendTest {
    private static CloseableHttpClient httpClient;
//    private static HttpPost httpPost;
 
    public static void  main(String[] args) throws Exception {
        int Total= 1000000;
        int Num = 0;
        httpClient = HttpClients.createDefault();
        long Starttime = System.currentTimeMillis();
 
        for(int i = 0; i< Total; i++){
            List<Object> List = new ArrayList<>();
            Map<String, Object> map1 = new HashMap<>();
            Map<String, String> tags = new HashMap<>();
            tags.put("operationValue", "HDFF88FFF");
            tags.put("gateMac", "00E2690CDFFD");
            map1.put("metric", 10);
            map1.put("value", i);
            map1.put("tags", tags);
 
//            System.out.println(i + "条数据");
            map1.put("timestamp", Starttime/1000 + i + 3000000);
            List.add(map1);
            Num += 1;
            if (Num >= 200) {
                long Starttime1 = System.currentTimeMillis();
                sendPost("http://192.168.3.101:4242/api/put?async", List);
                long Endtime1 = System.currentTimeMillis();
                System.out.println("==============================opentsdb写入耗时=============================: " + (Endtime1-Starttime1));
                Num = 0;
                List.clear();
            }
        }
 
        long EndTime = System.currentTimeMillis();
        System.out.println(Starttime);
        System.out.println(EndTime);
        System.out.println("处理" + Total + "条数据, 消耗: " + (EndTime - Starttime));
    }
 
 
 
    public static void sendPost(final String url, List<Object> list) throws IOException {
        final HttpPost httpPost = new HttpPost(url);
        if (! list.isEmpty()) {
//            System.out.println(list);
            String string = JSON.toString(list);
            StringEntity entity = new StringEntity(string, "utf-8");
            entity.setContentEncoding("utf-8");
            entity.setContentType("application/json");
            httpPost.setEntity(entity);
            httpClient.execute(httpPost);
        }
    }
}
 

python测试代码
#coding:utf-8
import time
import requests
import uuid
import math
import random
import time, threading
start = time.time()
 
start_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
 
 
def get_value(num):
    return math.sin(num)+1
 
 
def send_json(json, s, ip):
    url = "http://%s:4242/api/put" % ip
    r = s.post(url, json=json)
    return r.text
 
 
def main(metric, ip, Num):
    s = requests.Session()
    a = int(time.time() + 2000000) - Num
    # print(a)
    ls = []
    k = random.randint(0, 10000)
 
    value = "转速"
    # value = "转速".encode('unicode_escape')
    # value = value.replace("\\u", "")
    for i in range(1, Num):
        print("%s: %s条数据" % (ip, i))
        time.sleep(2)
        json = {
            "metric": metric,
            "timestamp": a,
            "value": "sdfasd",
            "tags": {
                "operationValue": "HDFF88FFF",
                "gateMac": "00E2690CDFFD",
            }
        }
        a += 1
        k += 0.01
        ls.append(json)
 
        if len(ls) == 1:
            send_json(ls, s, ip)
            ls = []
    send_json(ls, s, ip)
    print("%s: %s条数据" % (ip, Num))
    print("开始时间: %s" % start_time)
    print("时间: %s" % time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
 
 
import json
 
 
def read_opentsdb():
    queries = [{
        "metric": 53,
        "tags": {"operationValue": "WeldCurrent"},
        "aggregator": "none",
    }]
    url = "http://192.168.3.101:4242/api/query"
    post_dict = {
        "start": 1551285963,
        "end": 1551285965,
        "queries": queries
    }
 
    ret = requests.post(url, data=json.dumps(post_dict))
    data_ret = ret.json()
    print(data_ret)
 
 
def start_opentsdb():
    main("12", "192.168.3.101", 1000000)
 
if __name__ == "__main__":
    start_opentsdb()
    # read_opentsdb()
测试结果: 1秒大概1万多条数据


--------------------- 

猜你喜欢

转载自blog.csdn.net/dqyxw520/article/details/89790280