Session 1

(Created blank page)
 
 
Line 1: Line 1:
 +
= Hands-On Session 1 =
  
 +
== PostgreSQL ==
 +
 +
<code>vim /etc/postgresql/9.5/main/pg_hba.conf</code>
 +
 +
Set all to <code>trust</code>, then add this line <code>host all all 0.0.0.0/0 trust</code>
 +
 +
<code>service postgresql restart</code>
 +
 +
Should not ask for password, and you can quit with <code>ctrl-d</code>
 +
 +
<pre>sudo -u postgres /usr/bin/psql -c &quot;CREATE ROLE gridka01 PASSWORD 'asdf1234' SUPERUSER CREATEDB CREATEROLE INHERIT LOGIN;&quot;
 +
sudo -u postgres /usr/bin/psql -c &quot;CREATE DATABASE gridka_db OWNER gridka01 ENCODING 'UTF8'&quot;
 +
sudo -u postgres /usr/bin/psql -d gridka_db -c &quot;CREATE SCHEMA gridka_schema AUTHORIZATION gridka01&quot;
 +
sudo -u postgres /usr/bin/psql -d gridka_db -c &quot;GRANT ALL ON SCHEMA gridka_schema TO gridka01&quot;
 +
sudo -u postgres /usr/bin/psql -d gridka_db -c &quot;GRANT ALL ON DATABASE gridka_db TO gridka01&quot;</pre>
 +
<code>ls -la /var/lib/postgresql/9.5/</code>
 +
 +
<code>psql -U gridka01 gridka_db</code>
 +
 +
<code>vim /etc/postgresql/9.5/main/pg_hba.conf</code>
 +
 +
Set all to <code>md5</code>
 +
 +
<code>psql -U gridka01 gridka_db</code>
 +
 +
Should now ask for password.
 +
 +
<code>psql -U gridka01 gridka_db</code>
 +
 +
<code>\l</code> .. list all databases
 +
 +
<code>\d</code> .. list all relations
 +
 +
<pre>CREATE TABLE test_table ( first_name VARCHAR, last_name VARCHAR );
 +
 +
\d
 +
 +
SELECT * FROM test_table;
 +
 +
INSERT INTO test_table VALUES ('Vincent', 'Vega');
 +
 +
SELECT * FROM test_table;
 +
 +
INSERT INTO test_table (last_name, first_name) VALUES ('Jules', 'Winnfield');
 +
 +
SELECT * FROM test_table;
 +
 +
DELETE FROM test_table;
 +
 +
SELECT * FROM test_table;
 +
 +
DROP TABLE test_table;
 +
 +
SELECT * FROM test_table;</pre>
 +
<pre>#include &lt;stdio.h&gt;
 +
#include &lt;stdlib.h&gt;
 +
 +
#include &lt;libpq-fe.h&gt;
 +
 +
int main()
 +
{
 +
        PGconn *conn;
 +
        conn = PQconnectdb(&quot;dbname='gridka_db' user='gridka01' host='localhost' password='asdf1234'&quot;);
 +
        if (PQstatus(conn) != CONNECTION_OK) {
 +
                printf(&quot;%s\n&quot;, PQerrorMessage(conn));
 +
                exit(1);
 +
        }
 +
 +
        PGresult *res;
 +
        res = PQexec(conn, &quot;CREATE TABLE test_table ( first_name VARCHAR, last_name VARCHAR )&quot;);
 +
        if (!res)
 +
                printf(&quot;%s\n&quot;, PQerrorMessage(conn));
 +
        PQclear(res);
 +
 +
        res = PQexec(conn, &quot;INSERT INTO test_table VALUES ( 'Vincent', 'Vega' )&quot;);
 +
        if (!res)
 +
                printf(&quot;%s\n&quot;, PQerrorMessage(conn));
 +
        PQclear(res);
 +
 +
        res = PQexec(conn, &quot;INSERT INTO test_table (last_name, first_name) VALUES ( 'Jules', 'Winnfield' )&quot;);
 +
        if (!res)
 +
                printf(&quot;%s\n&quot;, PQerrorMessage(conn));
 +
        PQclear(res);
 +
 +
        res = PQexec(conn, &quot;SELECT * FROM test_table&quot;);
 +
        if (PQresultStatus(res) == PGRES_TUPLES_OK)
 +
                for(int i = 0; i &lt; PQntuples(res); ++i)
 +
                        printf(&quot;%s %s\n&quot;, PQgetvalue(res, i, 0), PQgetvalue(res, i, 1));
 +
        else
 +
                printf(&quot;%s\n&quot;, PQerrorMessage(conn));
 +
        PQclear(res);
 +
 +
        res = PQexec(conn, &quot;DELETE FROM test_table&quot;);
 +
        if (!res)
 +
                printf(&quot;%s\n&quot;, PQerrorMessage(conn));
 +
        PQclear(res);
 +
 +
        PQfinish(conn);
 +
}</pre>
 +
<code>gcc -g --std=c11 -Wall -Wextra -I$(pg_config --includedir) test_pg.c -o test_pg -lpq</code>
 +
 +
<pre>import psycopg2 as pg
 +
conn = pg.connect(&quot;dbname='gridka_db' user='gridka01' host='localhost' password='asdf1234'&quot;)
 +
 +
cur = conn.cursor()
 +
 +
cur.execute(&quot;CREATE TABLE test_table ( first_name VARCHAR, last_name VARCHAR )&quot;)
 +
 +
cur.execute(&quot;INSERT INTO test_table VALUES ( 'Vincent', 'Vega' )&quot;)
 +
conn.commit()
 +
 +
cur.execute(&quot;SELECT * FROM test_table&quot;)
 +
cur.fetchall()
 +
 +
cur.execute(&quot;INSERT INTO test_table (last_name, first_name) VALUES ( 'Jules', 'Winnfield' )&quot;)
 +
conn.commit()
 +
 +
cur.execute(&quot;SELECT * FROM test_table&quot;)
 +
print cur.fetchall()
 +
 +
cur.execute(&quot;DROP TABLE test_table&quot;)
 +
conn.commit()</pre>
 +
== MonetDB ==
 +
 +
<pre>monetdbd create /tmp/gridka_schema
 +
