Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: udf for graphd added #22

Merged
merged 1 commit into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion docs/ngdi_API_Gateway.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@ pdm run ngdi-api

See https://github.com/wey-gu/nebula/tree/ngdi_udf

- Build binary `ngdi.so` file
### Build binary `ngdi.so` file

Clone the `nebula` repo and checkout the hash of your existing nebulagraph cluster(check with `SHOW HOSTS GRAPH`)

```bash
git clone https://github.com/vesoft-inc/nebula && cd nebula
```

Prepare nebula_dev docker container for building UDF.

```bash
export TAG=ubuntu2004
Expand All @@ -44,6 +52,9 @@ docker run -ti \
mkdir build && cd build
cmake -DCMAKE_CXX_COMPILER=$TOOLSET_CLANG_DIR/bin/g++ -DCMAKE_C_COMPILER=$TOOLSET_CLANG_DIR/bin/gcc -DENABLE_WERROR=OFF -DCMAKE_BUILD_TYPE=Release -DENABLE_TESTING=OFF ..

Build the `ngdi.so` file.

```bash
cd ../udf
make UDF=ngdi
```
Expand Down
33 changes: 33 additions & 0 deletions udf/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright (c) 2023 vesoft inc. All rights reserved.
#
# This source code is licensed under Apache 2.0 License.
#

ifneq ($(wildcard ../build/third-party/install),)
3PP_PATH := ../build/third-party/install
else ifneq ($(wildcard /opt/vesoft/third-party/3.3),)
3PP_PATH := /opt/vesoft/third-party/3.3
else ifneq ($(wildcard /opt/vesoft/third-party/3.0),)
3PP_PATH := /opt/vesoft/third-party/3.0
else
$(error "Cannot find the third-party installation directory")
endif

CXX := g++
CXX_FLAGS := -std=c++17 -c -I ../src/ -I $(3PP_PATH)/include/ -fPIC -static-libstdc++
LIBS := -L$(3PP_PATH)/lib -static-libstdc++ -lglog -lgflags -Wl,-rpath=$(3PP_PATH)/lib -lcurl
UDF ?= ngdi

all: $(UDF).cpp
$(CXX) $(CXX_FLAGS) $(UDF).cpp -o $(UDF).o
$(CXX) -shared $(UDF).o $(LIBS) -o $(UDF).so


clean:
rm ./*.o
rm ./*.so

.PHONY: help
help:
@echo "Usage: make UDF=<udf_name>"
@echo "Example: make UDF=ngdi"
3 changes: 3 additions & 0 deletions udf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This is the User Defined Function (UDF) to enable calling ngdi from nGQL.

See [docs](https://github.com/wey-gu/nebulagraph-di/blob/main/docs/ngdi_API_Gateway.md)
60 changes: 60 additions & 0 deletions udf/http_client.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* Copyright (c) 2023 vesoft inc. All rights reserved.
*
* This source code is licensed under Apache 2.0 License.
*/

#ifndef UDF_HTTP_CLIENT
#define UDF_HTTP_CLIENT

#include <curl/curl.h>

#include <cctype>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>

#include "../src/common/datatypes/List.h"
#include "../src/common/datatypes/Map.h"

size_t write_callback(char* ptr, size_t size, size_t nmemb, void* userdata) {
std::string* response = static_cast<std::string*>(userdata);
response->append(ptr, size * nmemb);
return size * nmemb;
}

std::string do_post(const std::string& url,
const std::vector<std::string>& headers,
const std::string& body) {
std::cout << "url=" << url << std::endl;
std::cout << "body=" << body << std::endl;
CURL* curl = curl_easy_init(); // 初始化curl
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); // 设置请求URL
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); // 支持重定向
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
write_callback); // 设置回调函数
std::string response;
curl_easy_setopt(curl, CURLOPT_WRITEDATA,
&response); // 设置回调函数参数
curl_easy_setopt(curl, CURLOPT_POST, 1L); // 设置为POST请求
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body.c_str()); // 设置请求体
struct curl_slist* header_list = nullptr;
for (auto& header : headers) {
header_list = curl_slist_append(header_list, header.c_str());
}
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header_list);
CURLcode result_code = curl_easy_perform(curl); // 执行请求
std::cout << "result code=" << result_code << std::endl;
if (result_code != CURLE_OK) {
return curl_easy_strerror(result_code);
} else {
std::cout << "response=" << response << std::endl;
return response;
}
curl_easy_cleanup(curl); // 释放curl资源
}
return "ERROR: curl init fail.";
}

#endif
Loading