commit 3802936800fa5b0b9f51d2c57cf961ca62dd8301 Author: H4CK3R-01 Date: Tue Mar 7 14:35:47 2023 +0100 initial commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..538f5df --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +# Datenbanken II example project + +## Run elasticsearch +``` +docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:8.6.2 +``` \ No newline at end of file diff --git a/elasticsearch_nested.py b/elasticsearch_nested.py new file mode 100644 index 0000000..3dc757c --- /dev/null +++ b/elasticsearch_nested.py @@ -0,0 +1,52 @@ +from elasticsearch import Elasticsearch + +# Create a connection to Elasticsearch +es = Elasticsearch("http://localhost:9200") + +# Define an index mapping +mapping = { + "mappings": { + "properties": { + "customer_name": {"type": "keyword"}, + "orders": { + "type": "nested", + "properties": { + "order_id": {"type": "integer"}, + "product_name": {"type": "keyword"}, + "quantity": {"type": "integer"}, + "price": {"type": "float"} + } + } + } + } +} + +# Create an index with the mapping +es.indices.create(index="customers", body=mapping) + +# Insert some documents +es.index(index="customers", body={"customer_name": "Alice", "orders": [ + {"order_id": 1, "product_name": "Apple", "quantity": 2, "price": 1.00}, + {"order_id": 4, "product_name": "Banana", "quantity": 1, "price": 0.50} +]}) +es.index(index="customers", body={"customer_name": "Bob", "orders": [ + {"order_id": 2, "product_name": "Banana", "quantity": 3, "price": 0.50} +]}) +es.index(index="customers", body={"customer_name": "Charlie", "orders": [ + {"order_id": 3, "product_name": "Cherry", "quantity": 1, "price": 2.00}, + {"order_id": 5, "product_name": "Durian", "quantity": 2, "price": 3.00} +]}) + +# Search for documents +query = {"query": {"nested": {"path": "orders", "query": {"match": {"orders.product_name": "Banana"}}}}} +result = es.search(index="customers", body=query) + +# Print the search results +for hit in result['hits']['hits']: + print(hit['_source']) + +# OUTPUT +''' +{'customer_name': 'Alice', 'orders': [{'order_id': 4, 'product_name': 'Banana', 'quantity': 1, 'price': 0.5}]} +{'customer_name': 'Bob', 'orders': [{'order_id': 2, 'product_name': 'Banana', 'quantity': 3, 'price': 0.5}]} +''' \ No newline at end of file diff --git a/elasticsearch_one_index.py b/elasticsearch_one_index.py new file mode 100644 index 0000000..0b135f1 --- /dev/null +++ b/elasticsearch_one_index.py @@ -0,0 +1,41 @@ +from elasticsearch import Elasticsearch + +# Create a connection to Elasticsearch +es = Elasticsearch("http://localhost:9200") + +# Define an index mapping +mapping = { + "mappings": { + "properties": { + "customer_name": {"type": "keyword"}, + "order_id": {"type": "integer"}, + "product_name": {"type": "keyword"}, + "quantity": {"type": "integer"}, + "price": {"type": "float"} + } + } +} + +# Create an index with the mapping +es.indices.create(index="orders", body=mapping) + +# Insert some documents +es.index(index="orders", body={"customer_name": "Alice", "order_id": 1, "product_name": "Apple", "quantity": 2, "price": 1.00}) +es.index(index="orders", body={"customer_name": "Bob", "order_id": 2, "product_name": "Banana", "quantity": 3, "price": 0.50}) +es.index(index="orders", body={"customer_name": "Charlie", "order_id": 3, "product_name": "Cherry", "quantity": 1, "price": 2.00}) +es.index(index="orders", body={"customer_name": "Alice", "order_id": 4, "product_name": "Banana", "quantity": 1, "price": 0.50}) +es.index(index="orders", body={"customer_name": "Charlie", "order_id": 5, "product_name": "Durian", "quantity": 2, "price": 3.00}) + +# Search for documents +query = {"query": {"match": {"customer_name": "Alice"}}} +result = es.search(index="orders", body=query) + +# Print the search results +for hit in result['hits']['hits']: + print(hit['_source']) + +# OUTPUT +''' +{'customer_name': 'Alice', 'order_id': 1, 'product_name': 'Apple', 'quantity': 2, 'price': 1.0} +{'customer_name': 'Alice', 'order_id': 4, 'product_name': 'Banana', 'quantity': 1, 'price': 0.5} +''' \ No newline at end of file diff --git a/elasticsearch_parent_child.py b/elasticsearch_parent_child.py new file mode 100644 index 0000000..ee7ed46 --- /dev/null +++ b/elasticsearch_parent_child.py @@ -0,0 +1,66 @@ +from elasticsearch import Elasticsearch + +# Create a connection to Elasticsearch +es = Elasticsearch() + +# Define an index mapping for customers and orders +mapping = { + "mappings": { + "properties": { + "customer_name": {"type": "keyword"} + } + } +} + +order_mapping = { + "mappings": { + "properties": { + "order_id": {"type": "integer"}, + "product_name": {"type": "keyword"}, + "quantity": {"type": "integer"}, + "price": {"type": "float"}, + "customer_id": {"type": "join", "relations": {"customer": "order"}} + } + } +} + +# Create the customers and orders indices with the mappings +es.indices.create(index="customers", body=mapping) +es.indices.create(index="orders", body=order_mapping) + +# Insert some documents +es.index(index="customers", body={"customer_name": "Alice"}) +es.index(index="customers", body={"customer_name": "Bob"}) +es.index(index="customers", body={"customer_name": "Charlie"}) + +es.index(index="orders", body={"order_id": 1, "product_name": "Apple", "quantity": 2, "price": 1.00, "customer_id": {"name": "Alice", "parent": "customer"}}) +es.index(index="orders", body={"order_id": 2, "product_name": "Banana", "quantity": 3, "price": 0.50, "customer_id": {"name": "Bob", "parent": "customer"}}) +es.index(index="orders", body={"order_id": 3, "product_name": "Cherry", "quantity": 1, "price": 2.00, "customer_id": {"name": "Charlie", "parent": "customer"}}) +es.index(index="orders", body={"order_id": 4, "product_name": "Banana", "quantity": 1, "price": 0.50, "customer_id": {"name": "Alice", "parent": "customer"}}) +es.index(index="orders", body={"order_id": 5, "product_name": "Durian", "quantity": 2, "price": 3.00, "customer_id": {"name": "Charlie", "parent": "customer"}}) + +# Search for documents +query = { + "query": { + "has_child": { + "type": "order", + "query": { + "match": { + "product_name": "Banana" + } + } + } + } +} + +result = es.search(index="customers", body=query) + +# Print the search results +for hit in result['hits']['hits']: + print(hit['_source']) + +# OUTPUT +''' +{'customer_name': 'Alice'} +{'customer_name': 'Bob'} +''' \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..62164e2 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +elasticsearch \ No newline at end of file diff --git a/sqlite.py b/sqlite.py new file mode 100644 index 0000000..7a19b40 --- /dev/null +++ b/sqlite.py @@ -0,0 +1,84 @@ +import sqlite3 + +# Connect to the database (creates it if it doesn't already exist) +conn = sqlite3.connect('mydatabase.db') + +# Create a cursor object to execute SQL commands +c = conn.cursor() + +# Drop all tables +c.execute('''DROP TABLE customers''') +c.execute('''DROP TABLE orders''') + +# Create a table for customers +c.execute('''CREATE TABLE customers + (id INTEGER PRIMARY KEY, + name text, + email text)''') + +# Create a table for orders +c.execute('''CREATE TABLE orders + (id INTEGER PRIMARY KEY, + customer_id INTEGER, + product text, + quantity INTEGER, + FOREIGN KEY (customer_id) REFERENCES customers(id))''') + +# Insert some data into customers +c.execute("INSERT INTO customers VALUES (1, 'Alice', 'alice@example.com')") +c.execute("INSERT INTO customers VALUES (2, 'Bob', 'bob@example.com')") +c.execute("INSERT INTO customers VALUES (3, 'Charlie', 'charlie@example.com')") + +# Insert some data into orders +c.execute("INSERT INTO orders VALUES (1, 1, 'Product A', 2)") +c.execute("INSERT INTO orders VALUES (2, 2, 'Product B', 1)") +c.execute("INSERT INTO orders VALUES (3, 1, 'Product C', 3)") + +# Save (commit) the changes +conn.commit() + +# Retrieve data +print("SELECT * FROM customers") +c.execute("SELECT * FROM customers") +rows = c.fetchall() +for row in rows: + print(row) +print("") + +print("SELECT * FROM orders") +c.execute("SELECT * FROM orders") +rows = c.fetchall() +for row in rows: + print(row) +print("") + +print("SELECT orders.product, customers.name FROM orders JOIN customers ON orders.customer_id == customers.id") +c.execute("SELECT orders.product, customers.name FROM orders JOIN customers ON orders.customer_id == customers.id") +rows = c.fetchall() +for row in rows: + print(row) +print("") + + +# Close the cursor and the connection +c.close() +conn.close() + + +# OUTPUT +''' +SELECT * FROM customers +(1, 'Alice', 'alice@example.com') +(2, 'Bob', 'bob@example.com') +(3, 'Charlie', 'charlie@example.com') + +SELECT * FROM orders +(1, 1, 'Product A', 2) +(2, 2, 'Product B', 1) +(3, 1, 'Product C', 3) + +SELECT orders.product, customers.name FROM orders JOIN customers ON orders.customer_id == customers.id +('Product A', 'Alice') +('Product B', 'Bob') +('Product C', 'Alice') +''' \ No newline at end of file