monetdbd start /tmp/gridka_schema
 +
monetdb create gridka_db
 +
monetdb release gridka_db</pre>
 +
<code>mclient -u monetdb -d gridka_db</code> (default password: <code>monetdb</code>)
 +
 +
<pre>CREATE USER &quot;gridka01&quot; WITH PASSWORD 'asdf1234' NAME 'gridka01' SCHEMA &quot;sys&quot;;
 +
CREATE SCHEMA &quot;gridka01&quot; AUTHORIZATION &quot;gridka01&quot;;
 +
ALTER USER &quot;gridka01&quot; SET SCHEMA &quot;gridka01&quot;;</pre>
 +
<pre>CREATE TABLE test_table ( first_name VARCHAR(64), last_name VARCHAR(64) );
 +
INSERT INTO test_table VALUES ('Vincent', 'Vega');
 +
INSERT INTO test_table (last_name, first_name) VALUES ('Jules', 'Winnfield');
 +
SELECT * FROM test_table;
 +
DELETE FROM test_table;
 +
DROP test_table;</pre>
 +
<pre>#include &lt;stdio.h&gt;
 +
#include &lt;stdlib.h&gt;
 +
 +
#include &lt;mapi.h&gt;
 +
 +
int main()
 +
{
 +
        Mapi db;
 +
        db = mapi_connect(&quot;localhost&quot;, 50000,
 +
                          &quot;gridka01&quot;, &quot;asdf1234&quot;,
 +
                          &quot;sql&quot;, &quot;gridka_db&quot;);
 +
        if (mapi_error(db))
 +
                mapi_explain(db, stderr);
 +
 +
        MapiHdl res;
 +
        res = mapi_query(db, &quot;CREATE TABLE test_table ( first_name VARCHAR(64), last_name VARCHAR(64) )&quot;);
 +
        if (mapi_error(db))
 +
                mapi_explain(db, stderr);
 +
        mapi_close_handle(res);
 +
 +
        res = mapi_query(db, &quot;INSERT INTO test_table VALUES ( 'Vincent', 'Vega' )&quot;);
 +
        if (mapi_error(db))
 +
                mapi_explain(db, stderr);
 +
        mapi_close_handle(res);
 +
 +
        res = mapi_query(db, &quot;INSERT INTO test_table (last_name, first_name) VALUES ( 'Jules', 'Winnfield' )&quot;);
 +
        if (mapi_error(db))
 +
                mapi_explain(db, stderr);
 +
        mapi_close_handle(res);
 +
 +
        res = mapi_query(db, &quot;SELECT * FROM test_table&quot;);
 +
        if (mapi_error(db))
 +
                mapi_explain(db, stderr);
 +
        while(mapi_fetch_row(res)) {
 +
                printf(&quot;%s %s\n&quot;, mapi_fetch_field(res, 0), mapi_fetch_field(res, 1));
 +
        }
 +
        mapi_close_handle(res);
 +
 +
        res = mapi_query(db, &quot;DELETE FROM test_table&quot;);
 +
        if (mapi_error(db))
 +
                mapi_explain(db, stderr);
 +
        mapi_close_handle(res);
 +
 +
        mapi_destroy(db);
 +
}</pre>
 +
<code>gcc -g -std=c11 -Wall -Wextra $(pkg-config --cflags --libs monetdb-mapi) monetdb_test.c -o monetdb_test</code>
 +
 +
<pre>import monetdb.sql as mo
 +
conn = mo.connect(database='gridka_db', username='gridka01', hostname='localhost', password='asdf1234')
 +
 +
cur = conn.cursor()
 +
 +
cur.execute(&quot;CREATE TABLE test_table ( first_name VARCHAR(64), last_name VARCHAR(64) )&quot;)
 +
 +
cur.execute(&quot;INSERT INTO test_table VALUES ( 'Vincent', 'Vega' )&quot;)
 +
conn.commit()
 +
 +
cur.execute(&quot;SELECT * FROM test_table&quot;)
 +
cur.fetchall()
 +
 +
cur.execute(&quot;INSERT INTO test_table (last_name, first_name) VALUES ( 'Jules', 'Winnfield' )&quot;)
 +
conn.commit()
 +
 +
cur.execute(&quot;SELECT * FROM test_table&quot;)
 +
cur.fetchall()
 +
 +
cur.execute(&quot;DROP TABLE test_table&quot;)
 +
conn.commit()</pre>
 +
== LevelDB ==
 +
 +
<pre>mkdir gridka_db</pre>
 +
<pre>#include &lt;iostream&gt;
 +
#include &lt;string&gt;
 +
 +
#include &lt;leveldb/db.h&gt;
 +
 +
int main()
 +
{
 +
        leveldb::DB* db;
 +
        leveldb::Options opts;
 +
        leveldb::ReadOptions ropts;
 +
        leveldb::WriteOptions wopts;
 +
        leveldb::Status status;
 +
        leveldb::Iterator* it;
 +
 +
        opts.create_if_missing = true;
 +
 +
        status = leveldb::DB::Open(opts, &quot;gridka_db/test_table&quot;, &amp;db);
 +
 +
        status = db-&gt;Put(wopts, &quot;0&quot;, &quot;{\&quot;first_name\&quot;: \&quot;Vincent\&quot;, \&quot;last_name\&quot;: \&quot;Vega\&quot;}&quot;);
 +
        status = db-&gt;Put(wopts, &quot;1&quot;, &quot;{\&quot;first_name\&quot;: \&quot;Jules\&quot;, \&quot;last_name\&quot;: \&quot;Winnfield\&quot;}&quot;);
 +
 +
        std::string value;
 +
        db-&gt;Get(ropts, std::string(&quot;0&quot;), &amp;value);
 +
        std::cout &lt;&lt; value &lt;&lt; '\n';
 +
 +
        it = db-&gt;NewIterator(ropts);
 +
        for(it-&gt;SeekToFirst(); it-&gt;Valid(); it-&gt;Next())
 +
                std::cout &lt;&lt; it-&gt;key().ToString() &lt;&lt; '\t' &lt;&lt; it-&gt;value().ToString() &lt;&lt; '\n';
 +
        delete it;
 +
 +
        status = db-&gt;Delete(wopts, &quot;0&quot;);
 +
        status = db-&gt;Delete(wopts, &quot;1&quot;);
 +
 +
        delete db;
 +
}</pre>
 +
