REST API Format : http://host:port/[index]/[type]/[_action/id]
Yukarıda formattaki gibi url uzantımız Elasticsearch ve port adresimiz ayrıca oluşturduğumuz index ve type yazarak bu url ye POST edebiliriz.
Örneğin; benim oluşturduğum index adı “sap_test” type adıda “data” url’miz aşağıdaki gibi olacaktır.
http://0.0.0.0:9200/sap_test/data
index oluşturma detaylı bilgi için aşağıdaki linke göz atabilirsiniz. Bu konuda çok detaya girmeyeceğim.
https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html
ayrıca birde bu index ve type’ın içinde hangi alanların olacağı tipleri yani mapping ayarları için yine aşağıdaki linke göz atabilirsiniz.
https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html
Json Format aşağıdaki gibidir.
1 2 3 4 5 6 |
{ "name": "Mustafa Hamit", "age": 31, "email": "info@mustafahamit.com" } |
Benim mapping’im için yukarıdaki gibi test için 3 alandan oluşturdum.
Önce bunu postman ve advanced rest client benzeri bir tool ile test edebiliriz.
![](http://www.mustafahamit.com/wp-content/uploads/2019/11/1-1-1024x409.png?v=1575112405)
Bu verileri gönderebilmek için dediğim gibi Elasticsearch’te index ve type oluşturmalıyız ayrıca bunların mapping’lerini yapmalıyız.
Sonrasında veriyi gönderdik mi aşağıdaki gibi başarılı bir sonuç alırız.
![](http://www.mustafahamit.com/wp-content/uploads/2019/11/2.png)
Kontrol etmek için Elasticsearch dev tools’u kullanabiliriz. Buradan da komutlar yazıp aynı işlemleri yapabiliriz.
![](http://www.mustafahamit.com/wp-content/uploads/2019/11/3-1024x699.jpg)
Yukarıdaki gibi gönderdiğimiz veriler gelmiş.
![](http://www.mustafahamit.com/wp-content/uploads/2019/11/4-1-1024x357.png?v=1575112448)
Aynı şekilde advanced rest client ile de bu verileri çekebiliriz.
Dönüş JSON formatı aşağıdaki gibidir.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
{ "took": 0, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped": 0, "failed": 0 }, "hits": { "total": { "value": 1, "relation": "eq" }, "max_score": 1.0, "hits": [{ "_index": "sap_test", "_type": "data", "_id": "k_zUtW4BEXympLPHzxyi", "_score": 1.0, "_source": { "name": "Mustafa Hamit", "age": 31, "email": "info@mustafahamit.com" } }] } } |
![](http://www.mustafahamit.com/wp-content/uploads/2019/11/5.png)
Hadi birde bunun abap kodunu yazalım 🙂
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
REPORT zmh_test. DATA : lo_http_client TYPE REF TO if_http_client, lv_url TYPE string, lv_payload TYPE string, lv_payload_x TYPE xstring, lv_response TYPE string, lv_xresponse TYPE xstring, lv_code TYPE i, lv_reason TYPE string. DATA : BEGIN OF lt_send OCCURS 1, name TYPE string, age TYPE string, email TYPE string, END OF lt_send. CLEAR lt_send. lt_send-name = 'Mustafa Test'. lt_send-age = 18. lt_send-email = 'mustafa@mustafahamit.com'. APPEND lt_send. lv_url = 'http://0.0.0.0:9200/sap_test/data'. ********************************************************************** lv_payload = cl_fdt_json=>data_to_json( ia_data = lt_send ). cl_http_client=>create_by_url( EXPORTING url = lv_url IMPORTING client = lo_http_client EXCEPTIONS argument_not_found = 1 plugin_not_active = 2 internal_error = 3 OTHERS = 4 ). CLEAR lv_payload_x. CALL FUNCTION 'SCMS_STRING_TO_XSTRING' EXPORTING text = lv_payload IMPORTING buffer = lv_payload_x. lo_http_client->request->set_method( if_http_request=>co_request_method_post ). lo_http_client->request->set_content_type( 'application/json; charset=UTF-8' ). lo_http_client->request->set_header_field( name = 'Accept-Language' value = 'tr-TR' ). *lo_http_client->request->set_header_field( name = 'Accept-Charset' value = 'UTF-8' ). lo_http_client->request->set_data( lv_payload_x ). "Send lo_http_client->send( EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2 ). "response lo_http_client->receive( EXCEPTIONS http_communication_failure = 1 http_invalid_state = 2 http_processing_failed = 3 ). CLEAR : lv_response,lv_xresponse. * lv_response = lo_http_client->response->get_cdata( ). lv_xresponse = lo_http_client->response->get_data( ). CALL FUNCTION 'ECATT_CONV_XSTRING_TO_STRING' EXPORTING im_xstring = lv_xresponse im_encoding = 'UTF-8' IMPORTING ex_string = lv_response. REPLACE ALL OCCURRENCES OF cl_abap_char_utilities=>cr_lf IN lv_response WITH ''. REPLACE ALL OCCURRENCES OF cl_abap_char_utilities=>horizontal_tab IN lv_response WITH ''. REPLACE ALL OCCURRENCES OF cl_abap_char_utilities=>vertical_tab IN lv_response WITH ''. REPLACE ALL OCCURRENCES OF cl_abap_char_utilities=>newline IN lv_response WITH ''. REPLACE ALL OCCURRENCES OF cl_abap_char_utilities=>form_feed IN lv_response WITH ''. REPLACE ALL OCCURRENCES OF cl_abap_char_utilities=>backspace IN lv_response WITH ''. CLEAR : lv_code, lv_reason. lo_http_client->response->get_status( IMPORTING code = lv_code reason = lv_reason ). IF lv_code NE '201' AND lv_reason NE 'Created'. MESSAGE 'Mesajlar Gönderilemedi !' TYPE 'I'. ENDIF. lo_http_client->close( EXCEPTIONS http_invalid_state = 1 ). BREAK-POINT. |
Programı çalıştırtıktan sonra sonuç 🙂
![](http://www.mustafahamit.com/wp-content/uploads/2019/11/6-1024x734.jpg)
Yukarıdaki gibi son datamızda gelmiştir iyi eğlenceler 🙂