Can any one resolve this exception in the below Thrift program? - c++

I am new to Thrift.
I am trying to create a table ("sample") in Hbase using the following Thrift program on ubuntu 10.10 and can anyone tell me whether this is correct or not.
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <poll.h>
#include <iostream>
#include <boost/lexical_cast.hpp>
#include <protocol/TBinaryProtocol.h>
#include <transport/TSocket.h>
#include <transport/TTransportUtils.h>
#include "Hbase.h"
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace apache::hadoop::hbase::thrift;
using namespace std;
int main()
{
boost::shared_ptr<TTransport> socket(new TSocket("localhost", 60010));
boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
HbaseClient client1(protocol);
try
{
transport->open();
string tableName("Sample");
vector<ColumnDescriptor> columns;
columns.push_back(ColumnDescriptor());
columns.back().name = "cf:";
columns.back().maxVersions = 10;
columns.push_back(ColumnDescriptor());
columns.back().name = "test";
try {
client1.createTable(tableName, columns);
} catch (const AlreadyExists &ae) {
std::cerr << "WARN: " << ae.message << std::endl;
}
}
catch (const TException &tx) {
std::cerr << "ERROR: " << tx.what() << std::endl;
}
return 0;
}
But i am getting the following exception at this place client1.createTable(tableName, columns);
ERROR: No more data to read.
Please help in resolving this.

Got it,
Need to start thrift server on hbase by .<hbaseinstallationpath>/bin/hbase thrift -threadpool start

Related

rapidjson + c++: "abort() has been called" error

I need to parse json in my C++ program. I decided to use RapidJson library for this purpose, but I got "abort() has been called" error. I truncated the code to this:
#include <iostream>
#include <cstdlib>
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/encodings.h"
#include "rapidjson/stringbuffer.h"
using namespace std;
using namespace rapidjson;
typedef GenericDocument<UTF16<> > WDocument;
typedef GenericValue<UTF16<> > WValue;
wchar_t request[] = L"{\"result\":\"OK\"}";
int main()
{
WDocument d;
d.Parse(request);
WValue& v = d[L"result"]; // The exception throws here
if (wcscmp(v.GetString(), L"OK"))
{
cout << "Success!" << endl;
}
else
cout << "Fail!" << endl;
system("pause");
return 0;
}
but I got the error again. Where is the mistake? Thanks in advance!
check this line:
wchar_t request[] = L"{\"result\":\"OK\"}";
there is a character before the left brace.

Sending a audio buffer using C++ Rest SDK