<code>g++ -g -std=c++14 -Wall -Wextra leveldb_test.cpp -o leveldb_test -lleveldb</code>
 +
 +
<pre>import json
 +
import leveldb
 +
db = leveldb.LevelDB('gridka_db/test_table')
 +
db.Put('0', json.dumps({'first_name': 'Vincent', 'last_name': 'Vega'}))
 +
db.Put('1', json.dumps({'first_name': 'Jules', 'last_name': 'Winnfield'}))
 +
json.loads(db.Get('0'))
 +
it = db.RangeIter()
 +
print [(x[0],json.loads(x[1])) for x in it]
 +
db.Delete('0')
 +
db.Delete('1')</pre>
 +
== Redis ==
 +
 +
<code>vim /etc/redis/redis.conf</code>
 +
 +
<pre>requirepass asdf1234</pre>
 +
<code>service redis restart</code>
 +
 +
<pre class="redis-cli```"></pre>
 +
auth asdf1234
 +
 +
set test_table:0 '{&quot;first_name&quot;: &quot;Vincent&quot;, &quot;last_name&quot;: &quot;Vega&quot;}' set test_table:1 '{&quot;first_name&quot;: &quot;Jules&quot;, &quot;last_name&quot;: &quot;Winnfield&quot;}'
 +
 +
get test_table:0
 +
 +
hset mtest_table:0 first_name Vincent hset mtest_table:0 last_name Vega hset mtest_table:1 first_name Jules hset mtest_table:1 last_name Winnfield
 +
 +
hget mtest_table:0 first_name hget mtest_table:1 last_name
 +
 +
del test_table:0 del test_table:1 del mtest_table:0 del mtest_table:1
 +
 +
<pre></pre>
 +
= include &lt;stdio.h&gt; =
 +
 +
= include &lt;stdlib.h&gt; =
 +
 +
= include &lt;hiredis.h&gt; =
 +
 +
int main() { redisContext ''ctx; redisReply ''r;
 +
 +
<pre>    ctx = redisConnect(&quot;localhost&quot;, 6379);
 +
    if (ctx-&gt;err)
 +
            printf(&quot;error: %s\n&quot;, ctx-&gt;errstr);
 +
 +
    r = redisCommand(ctx, &quot;auth asdf1234&quot;);
 +
    if (r-&gt;type == REDIS_REPLY_ERROR)
 +
            printf(&quot;error: %s\n&quot;, r-&gt;str);
 +
    freeReplyObject(r);
 +
 +
    r = redisCommand(ctx, &quot;hset mtest_table:0 first_name Vincent&quot;);
 +
    if (r-&gt;type == REDIS_REPLY_ERROR)
 +
            printf(&quot;error: %s\n&quot;, r-&gt;str);
 +
    freeReplyObject(r);
 +
 +
    r = redisCommand(ctx, &quot;hset mtest_table:0 last_name Vega&quot;);
 +
    if (r-&gt;type == REDIS_REPLY_ERROR)
 +
            printf(&quot;error: %s\n&quot;, r-&gt;str);
 +
    freeReplyObject(r);
 +
 +
    r = redisCommand(ctx, &quot;hset mtest_table:1 first_name Jules&quot;);
 +
    if (r-&gt;type == REDIS_REPLY_ERROR)
 +
            printf(&quot;error: %s\n&quot;, r-&gt;str);
 +
    freeReplyObject(r);
 +
 +
    r = redisCommand(ctx, &quot;hset mtest_table:1 last_name Winnfield&quot;);
 +
    if (r-&gt;type == REDIS_REPLY_ERROR)
 +
            printf(&quot;error: %s\n&quot;, r-&gt;str);
 +
    freeReplyObject(r);
 +
 +
    r = redisCommand(ctx, &quot;hget mtest_table:0 first_name&quot;);
 +
    if (r-&gt;type == REDIS_REPLY_ERROR)
 +
            printf(&quot;error: %s\n&quot;, r-&gt;str);
 +
    else
 +
            printf(&quot;%s\n&quot;, r-&gt;str);
 +
    freeReplyObject(r);
 +
 +
    r = redisCommand(ctx, &quot;hget mtest_table:1 last_name&quot;);
 +
    if (r-&gt;type == REDIS_REPLY_ERROR)
 +
            printf(&quot;error: %s\n&quot;, r-&gt;str);
 +
    else
 +
            printf(&quot;%s\n&quot;, r-&gt;str);
 +
    freeReplyObject(r);
 +
 +
    r = redisCommand(ctx, &quot;del mtest_table:0&quot;);
 +
    if (r-&gt;type == REDIS_REPLY_ERROR)
 +
            printf(&quot;error: %s\n&quot;, r-&gt;str);
 +
    freeReplyObject(r);
 +
 +
    r = redisCommand(ctx, &quot;del mtest_table:1&quot;);
 +
    if (r-&gt;type == REDIS_REPLY_ERROR)
 +
            printf(&quot;error: %s\n&quot;, r-&gt;str);
 +
    freeReplyObject(r);
 +
 +
    redisFree(ctx);</pre>
 +
} ```
 +
 +
<code>gcc -g -std=c11 -Wall -Wextra redis_test.c -o redis_test $(pkg-config --cflags --libs hiredis)</code>
 +
 +
<pre>import redis
 +
db = redis.StrictRedis(password='asdf1234')
 +
 +
db.set('test_table:0', '{&quot;first_name&quot;: &quot;Vincent&quot;, &quot;last_name&quot;: &quot;Vega&quot;}')
 +
db.set('test_table:1', '{&quot;first_name&quot;: &quot;Jules&quot;, &quot;last_name&quot;: &quot;Winnfield&quot;}')
 +
 +
db.hset('mtest_table:0', 'first_name', 'Vincent')
 +
db.hset('mtest_table:0', 'last_name', 'Vega')
 +
db.hset('mtest_table:1', 'first_name', 'Jules')
 +
db.hset('mtest_table:1', 'last_name', 'Winnfield')
 +
 +
db.delete('test_table:0')
 +
db.delete('test_table:1')
 +
db.delete('mtest_table:0')
 +
db.delete('mtest_table:1')</pre>
 +
== MongoDB ==
 +
 +
<pre>semanage port -a -t mongod_port_t -p tcp 27017
 +
systemclt restart mongod
 +
chkconfig mongod on</pre>
 +
<pre class="mongo```"></pre>
 +
