많은 소스를 통해 확인했지만 결과는 얻을 수 없습니다. vertica에서 함수 만들기
어떻게 데이터베이스에 모든 세션의 수를 반환하는 Vertica의에서 함수를 만드는 방법?
누군가이 주제에서 가능한 아이디어 또는 가능한 예를 제공 할 수 있습니까?
많은 소스를 통해 확인했지만 결과는 얻을 수 없습니다. vertica에서 함수 만들기
어떻게 데이터베이스에 모든 세션의 수를 반환하는 Vertica의에서 함수를 만드는 방법?
누군가이 주제에서 가능한 아이디어 또는 가능한 예를 제공 할 수 있습니까?
가 C를 생성 ++ (count_it.cpp)
#include "Vertica.h"
#include <time.h>
#include <sstream>
#include <iostream>
using namespace Vertica;
using namespace std;
class Average : public AggregateFunction
{
virtual void initAggregate(ServerInterface &srvInterface, IntermediateAggs &aggs)
{
try {
VNumeric &sum = aggs.getNumericRef(0);
sum.setZero();
vint &cnt = aggs.getIntRef(1);
cnt = 0;
} catch(exception& e) {
vt_report_error(0, "Exception while initializing intermediate aggregates: [%s]", e.what());
}
}
void aggregate(ServerInterface &srvInterface, BlockReader &argReader, IntermediateAggs &aggs)
{
try {
VNumeric &sum = aggs.getNumericRef(0);
vint &cnt = aggs.getIntRef(1);
do {
const VNumeric &input = argReader.getNumericRef(0);
if (!input.isNull()) {
sum.accumulate(&input);
sum.setZero();
cnt++;
}
} while (argReader.next());
} catch(exception& e) {
vt_report_error(0, "Exception while processing aggregate: [%s]", e.what());
}
}
virtual void combine(ServerInterface &srvInterface,IntermediateAggs &aggs,MultipleIntermediateAggs &aggsOther)
{
try {
VNumeric &mySum = aggs.getNumericRef(0);
vint &myCount = aggs.getIntRef(1);
do {
const VNumeric &otherSum = aggsOther.getNumericRef(0);
const vint &otherCount = aggsOther.getIntRef(1);
mySum.accumulate(&otherSum);
mySum.setZero();
myCount += otherCount;
} while (aggsOther.next());
} catch(exception& e) {
vt_report_error(0, "Exception while combining intermediate aggregates: [%s]", e.what());
}
}
virtual void terminate(ServerInterface &srvInterface, BlockWriter &resWriter, IntermediateAggs &aggs)
{
try {
const VerticaType &numtype = aggs.getTypeMetaData().getColumnType(0);
const VNumeric &sum = aggs.getNumericRef(0);
sum.setZero();
uint64* tmp = (uint64*)malloc(numtype.getMaxSize()/sizeof(uint64));
VNumeric cnt(tmp, numtype.getNumericPrecision(), numtype.getNumericScale());
cnt.copy(aggs.getIntRef(1));
VNumeric &out = resWriter.getNumericRef();
if (cnt.isZero())
out.setZero();
else
out.add(&cnt,&sum);
} catch(exception& e) {
vt_report_error(0, "Exception while computing aggregate output: [%s]", e.what());
}
}
InlineAggregate()
};
class count_itFactory : public AggregateFunctionFactory
{
virtual void getPrototype(ServerInterface &srvfloaterface,ColumnTypes &argTypes,ColumnTypes &returnType)
{
argTypes.addNumeric();
returnType.addNumeric();
}
virtual void getReturnType(ServerInterface &srvfloaterface,const SizedColumnTypes &inputTypes,SizedColumnTypes &outputTypes)
{
int int_part = inputTypes.getColumnType(0).getNumericPrecision();
int frac_part = inputTypes.getColumnType(0).getNumericScale();
outputTypes.addNumeric(int_part+frac_part, frac_part);
}
virtual void getIntermediateTypes(ServerInterface &srvInterface,const SizedColumnTypes &inputTypes,SizedColumnTypes &intermediateTypeMetaData)
{
int int_part = inputTypes.getColumnType(0).getNumericIntegral();
int frac_part = inputTypes.getColumnType(0).getNumericFractional();
intermediateTypeMetaData.addNumeric(int_part+frac_part, frac_part);
intermediateTypeMetaData.addInt();
}
virtual AggregateFunction *createAggregateFunction(ServerInterface &srvfloaterface)
{ return vt_createFuncObject<Average>(srvfloaterface.allocator); }
};
RegisterFactory(count_itFactory);
,컴파일 C++
라이브러리
CREATE LIBRARY count_it AS '/home/dbadmin/libs/count_it.so';
생성 기능을
CREATE AGGREGATE FUNCTION count_it AS LANGUAGE 'C++' NAME 'count_itFactory' LIBRARY count_it;
0사용 함수를 생성
select count_it(client_id) from sesions;
Vertica에서 UDX를 만들 수 있지만 세션 핸들이 없으므로 API를 통해 노출 된 데이터에만 액세스 할 수 있습니다. 세션 데이터가 공개되지 않습니다.
이렇게하려면 쿼리를 실행해야합니다.
SELECT COUNT(*) FROM sessions;
이 쿼리에 대해 알고 있습니다. 그게 내가 테이블 데이터를 반환하는 함수를 시도하고 싶다. –
할 수 없습니다. 죄송합니다. Vertica에는 저장 프로 시저가 없습니다. UDX에는 쿼리 용도로 세션 핸들이 없습니다. 검색어 만 유일한 선택입니다. – woot
유사한이 게시물을 참조 :
v_monitor.sessions FROMSELECT
node_name
,user_name
,'SELECT CLOSE_SESSION(''' || session_id || ''');' AS CloseSession
,statement_start
,(GETDATE() - statement_start)::INTERVAL AS current_statement_duration
,REGEXP_REPLACE(current_statement,'[rnt]',' ') AS current_statement
,session_id
,transaction_id
,statement_id
,client_hostname
,client_os
,login_timestamp
,runtime_priority
,ssl_state
,authentication_method
,transaction_start
,GETDATE() AS Today
current_statement_duration의 DESC BY ORDER;
또한 당신은이 게시물에서 더 많은 정보를 얻을 수 있습니다 Script to List Vertica Active Sessions
이 답변에는 (1) 세션과 세션이 아닌 (2) 메모리가 할당되었지만 해제되지 않았습니다. (3) malloc이 캐스팅되었습니다. (4) malloc이 실제로 메모리를 할당하는지 제어하지 않습니다. 집계 함수는 Group by 등을 필요로한다. 이것은 코드를 작성하지 않는 방법의 완벽한 예이다. – mauro