initial commit

This commit is contained in:
Administrator 2023-03-07 14:35:47 +01:00
commit 3802936800
6 changed files with 250 additions and 0 deletions

6
README.md Normal file
View File

@ -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
```

52
elasticsearch_nested.py Normal file
View File

@ -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}]}
'''

View File

@ -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}
'''

View File

@ -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'}
'''

1
requirements.txt Normal file
View File

@ -0,0 +1 @@
elasticsearch

84
sqlite.py Normal file
View File

@ -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')
'''