show dbs use admin db.createUser({&quot;user&quot;: &quot;admin&quot;, &quot;pwd&quot;: &quot;admin&quot;, &quot;roles&quot;: [&quot;userAdminAnyDatabase&quot;]}) use gridka_db show collections db.createUser({&quot;user&quot;: &quot;gridka01&quot;, &quot;pwd&quot;: &quot;asdf1234&quot;, &quot;roles&quot;: [&quot;readWrite&quot;]}) ```
 +
 +
<code>vim /etc/mongod.conf</code>
 +
 +
<pre>auth=true</pre>
 +
<code>service mongod restart</code>
 +
 +
<code>mongo gridka_db</code>
 +
 +
<pre>db.test_table.insert({&quot;first_name&quot;: &quot;Vincent&quot;, &quot;last_name&quot;: &quot;Vega&quot;})
 +
mongo -u gridka01 -p asdf1234 gridka_db
 +
db.test_table.insert({&quot;first_name&quot;: &quot;Vincent&quot;, &quot;last_name&quot;: &quot;Vega&quot;})
 +
db.test_table.insert({&quot;first_name&quot;: &quot;Jules&quot;, &quot;last_name&quot;: &quot;Winnfield&quot;})
 +
db.test_table.find()
 +
db.test_table.insert({&quot;you_can_put&quot;: &quot;whatever you want&quot;})
 +
db.test_table.find()
 +
db.test_table.drop()</pre>
 +
<pre>#include &lt;stdio.h&gt;
 +
#include &lt;stdlib.h&gt;
 +
 +
#include &lt;bson.h&gt;
 +
#include &lt;mongoc.h&gt;
 +
 +
int main ()
 +
{
 +
        mongoc_client_t *client;
 +
        mongoc_collection_t *collection;
 +
        bson_error_t error;
 +
        bson_t *doc;
 +
        bson_t *query;
 +
        mongoc_cursor_t *cursor;
 +
        char *str;
 +
 +
        mongoc_init();
 +
 +
        client = mongoc_client_new(&quot;mongodb://gridka01:asdf1234@localhost:27017/?authSource=gridka_db&quot;);
 +
        collection = mongoc_client_get_collection (client,
 +
                                                  &quot;gridka_db&quot;,
 +
                                                  &quot;test_table&quot;);
 +
 +
        doc = BCON_NEW(&quot;first_name&quot;,BCON_UTF8(&quot;Vincent&quot;),
 +
                      &quot;last_name&quot;, BCON_UTF8(&quot;Vega&quot;));
 +
        if(!mongoc_collection_insert(collection, MONGOC_INSERT_NONE,
 +
                                    doc, NULL, &amp;error))
 +
                printf(&quot;error: %s\n&quot;, error.message);
 +
        bson_destroy(doc);
 +
        doc = BCON_NEW(&quot;first_name&quot;, BCON_UTF8(&quot;Jules&quot;),
 +
                      &quot;last_name&quot;, BCON_UTF8(&quot;Winnfield&quot;));
 +
        if(!mongoc_collection_insert(collection, MONGOC_INSERT_NONE,
 +
                                    doc, NULL, &amp;error))
 +
                printf(&quot;error: %s\n&quot;, error.message);
 +
        bson_destroy(doc);
 +
 +
        query = bson_new();
 +
        cursor = mongoc_collection_find(collection, MONGOC_QUERY_NONE,
 +
                                        0, 0, 0, query, NULL, NULL);
 +
        while(mongoc_cursor_next(cursor, &amp;doc)) {
 +
                str = bson_as_json(doc, NULL);
 +
                printf(&quot;%s\n&quot;, str);
 +
                bson_free(str);
 +
        }
 +
        bson_destroy(query);
 +
        mongoc_cursor_destroy(cursor);
 +
 +
        if(!mongoc_collection_drop(collection, &amp;error))
 +
                printf(&quot;error: %s\n&quot;, error.message);
 +
 +
        mongoc_collection_destroy(collection);
 +
        mongoc_client_destroy(client);
 +
}</pre>
 +
<code>gcc -g -std=c11 -Wall -Wextra -pedantic $(pkg-config --cflags --libs libmongoc-1.0) mongo_test.c -o mongo_test</code>
 +
 +
<pre>from pymongo import MongoClient
 +
client = MongoClient('mongodb://gridka01:asdf1234@localhost:27017/?authSource=gridka_db')
 +
db = client.gridka_db
 +
db.test_table.insert_one({'first_name': 'Vincent', 'last_name': 'Vega'})
 +
db.test_table.insert_one({'first_name': 'Jules', 'last_name': 'Winnfield'})
 +
[x for x in db.test_table.find()]
 +
db.test_table.insert({'first_name': 'Marcellus', 'occupation': 'businessman'})
 +
[x for x in db.test_table.find()]
 +
db.test_table.drop()</pre>
 +
== ElasticSearch ==
 +
 +
<code>curl localhost:9200</code>
 +
 +
<pre>import json, pprint
 +
import elasticsearch
 +
es = elasticsearch.Elasticsearch()
 +
 +
es.index(index='gridka_db', doc_type='person', id=0, body=json.dumps({'name': 'Vincent Vega', 'occupation': 'professional hitman'}))
 +
pprint.pprint(es.search(index='gridka_db'))
 +
 +
es.index(index='gridka_db', doc_type='person', id=0, body=json.dumps({'age': 42}))
 +
pprint.pprint(es.search(index='gridka_db'))
 +
 +
es.index(index='gridka_db', doc_type='person', id=0, body=json.dumps({'name': 'Vincent Vega', 'occupation': 'professional hitman', 'age': 42}))
 +
pprint.pprint(es.search(index='gridka_db'))
 +
 +
es.index(index='gridka_db', doc_type='person', id=1, body=json.dumps({'name': 'Jules Winnfield', 'occupation': 'professional hitman', 'hidden_occupation': 'Director of SHIELD', 'age': 57}))
 +
pprint.pprint(es.search(index='gridka_db'))
 +
 +
pprint.pprint(es.search(index='gridka_db', q='professional'))
 +
 +
pprint.pprint(es.search(index='gridka_db', q='occupation:professional'))
 +
 +
pprint.pprint(es.search(index='gridka_db', q='age:&lt;50'))
 +
 +
es.index(index='gridka_db', doc_type='person', body=json.dumps({'name': 'Marcellus Wallace', 'occupation': 'businessman', 'age': 66}))
 +
 +
pprint.pprint(es.search(index='gridka_db', q='NOT occupation:hitman'))
 +
 +
pprint.pprint(es.search(index='gridka_db', q='NOT occupation:hitman AND NOT _type:misc'))
 +
 +
from elasticsearch.helpers import scan
 +
 +
scan(es, query={'query': {'match_all': {}}}, index='gridka_db', doc_type='person')
 +
 +
pprint.pprint([res for res in scan(es, query={'query': {'match_all': {}}}, index='gridka_db', doc_type='person')])
 +
 +
#es.delete(index='gridka_db', doc_type='person', id=u'AVbkQFrUfoCJqKB6H6bu')
 +
 +
#es.indices.delete(index='gridka_db')</pre>
 +
== neo4j ==
 +
 +
Edit <code>vim /etc/neo4j/neo4j.conf</code> and uncomment <code>dbms.connectors.default_listen_address=0.0.0.0</code>
 +
 +
Delete if exists <code>rm /var/lib/neo4j/data/dbms/auth</code> to reset the admin password.
 +
 +
Afterwards <code>service neo4j restart</code>.
 +
 +
<pre class="http://localhost:7474/browser/```"></pre>
 +
