Mustafa HAMIT

SAP’den Elasticsearch’e Veri Göndermek

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.

{
	"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.

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.

Kontrol etmek için Elasticsearch dev tools’u kullanabiliriz. Buradan da komutlar yazıp aynı işlemleri yapabiliriz.

Yukarıdaki gibi gönderdiğimiz veriler gelmiş.

Aynı şekilde advanced rest client ile de bu verileri çekebiliriz.

Dönüş JSON formatı aşağıdaki gibidir.

{
	"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"
			}
		}]
	}
}

Hadi birde bunun abap kodunu yazalım 🙂

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ç 🙂

Yukarıdaki gibi son datamızda gelmiştir iyi eğlenceler 🙂

Loading