前提
S3にアップロードされたcsvをストリームで読み込み
配列へ格納し、Opensearchへjson変換、一部の文字列を整数変換し
postをつかって転送する
Opensearch側でグラフ化を想定しているため、一部の文字列を整数へ変換する必要があります。
実現したいこと
- csvで取り込んだ再json変換転送時に一部の文字列整数変換したい
発生している問題
Jsonを受け取ったOpensearch側でのGET結果
整数変換転送すると2桁以上で正常に変換できない
"_index" : "lamda-s3-index", "_type" : "_doc", "_id" : "xxxxxx", "_score" : 1.0, "_source" * { "vmanagename" : "vnamaenamexxxx" "hostname" : "hostnamexxxxx" "devicemodel" : "devicemodelxxxxx" "session1" : [ 1, 0 ], "session2 : [ 9 ], "sessionmax" : [ 9, 9, 9 ]
文字列転送の場合
"session1" : "10", "session2 : "9", "sessionmax" : "999"
該当のソースコード
csvサンプル
vmanagename, 1.1.1.1, hostname, devicemodel, 10, 9, 999
Lambda(Python)
1 2import boto3 3import re 4import requests 5import csv 6import io 7from requests_aws4auth import AWS4Auth 8 9#配列 10vmanagename = [] 11ip = [] 12hostname = [] 13devicemodel = [] 14session1 = [] 15session2 = [] 16sessionmax = [] 17 18 19region = '' # e.g. us-west-1 20service = 'es' 21credentials = boto3.Session().get_credentials() 22awsauth = AWS4Auth(credentials.access_key, credentials.secret_key, region, service, session_token=credentials.token) 23 24host = '' # the OpenSearch Service domain, e.g. https://search-mydomain.us-west-1.es.amazonaws.com 25index = 'lambda-s3-index' 26type = '_doc' 27url = host + '/' + index + '/' + type 28 29headers = { "Content-Type": "application/json" } 30 31s3 = boto3.client('s3') 32 33# Lambda execution starts here 34def handler(event, context): 35 for record in event['Records']: 36 37 # Get the bucket name and key for the new file 38 bucket = record['s3']['bucket']['name'] 39 key = record['s3']['object']['key'] 40 41 # Get, read, and split the file into lines 42 obj = s3.get_object(Bucket=bucket, Key=key) 43 body = obj['Body'].read() 44 textIo = io.TextIOWrapper(io.BytesIO(body)) 45 46 # Match the regular expressions to each line and index the JSON 47 for row in lines csv.reader(textIo): 48 print(row) 49 vmanagename = row[0] 50 ip = row[1] 51 devicemodel = row[2] 52 hostname = row[3] 53 session1 = row[4] 54 session2 = row[5] 55 sessionmax = row[6] 56 print(row[0], row[1], row[2], row[3], row[4], row[5], row[6]) 57 58 document = { "vmanagename ": vmanagename , "ip ": ip , "devicemodel ": devicemodel , "hostname ": hostname , "session1 ": list(map(int, session1)) , "session2": list(map(int, session2)), "sessionmax ": list(map(int, sessionmax ))} 59 r = requests.post(url, auth=awsauth, json=document, headers=headers)
試したこと
ストリームで取り込んだ文字列を整数変換しようと調べた結果
mapを使用すると変換できるとのことだったので使用してみましたが
正常に整数に変換することができず。
list(map(int, session2)
補足情報(FW/ツールのバージョンなど)
Python3.9

回答1件
あなたの回答
tips
プレビュー