CREATE (id_0:pulp_fiction {first_name: 'Vincent', last_name: 'Vega'}) CREATE (id_1:pulp_fiction {first_name: 'Jules', last_name: 'Winnfield'}) CREATE (id_0)-[:TELLS_STORY {about: 'le big mac'}]-&gt;(id_1) CREATE (id_1)-[:TELLS_STORY {about: 'foot massage'}]-&gt;(id_0)
 +
 +
MATCH (n) RETURN (n) MATCH (n) OPTIONAL MATCH (n)-[r]-&gt;() DELETE n,r
 +
 +
MATCH (n) DETACH DELETE (n)
 +
 +
CREATE (id_2:killbill {first_name: 'Beatrice', last_name: 'Kiddo'})
 +
 +
CREATE (id_2)-[:SAME_MOVE_UNIVERSE {about: 'tarantino'}]-&gt;(id_0) CREATE (id_2)-[:SAME_MOVE_UNIVERSE {about: 'tarantino'}]-&gt;(id_1)
 +
 +
MATCH (n) where ID(n) in [11,12,13] DETACH DELETE(n)
 +
 +
MATCH (a) WHERE ID(a)=18 MATCH(b) WHERE ID(b)=16 MERGE (a)-[:SAME_MOVE_UNIVERSE {about: 'tarantino'}]-(b) MATCH (a) WHERE ID(a)=18 MATCH(b) WHERE ID(b)=17 MERGE (a)-[:SAME_MOVE_UNIVERSE {about: 'tarantino'}]-(b)
 +
 +
<pre></pre>
 +
import pprint from py2neo import Graph, Node, Relationship
 +
 +
