NOTE: Your abap logic will only be executed when the CDS view is consumed by something that handles annotations i.e. exposed as an Odata service, but not in transaction SE16N or the preview in Eclipse.
There two ways actually.
First option:
@AbapCatalog.sqlViewName: 'ZV_TEST_ABAP'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #CHECK
@EndUserText.label: 'ABAP code in cds'
@OData.publish: true
define view YCDS_WITH_ABAP as select from sflight
{
//sflight
key carrid,
key connid,
key fldate,
seatsocc_f,
@ObjectModel.readOnly: true
@ObjectModel.virtualElement: true
@ObjectModel.virtualElementCalculatedBy: 'ABAP:YCL_CDS_FUNCTION' //ycl_cds_function
cast( '' as abap.char(255)) as text
}where connid = '0017'
Your abap class:
CLASS ycl_cds_function DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
INTERFACES: if_sadl_exit_calc_element_read.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS ycl_cds_function IMPLEMENTATION.
METHOD if_sadl_exit_calc_element_read~calculate.
DATA:
lt_calculated_data TYPE STANDARD TABLE OF ZV_TEST_ABAP .
" it_original_data -> data that comes from cds
" lt_calculated_data -> data that you will manipulate
MOVE-CORRESPONDING it_original_data TO lt_calculated_data.
" do your extra logic and append/update your cds view data
LOOP AT lt_calculated_data ASSIGNING FIELD-SYMBOL(<fs_data>).
<fs_data>-text = 'hello from abap!'.
ENDLOOP.
MOVE-CORRESPONDING lt_calculated_data TO ct_calculated_data.
ENDMETHOD.
METHOD if_sadl_exit_calc_element_read~get_calculation_info.
ENDMETHOD.
ENDCLASS.
View in eclipse:
View in odata:
...,{
"__metadata": {
"id": "lalala')",
"uri": "blablabla')",
"type": "YCDS_WITH_ABAP_CDS.YCDS_WITH_ABAPType"
},
"carrid": "AA",
"connid": "0017",
"fldate": "/Date(1535068800000)/",
"seatsocc_f": 20,
"text": "hello from abap!"
},...
Second option:
The second way is to change your service's DPC class with your custom class, which inherits this standard class and do your custom logic in those redefined methods.
Personally, I would go with the second option. I haven't tried it but I have a feeling that performance would be better. :)

For more info please check: https://blogs.sap.com/2020/05/11/abap-code-exits-in-cds-views/