1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

C++ で Redis のデータを作成 (Create)

Last updated at Posted at 2018-05-31
redis_create.cpp
// --------------------------------------------------------------------
/*
	redis_create.cpp

						May/31/2018

*/
// --------------------------------------------------------------------
#include <iostream>
#include <map>

#include <sys/socket.h>
#include <arpa/inet.h>

#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string.hpp>

#include "json11.hpp"
// -----------------------------------------------------------------------
using namespace std;
using namespace boost;
using namespace json11;

typedef map<string,string> Unit;

#define PORT 6379
// -----------------------------------------------------------------
// [6-8-6]:
void socket_write_proc (int sock,const char str_command[])
{
	int	len;
	int	slen;

	const char *pm = str_command;

	do
		{
		len = strlen (pm);
	try
		{
		slen = send (sock,pm,len,0);
		}
	catch (const char* ss)
		{
		cerr << "Exceptoin raised: " << ss << "\n";
		}

		pm += slen;
		}
		while ((0 <= slen) && (slen < len));
}

// -----------------------------------------------------------------
// [6-8]:
void redis_socket_write_proc (int sock,string key,string json_str)
{
	const char *key_in = key.c_str ();

	int ll_key = strlen (key.c_str ());
	int llx = strlen (json_str.c_str ());

//	cerr << "llx = " << llx <<  endl;

	char *com_aa = (char*)"*3\r\n";
	socket_write_proc (sock,com_aa);

	char *com_bb = (char*)"$3\r\n";
	socket_write_proc (sock,com_bb);

	char *com_cc = (char*)"set\r\n";
	socket_write_proc (sock,com_cc);

	char str_command_aa[255];
	sprintf (str_command_aa,"$%d\r\n",ll_key);
	socket_write_proc (sock,str_command_aa);

	char str_command_bb[255];
	sprintf (str_command_bb,"%s\r\n",key.c_str ());
	socket_write_proc (sock,str_command_bb);

	char str_command_cc[255];
	sprintf (str_command_cc,"$%d\r\n",llx);
	socket_write_proc (sock,str_command_cc);

	char str_command_dd[255];
	sprintf (str_command_dd,"%s\r\n",json_str.c_str ());
	socket_write_proc (sock,str_command_dd);

	ssize_t bytes_recieved;
	char incoming_data_buffer[1000];

	bytes_recieved = recv (sock,incoming_data_buffer,1000,0);

	if (bytes_recieved == 0) cout << "host shut down." << endl;
	if (bytes_recieved == -1) cout << "recieve error!" << endl;

}

// -----------------------------------------------------------------
// [6-4]:
string unit_to_json_proc (Unit unit_aa)
{
	string str_out = "";

	Json::object my_json;

	int population = atoi (unit_aa["population"].c_str ());

	my_json = Json::object {{ "name", unit_aa["name"]},{ "population", population},{ "date_mod", unit_aa["date_mod"] }};

	Json data = my_json;

	string json_str = data.dump();

	return	json_str;
}

// -----------------------------------------------------------------
// [6]:
int dict_to_redis_proc (int sock,map <string,Unit> dict_aa)
{
	cerr << "*** dict_to_redis_proc *** start ***\n";

	map <string,Unit>:: iterator itr = dict_aa.begin ();

	while (itr != dict_aa.end ())
		{
		string key_in = (*itr).first;
		Unit unit_aa = (*itr).second;

		string json_str = unit_to_json_proc (unit_aa);
		cerr << "key_in = " << key_in << '\n';
		cerr << json_str << '\n';

		redis_socket_write_proc (sock,key_in,json_str);
		itr++;
		}

	cerr << "*** dict_to_redis_proc *** end ***\n";

	return	0;
}

// -----------------------------------------------------------------
// [2-4]:
map <string,Unit> dict_append_proc
	(map <string,Unit> dict_aa,string id,string name,
	int population,string date_mod)
{
	Unit unit_aa;

	unit_aa["name"] = name;
	unit_aa["population"] = lexical_cast<string>(population);

	unit_aa["date_mod"] = date_mod;

	string key = lexical_cast<string>(id);

	dict_aa[key] = unit_aa;

	return dict_aa;
}

// -----------------------------------------------------------------
// [2]:
static map <string,Unit >  data_prepare_proc ()
{
	map <string,Unit> dict_aa;

	dict_aa = dict_append_proc (dict_aa,"t1851","福井",76129,"1922-8-27");

	dict_aa = dict_append_proc (dict_aa,"t1852","敦賀",98637,"1922-2-2");
	dict_aa = dict_append_proc (dict_aa,"t1853","小浜",42915,"1922-7-21");
	dict_aa = dict_append_proc (dict_aa,"t1854","大野",51238,"1922-5-25");

	dict_aa = dict_append_proc (dict_aa,"t1855","勝山",83471,"1922-1-5");
	dict_aa = dict_append_proc (dict_aa,"t1856","鯖江",71249,"1922-9-22");
	dict_aa = dict_append_proc (dict_aa,"t1857","あわら",46185,"1922-7-17");
	dict_aa = dict_append_proc (dict_aa,"t1858","越前",61873,"1922-3-4");
	dict_aa = dict_append_proc (dict_aa,"t1859","坂井",74923,"1922-7-9");

	return	dict_aa;
}

// -----------------------------------------------------------------
int main (int argc, char *argv[])
{
	cerr << "*** 開始 ***\n";

	map <string,Unit> dict_aa = data_prepare_proc ();

	sockaddr_in address;
	sockaddr_in serv_addr;

	int sock = 0;

	char buffer[1024] = {0};
	if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
		{
		printf("\n Socket creation error \n");
		return -1;
		}

	memset(&serv_addr, '0', sizeof(serv_addr));
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_port = htons(PORT);

	if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0) 
		{
		printf("\nInvalid address/ Address not supported \n");
		return -1;
		}

	printf("sock = %d\n",sock);
	printf("sizeof(serv_addr) = %ld\n",sizeof(serv_addr));

	if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
		{
		printf("\nConnection Failed \n");
		return -1;
		}

	dict_to_redis_proc (sock,dict_aa);

	cerr << "*** 終了 ***\n";
	return 0;
}

// -----------------------------------------------------------------
Makefile
redis_create: redis_create.cpp
	clang++ -o redis_create redis_create.cpp -ljson11
clean:
	rm -f redis_create

実行コマンド

./redis_create

次のバージョンで確認しました。

$ clang++ --version
clang version 11.1.0
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?