重点技术-20170707-阿里云-日志服务-JAVA API写入日志

------ ------每一个shard的限制 ------ ------
  • 写入:5MB/s,2000次/s
  • 读取:10MB/s,100次/s
  • 写入数据建议能批量上传,批量限制每次上传需要小于3M或者4096条。(每条数据3个参数;1000条数据做为一个LogGroup,2万条数据提交用时2-4秒;2000条数据做为一个LogGroup,4万条数据提交用时4-6秒;
---------Maven依赖---------
<!-- 日志服务 -->
<dependency>
<groupId>com.aliyun.openservices</groupId>
<artifactId>aliyun-log</artifactId>
<version>0.6.6</version>
</dependency>

---------示例代码---------
        String endpoint = "cn-hangzhou.log.aliyuncs.com"; // 选择与上面步骤创建 project 所属区域匹配的 Endpoint
        String accessKeyId = S_AliyunAccount.AccessKey; // 使用您的阿里云访问密钥 AccessKeyId
        String accessKeySecret = S_AliyunAccount.SecretKey; // 使用您的阿里云访问密钥 AccessKeySecret
        String project = "logchisalsoft3355"; // 上面步骤创建的项目名称
        String logstore = "logstorechisal3355"; // 上面步骤创建的日志库名称

        // 构建一个客户端实例
        Client client = new Client(endpoint, accessKeyId, accessKeySecret);

        // 写入日志
        String topic = "testtopic";
        String source = "tony-pc";
        long start = System.currentTimeMillis();
        System.out.println("开始写入:"+start);
        
        int startIndex = 0;
        int countPerLog = 4000;
        for (int i = startIndex; i < startIndex+40; i++) {
            Vector<LogItem> logGroup = new Vector<LogItem>();
            for (int j = 0; j < countPerLog; j++) {
                LogItem logItem = new LogItem((int) (new Date().getTime() / 1000));
                logItem.PushBack("theIndex", String.valueOf(i * countPerLog + j));
                logItem.PushBack("theValue", "备注字段内容"+(i * countPerLog + j));
                logItem.PushBack("theTime", System.currentTimeMillis()+"");
                logGroup.add(logItem);
            }
            PutLogsRequest req2 = new PutLogsRequest(project, logstore, topic, source, logGroup);
            client.PutLogs(req2);
            /*
             * 发送的时候也可以指定将数据发送至有一个特定的 shard,只要设置 shard 的 hashkey,则数据会写入包含该
             * hashkey 的 range 所对应的 shard,具体 API 参考以下接口: public PutLogsResponse
             * PutLogs( String project, String logStore, String topic,
             * List<LogItem> logItems, String source, String shardHash // 根据
             * hashkey 确定写入 shard,hashkey 可以是 MD5(ip) 或 MD5(id) 等 ) throws
             * LogException;
             */
        }
        long end = System.currentTimeMillis();
        System.out.println("结束写入:"+end);
        System.out.println("用时:"+(end-start));

猜你喜欢

转载自blog.csdn.net/namelessfighter/article/details/80547299