graph = Graph(&quot;http://localhost:7474/db/data/&quot;)
 +
 +
id_0 = Node(&quot;pulp_fiction&quot;, first_name='Vincent', last_name='Vega') id_1 = Node(&quot;pulp_fiction&quot;, first_name='Jules', last_name='Winnfield') graph.create(id_0) graph.create(id_1) graph.create(Relationship(id_0, 'TELLS_STORY', id_1, about='le big mac')) graph.create(Relationship(id_1, 'TELLS_STORY', id_0, about='foot massage')) pprint.pprint([node for node in graph.match()]) ```

Latest revision as of 11:55, 19 September 2017

Hands-On Session 1[edit | edit source]

PostgreSQL[edit | edit source]

vim /etc/postgresql/9.5/main/pg_hba.conf

Set all to trust, then add this line host all all 0.0.0.0/0 trust

service postgresql restart

Should not ask for password, and you can quit with ctrl-d

sudo -u postgres /usr/bin/psql -c "CREATE ROLE gridka01 PASSWORD 'asdf1234' SUPERUSER CREATEDB CREATEROLE INHERIT LOGIN;"
sudo -u postgres /usr/bin/psql -c "CREATE DATABASE gridka_db OWNER gridka01 ENCODING 'UTF8'"
sudo -u postgres /usr/bin/psql -d gridka_db -c "CREATE SCHEMA gridka_schema AUTHORIZATION gridka01"
sudo -u postgres /usr/bin/psql -d gridka_db -c "GRANT ALL ON SCHEMA gridka_schema TO gridka01"
sudo -u postgres /usr/bin/psql -d gridka_db -c "GRANT ALL ON DATABASE gridka_db TO gridka01"

ls -la /var/lib/postgresql/9.5/

psql -U gridka01 gridka_db

vim /etc/postgresql/9.5/main/pg_hba.conf

Set all to md5

psql -U gridka01 gridka_db

Should now ask for password.

psql -U gridka01 gridka_db

\l .. list all databases

\d .. list all relations

CREATE TABLE test_table ( first_name VARCHAR, last_name VARCHAR );

\d

SELECT * FROM test_table;

INSERT INTO test_table VALUES ('Vincent', 'Vega');

SELECT * FROM test_table;

INSERT INTO test_table (last_name, first_name) VALUES ('Jules', 'Winnfield');

SELECT * FROM test_table;

DELETE FROM test_table;

SELECT * FROM test_table;

DROP TABLE test_table;

SELECT * FROM test_table;
#include <stdio.h>
#include <stdlib.h>

#include <libpq-fe.h>

int main()
{
        PGconn *conn;
        conn = PQconnectdb("dbname='gridka_db' user='gridka01' host='localhost' password='asdf1234'");
        if (PQstatus(conn) != CONNECTION_OK) {
                printf("%s\n", PQerrorMessage(conn));
                exit(1);
        }

        PGresult *res;
        res = PQexec(conn, "CREATE TABLE test_table ( first_name VARCHAR, last_name VARCHAR )");
        if (!res)
                printf("%s\n", PQerrorMessage(conn));
        PQclear(res);

        res = PQexec(conn, "INSERT INTO test_table VALUES ( 'Vincent', 'Vega' )");
        if (!res)
                printf("%s\n", PQerrorMessage(conn));
        PQclear(res);

        res = PQexec(conn, "INSERT INTO test_table (last_name, first_name) VALUES ( 'Jules', 'Winnfield' )");
        if (!res)
                printf("%s\n", PQerrorMessage(conn));
        PQclear(res);

        res = PQexec(conn, "SELECT * FROM test_table");
        if (PQresultStatus(res) == PGRES_TUPLES_OK)
                for(int i = 0; i < PQntuples(res); ++i)
                        printf("%s %s\n", PQgetvalue(res, i, 0), PQgetvalue(res, i, 1));
        else
                printf("%s\n", PQerrorMessage(conn));
        PQclear(res);

        res = PQexec(conn, "DELETE FROM test_table");
        if (!res)
                printf("%s\n", PQerrorMessage(conn));
        PQclear(res);

        PQfinish(conn);
}

gcc -g --std=c11 -Wall -Wextra -I$(pg_config --includedir) test_pg.c -o test_pg -lpq

import psycopg2 as pg
conn = pg.connect("dbname='gridka_db' user='gridka01' host='localhost' password='asdf1234'")

cur = conn.cursor()

cur.execute("CREATE TABLE test_table ( first_name VARCHAR, last_name VARCHAR )")

cur.execute("INSERT INTO test_table VALUES ( 'Vincent', 'Vega' )")
conn.commit()

cur.execute("SELECT * FROM test_table")
cur.fetchall()

cur.execute("INSERT INTO test_table (last_name, first_name) VALUES ( 'Jules', 'Winnfield' )")
conn.commit()

cur.execute("SELECT * FROM test_table")
print cur.fetchall()

cur.execute("DROP TABLE test_table")
conn.commit()

MonetDB[edit | edit source]

monetdbd create /tmp/gridka_schema
monetdbd start /tmp/gridka_schema
monetdb create gridka_db
monetdb release gridka_db

mclient -u monetdb -d gridka_db (default password: monetdb)

CREATE USER "gridka01" WITH PASSWORD 'asdf1234' NAME 'gridka01' SCHEMA "sys";
CREATE SCHEMA "gridka01" AUTHORIZATION "gridka01";
ALTER USER "gridka01" SET SCHEMA "gridka01";
CREATE TABLE test_table ( first_name VARCHAR(64), last_name VARCHAR(64) );
INSERT INTO test_table VALUES ('Vincent', 'Vega');
INSERT INTO test_table (last_name, first_name) VALUES ('Jules', 'Winnfield');
SELECT * FROM test_table;
DELETE FROM test_table;
DROP test_table;
#include <stdio.h>
#include <stdlib.h>

#include <mapi.h>

int main()
{
        Mapi db;
        db = mapi_connect("localhost", 50000,
                          "gridka01", "asdf1234",
                          "sql", "gridka_db");
        if (mapi_error(db))
                mapi_explain(db, stderr);

        MapiHdl res;
        res = mapi_query(db, "CREATE TABLE test_table ( first_name VARCHAR(64), last_name VARCHAR(64) )");
        if (mapi_error(db))
                mapi_explain(db, stderr);
        mapi_close_handle(res);

        res = mapi_query(db, "INSERT INTO test_table VALUES ( 'Vincent', 'Vega' )");
        if (mapi_error(db))
                mapi_explain(db, stderr);
        mapi_close_handle(res);

        res = mapi_query(db, "INSERT INTO test_table (last_name, first_name) VALUES ( 'Jules', 'Winnfield' )");
        if (mapi_error(db))
                mapi_explain(db, stderr);
        mapi_close_handle(res);

        res = mapi_query(db, "SELECT * FROM test_table");
        if (mapi_error(db))
                mapi_explain(db, stderr);
        while(mapi_fetch_row(res)) {
                printf("%s %s\n", mapi_fetch_field(res, 0), mapi_fetch_field(res, 1));
        }
        mapi_close_handle(res);

        res = mapi_query(db, "DELETE FROM test_table");
        if (mapi_error(db))
                mapi_explain(db, stderr);
        mapi_close_handle(res);

        mapi_destroy(db);
}

gcc -g -std=c11 -Wall -Wextra $(pkg-config --cflags --libs monetdb-mapi) monetdb_test.c -o monetdb_test

import monetdb.sql as mo
conn = mo.connect(database='gridka_db', username='gridka01', hostname='localhost', password='asdf1234')

cur = conn.cursor()

cur.execute("CREATE TABLE test_table ( first_name VARCHAR(64), last_name VARCHAR(64) )")

cur.execute("INSERT INTO test_table VALUES ( 'Vincent', 'Vega' )")
conn.commit()

cur.execute("SELECT * FROM test_table")
cur.fetchall()

cur.execute("INSERT INTO test_table (last_name, first_name) VALUES ( 'Jules', 'Winnfield' )")
conn.commit()

cur.execute("SELECT * FROM test_table")
cur.fetchall()

cur.execute("DROP TABLE test_table")
conn.commit()

LevelDB[edit | edit source]

mkdir gridka_db
#include <iostream>
#include <string>

#include <leveldb/db.h>

int main()
{
        leveldb::DB* db;
        leveldb::Options opts;
        leveldb::ReadOptions ropts;
        leveldb::WriteOptions wopts;
        leveldb::Status status;
        leveldb::Iterator* it;

        opts.create_if_missing = true;

        status = leveldb::DB::Open(opts, "gridka_db/test_table", &db);

        status = db->Put(wopts, "0", "{\"first_name\": \"Vincent\", \"last_name\": \"Vega\"}");
        status = db->Put(wopts, "1", "{\"first_name\": \"Jules\", \"last_name\": \"Winnfield\"}");

        std::string value;
        db->Get(ropts, std::string("0"), &value);
        std::cout << value << '\n';

        it = db->NewIterator(ropts);
        for(it->SeekToFirst(); it->Valid(); it->Next())
                std::cout << it->key().ToString() << '\t' << it->value().ToString() << '\n';
        delete it;

        status = db->Delete(wopts, "0");
        status = db->Delete(wopts, "1");

        delete db;
}

g++ -g -std=c++14 -Wall -Wextra leveldb_test.cpp -o leveldb_test -lleveldb

import json
import leveldb
db = leveldb.LevelDB('gridka_db/test_table')
db.Put('0', json.dumps({'first_name': 'Vincent', 'last_name': 'Vega'}))
db.Put('1', json.dumps({'first_name': 'Jules', 'last_name': 'Winnfield'}))
json.loads(db.Get('0'))
it = db.RangeIter()
print [(x[0],json.loads(x[1])) for x in it]
db.Delete('0')
db.Delete('1')

Redis[edit | edit source]

vim /etc/redis/redis.conf

requirepass asdf1234

service redis restart


auth asdf1234

set test_table:0 '{"first_name": "Vincent", "last_name": "Vega"}' set test_table:1 '{"first_name": "Jules", "last_name": "Winnfield"}'

get test_table:0

hset mtest_table:0 first_name Vincent hset mtest_table:0 last_name Vega hset mtest_table:1 first_name Jules hset mtest_table:1 last_name Winnfield

hget mtest_table:0 first_name hget mtest_table:1 last_name

del test_table:0 del test_table:1 del mtest_table:0 del mtest_table:1


include <stdio.h>[edit | edit source]

include <stdlib.h>[edit | edit source]

include <hiredis.h>[edit | edit source]

int main() { redisContext ctx; redisReply r;

    ctx = redisConnect("localhost", 6379);
    if (ctx->err)
            printf("error: %s\n", ctx->errstr);

    r = redisCommand(ctx, "auth asdf1234");
    if (r->type == REDIS_REPLY_ERROR)
            printf("error: %s\n", r->str);
    freeReplyObject(r);

    r = redisCommand(ctx, "hset mtest_table:0 first_name Vincent");
    if (r->type == REDIS_REPLY_ERROR)
            printf("error: %s\n", r->str);
    freeReplyObject(r);

    r = redisCommand(ctx, "hset mtest_table:0 last_name Vega");
    if (r->type == REDIS_REPLY_ERROR)
            printf("error: %s\n", r->str);
    freeReplyObject(r);

    r = redisCommand(ctx, "hset mtest_table:1 first_name Jules");
    if (r->type == REDIS_REPLY_ERROR)
            printf("error: %s\n", r->str);
    freeReplyObject(r);

    r = redisCommand(ctx, "hset mtest_table:1 last_name Winnfield");
    if (r->type == REDIS_REPLY_ERROR)
            printf("error: %s\n", r->str);
    freeReplyObject(r);

    r = redisCommand(ctx, "hget mtest_table:0 first_name");
    if (r->type == REDIS_REPLY_ERROR)
            printf("error: %s\n", r->str);
    else
            printf("%s\n", r->str);
    freeReplyObject(r);

    r = redisCommand(ctx, "hget mtest_table:1 last_name");
    if (r->type == REDIS_REPLY_ERROR)
            printf("error: %s\n", r->str);
    else
            printf("%s\n", r->str);
    freeReplyObject(r);

    r = redisCommand(ctx, "del mtest_table:0");
    if (r->type == REDIS_REPLY_ERROR)
            printf("error: %s\n", r->str);
    freeReplyObject(r);

    r = redisCommand(ctx, "del mtest_table:1");
    if (r->type == REDIS_REPLY_ERROR)
            printf("error: %s\n", r->str);
    freeReplyObject(r);

    redisFree(ctx);

} ```

gcc -g -std=c11 -Wall -Wextra redis_test.c -o redis_test $(pkg-config --cflags --libs hiredis)

import redis
db = redis.StrictRedis(password='asdf1234')

db.set('test_table:0', '{"first_name": "Vincent", "last_name": "Vega"}')
db.set('test_table:1', '{"first_name": "Jules", "last_name": "Winnfield"}')

db.hset('mtest_table:0', 'first_name', 'Vincent')
db.hset('mtest_table:0', 'last_name', 'Vega')
db.hset('mtest_table:1', 'first_name', 'Jules')
db.hset('mtest_table:1', 'last_name', 'Winnfield')

db.delete('test_table:0')
db.delete('test_table:1')
db.delete('mtest_table:0')
db.delete('mtest_table:1')

MongoDB[edit | edit source]

semanage port -a -t mongod_port_t -p tcp 27017
systemclt restart mongod
chkconfig mongod on

show dbs use admin db.createUser({"user": "admin", "pwd": "admin", "roles": ["userAdminAnyDatabase"]}) use gridka_db show collections db.createUser({"user": "gridka01", "pwd": "asdf1234", "roles": ["readWrite"]}) ```

vim /etc/mongod.conf

auth=true

service mongod restart

mongo gridka_db

db.test_table.insert({"first_name": "Vincent", "last_name": "Vega"})
mongo -u gridka01 -p asdf1234 gridka_db
db.test_table.insert({"first_name": "Vincent", "last_name": "Vega"})
db.test_table.insert({"first_name": "Jules", "last_name": "Winnfield"})
db.test_table.find()
db.test_table.insert({"you_can_put": "whatever you want"})
db.test_table.find()
db.test_table.drop()
#include <stdio.h>
#include <stdlib.h>

#include <bson.h>
#include <mongoc.h>

int main ()
{
        mongoc_client_t *client;
        mongoc_collection_t *collection;
        bson_error_t error;
        bson_t *doc;
        bson_t *query;
        mongoc_cursor_t *cursor;
        char *str;

        mongoc_init();

        client = mongoc_client_new("mongodb://gridka01:asdf1234@localhost:27017/?authSource=gridka_db");
        collection = mongoc_client_get_collection (client,
                                                   "gridka_db",
                                                   "test_table");

        doc = BCON_NEW("first_name",BCON_UTF8("Vincent"),
                       "last_name", BCON_UTF8("Vega"));
        if(!mongoc_collection_insert(collection, MONGOC_INSERT_NONE,
                                     doc, NULL, &error))
                printf("error: %s\n", error.message);
        bson_destroy(doc);
        doc = BCON_NEW("first_name", BCON_UTF8("Jules"),
                       "last_name", BCON_UTF8("Winnfield"));
        if(!mongoc_collection_insert(collection, MONGOC_INSERT_NONE,
                                     doc, NULL, &error))
                printf("error: %s\n", error.message);
        bson_destroy(doc);

        query = bson_new();
        cursor = mongoc_collection_find(collection, MONGOC_QUERY_NONE,
                                        0, 0, 0, query, NULL, NULL);
        while(mongoc_cursor_next(cursor, &doc)) {
                str = bson_as_json(doc, NULL);
                printf("%s\n", str);
                bson_free(str);
        }
        bson_destroy(query);
        mongoc_cursor_destroy(cursor);

        if(!mongoc_collection_drop(collection, &error))
                printf("error: %s\n", error.message);

        mongoc_collection_destroy(collection);
        mongoc_client_destroy(client);
}

gcc -g -std=c11 -Wall -Wextra -pedantic $(pkg-config --cflags --libs libmongoc-1.0) mongo_test.c -o mongo_test

from pymongo import MongoClient
client = MongoClient('mongodb://gridka01:asdf1234@localhost:27017/?authSource=gridka_db')
db = client.gridka_db
db.test_table.insert_one({'first_name': 'Vincent', 'last_name': 'Vega'})
db.test_table.insert_one({'first_name': 'Jules', 'last_name': 'Winnfield'})
[x for x in db.test_table.find()]
db.test_table.insert({'first_name': 'Marcellus', 'occupation': 'businessman'})
[x for x in db.test_table.find()]
db.test_table.drop()

ElasticSearch[edit | edit source]

curl localhost:9200

import json, pprint
import elasticsearch
es = elasticsearch.Elasticsearch()

es.index(index='gridka_db', doc_type='person', id=0, body=json.dumps({'name': 'Vincent Vega', 'occupation': 'professional hitman'}))
pprint.pprint(es.search(index='gridka_db'))

es.index(index='gridka_db', doc_type='person', id=0, body=json.dumps({'age': 42}))
pprint.pprint(es.search(index='gridka_db'))

es.index(index='gridka_db', doc_type='person', id=0, body=json.dumps({'name': 'Vincent Vega', 'occupation': 'professional hitman', 'age': 42}))
pprint.pprint(es.search(index='gridka_db'))

es.index(index='gridka_db', doc_type='person', id=1, body=json.dumps({'name': 'Jules Winnfield', 'occupation': 'professional hitman', 'hidden_occupation': 'Director of SHIELD', 'age': 57}))
pprint.pprint(es.search(index='gridka_db'))

pprint.pprint(es.search(index='gridka_db', q='professional'))

pprint.pprint(es.search(index='gridka_db', q='occupation:professional'))

pprint.pprint(es.search(index='gridka_db', q='age:<50'))

es.index(index='gridka_db', doc_type='person', body=json.dumps({'name': 'Marcellus Wallace', 'occupation': 'businessman', 'age': 66}))

pprint.pprint(es.search(index='gridka_db', q='NOT occupation:hitman'))

pprint.pprint(es.search(index='gridka_db', q='NOT occupation:hitman AND NOT _type:misc'))

from elasticsearch.helpers import scan

scan(es, query={'query': {'match_all': {}}}, index='gridka_db', doc_type='person')

pprint.pprint([res for res in scan(es, query={'query': {'match_all': {}}}, index='gridka_db', doc_type='person')])

#es.delete(index='gridka_db', doc_type='person', id=u'AVbkQFrUfoCJqKB6H6bu')

#es.indices.delete(index='gridka_db')

neo4j[edit | edit source]

Edit vim /etc/neo4j/neo4j.conf and uncomment dbms.connectors.default_listen_address=0.0.0.0

Delete if exists rm /var/lib/neo4j/data/dbms/auth to reset the admin password.

Afterwards service neo4j restart.


CREATE (id_0:pulp_fiction {first_name: 'Vincent', last_name: 'Vega'}) CREATE (id_1:pulp_fiction {first_name: 'Jules', last_name: 'Winnfield'}) CREATE (id_0)-[:TELLS_STORY {about: 'le big mac'}]->(id_1) CREATE (id_1)-[:TELLS_STORY {about: 'foot massage'}]->(id_0)

MATCH (n) RETURN (n) MATCH (n) OPTIONAL MATCH (n)-[r]->() DELETE n,r

MATCH (n) DETACH DELETE (n)

CREATE (id_2:killbill {first_name: 'Beatrice', last_name: 'Kiddo'})

CREATE (id_2)-[:SAME_MOVE_UNIVERSE {about: 'tarantino'}]->(id_0) CREATE (id_2)-[:SAME_MOVE_UNIVERSE {about: 'tarantino'}]->(id_1)

MATCH (n) where ID(n) in [11,12,13] DETACH DELETE(n)

MATCH (a) WHERE ID(a)=18 MATCH(b) WHERE ID(b)=16 MERGE (a)-[:SAME_MOVE_UNIVERSE {about: 'tarantino'}]-(b) MATCH (a) WHERE ID(a)=18 MATCH(b) WHERE ID(b)=17 MERGE (a)-[:SAME_MOVE_UNIVERSE {about: 'tarantino'}]-(b)


import pprint from py2neo import Graph, Node, Relationship

graph = Graph("http://localhost:7474/db/data/%22)

id_0 = Node("pulp_fiction", first_name='Vincent', last_name='Vega') id_1 = Node("pulp_fiction", first_name='Jules', last_name='Winnfield') graph.create(id_0) graph.create(id_1) graph.create(Relationship(id_0, 'TELLS_STORY', id_1, about='le big mac')) graph.create(Relationship(id_1, 'TELLS_STORY', id_0, about='foot massage')) pprint.pprint([node for node in graph.match()]) ```

 PreviousNext