I recently started to use C++ Rest SDK and I'm trying to send an audio buffer to Watson Speech to Text service and the docs didn't make clear to me how to use or upload a buffer using this lib, I started using a Microsfot sample to upload files(https://msdn.microsoft.com/en-us/library/jj950081.aspx) and then a I tried to modify the code to send a buffer as the examples (https://msdn.microsoft.com/en-us/library/jj950083.aspx), but I for some how doesn't work.
This is my current code:
My current code:
#include <codecvt>
#include <cpprest/containerstream.h>
#include <cpprest/http_client.h>
#include <iostream>
#include <cpprest/producerconsumerstream.h>
#include <cpprest/rawptrstream.h>
#include <cpprest/json.h>
#include "ReqHTTP.h"
#include <sstream>
#include <fstream>
#include <Windows.h>
using namespace std;
using namespace web;
using namespace concurrency;
using namespace concurrency::streams;
using namespace web::http;
using namespace web::http::client;
using namespace utility::conversions;
#pragma comment(lib, "winmm.lib")
pplx::task<void> RequestHTTP::UploadFileToHttpServerAsync(streams::ostream outStream, LPSTR buffer)
{
container_buffer<LPSTR> outAudioBuffer(move(buffer));
return outStream.write(outAudioBuffer, outAudioBuffer.size()).then([](size_t
bytesWritten)
{
try {
http_client_config config;
credentials cred(L"c674021d-5a0f-4ec1-84a4-da6fbd50541c", L"WvYMlztHWCbC");
config.set_credentials(cred);
http_request req(methods::POST);
req.headers().add(L"Transfer-Encoding", L"chunked");
req.headers().add(L"Content-Type", L"audio/wav");
req.set_request_uri(L"speech-to-text/api/v1/recognize?continuous=true&model=pt-BR_BroadbandModel");
req.set_body(bytesWritten);
// Make HTTP request with the file stream as the body.
http_client client(L"https://stream.watsonplatform.net/", config);
//concurrency::streams::producer_consumer_buffer<uint8_t> buf;
//buf.putn_nocopy(&body[0], body.size());
return client.request(req).then([bytesWritten](pplx::task<http_response> previousTask)
{
//buffer.close;
std::wostringstream ss;
try
{
auto response = previousTask.get();
response.extract_json().then([=](json::value js) {
int i = 0;
auto campo = js.at(to_string_t("results"));
while (i < campo.size()) {
auto transc = campo.operator[](i).operator[](to_string_t("alternatives")).operator[](0);
wcout << transc.at(to_string_t("transcript")) << endl;
i++;
}
});
}
catch (const http_exception& e)
{
ss << e.what() << std::endl;
}
std::wcout << ss.str();
});
}
catch (const std::system_error& e)
{
std::wostringstream ss;
ss << e.what() << std::endl;
std::wcout << ss.str();
// Return an empty task.
return pplx::task_from_result();
}
});
}

Visual Studio 2015: Getting "non standard syntax, use '&' to create a pointer to member" error

Super confused as to what is throwing the error when I try to compile my code. I'm currently trying to test a function I wrote by printing out the values it should extract from a file.
gameboard.cpp
#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <fstream>
#include "error.h"
using namespace std;
int boardDim(ifstream & inputFile, unsigned int x, unsigned int y) {
inputFile.open; //error is thrown here
if (!(inputFile.is_open())) {
throw fileNotOpen;
}
else {
stringstream output(inputFile.getline); //error is also thrown here
if (output >> x) {
if (output >> y) {
return success;
}
return secBoardVarErr;
}
return firstBoardVarErr;
}
cout << x << endl;
cout << y << endl;
}
gameboard.h
#ifndef GAMEBOARD_H
#define GAMEBOARD_H
#include <iostream>
using namespace std;
//takes in dimensions of board from file
int boardDim(ifstream &, unsigned int, unsigned int);
#endif !GAMEBOARD_H
main function
#include "stdafx.h"
#include "functions.h"
#include "gamepieces.h"
#include "gameboard.h"
#include "error.h"
int main(int argc, char * argv[])
{
ifstream x("test.txt");
int test = 0;
cout << boardDim(x, 0, 0) << endl;
return success;
}
I'm only testing the function I declared and defined in the gameboard header and source files, so the other included files will be used in the future but have already been tested and are not throwing errors when I compile and run it.
Thank you!
inputFile.open is a function, same with inputFile.getline, so what you have here is a syntactic error. The correct syntax is:
inputFile.open()
and
inputFile.getline()

Getting started with MongoDB C++ driver in Windows

Trying to set up a simple MongoDB database connection in Windows 7, using the C++ driver. I'm using Visual C++ compiler 19 for x86, 32-bit MongoDB 3.0.6, Boost 1_59_0, Mongo legacy 1.0.5 C++ driver.
Driver compiles OK using the command
scons --cpppath=d:\boost_1_59_0 --libpath=d:\boost_1_59_0\stage\lib --msvc-host-arch=x86 install
Program is
#include <cstdlib>
#include <iostream>
using namespace std;
#include <WinSock2.h>
#include <windows.h>
#include "mongo/client/dbclient.h"
void run() {
mongo::DBClientConnection c;
c.connect("localhost");
}
int main() {
try {
run();
std::cout << "connected ok" << std::endl;
} catch( const mongo::DBException &e ) {
std::cout << "caught " << e.what() << std::endl;
}
return EXIT_SUCCESS;
}
Program compiles using
cl /EHsc /I"c:\mongo-cxx-driver-legacy-1.0.5\build\install\include" /I"d:\boost_1_59_0" /DSTATIC_LIBMONGOCLIENT mdb.cpp c:\mongo-cxx-driver-legacy-1.0.5\build\install\lib\libmongoclient-s.lib /link /LIBPATH:"D:\boost_1_59_0\stage\lib" ws2_32.lib
But when I run the program, get the error message
caught can't connect couldn't initialize connection to localhost, address is invalid
The server is running OK as I can access it through the shell, add records etc.
This is my first time programming MongoDB and I'm kind of stuck. Any suggestions?
OK, problem solved (thanks to stevepowell.ca/mongo-db-1.html). Here's the answer for anyone else who runs into this problem:
Windows needs to initialize the client before setting up the connection.
#include <cstdlib>
#include <iostream>
#include <WinSock2.h>
#include <windows.h>
#include <memory>
#include "mongo/client/dbclient.h"
using namespace mongo;
using namespace std;
void run() {
mongo::client::initialize(); // this line is new
mongo::DBClientConnection c;
c.connect("localhost");
}
int main() {
try {
run();
std::cout << "connected ok" << std::endl;
} catch( const mongo::DBException &e ) {
std::cout << "caught " << e.what() << std::endl;
}
return EXIT_SUCCESS;
}
I wish this had been in the tutorial!
Onwards and upwards.

c++ sql connection "get_driver_instance(); is undefined " error

MyCode is ;
#include<iostream>
using namespace std;
#include <string>
#include "mysql_connection.h"
#include <stdlib.h>
#include <iomanip>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
int main(){
cout<<"sadullah";
string database = "sch1667138";
string ipAdress = "144.122.71.165";
string password = "???";
string userName = "???";
sql::Driver *driver;
sql::Connection *con;
try {
driver =get_driver_instance();
con = driver->connect("tcp://" + ipAdress + ":3306", userName,
password);
con->setSchema("sch1667138");
cout << "successfully connected to db...." << endl;
} catch (sql::SQLException &e) {
cout << e.what() <<endl;
}
return 0;
}
When I run the code,I take the message "reference undefined get_driver_instance();" What should I do
I solved the problem. The reason is that I am working on Ubuntu, I should have run it from terminal, with the code ; g++ -o sado 1667138.cpp -lmysqlcppconn sadu is the name of execute file, and -lmysqlcppconn is adding the mysql library to cpp