159 lines
5.3 KiB
TeX
159 lines
5.3 KiB
TeX
\hypertarget{install-couchbase-and-pandas-with-specified-versions}{%
|
|
\subsubsection{Install couchbase and pandas with specified
|
|
versions}\label{install-couchbase-and-pandas-with-specified-versions}}
|
|
|
|
The cell below installs the couchbase python package and pandas.
|
|
|
|
\hypertarget{0c9dbddc-f5f5-4f58-a2e2-dfff0faf517f}{}
|
|
\begin{minipage}{1.0\textwidth}
|
|
\begin{lstlisting}[language=iPython]
|
|
!pip install couchbase==4.1.1 -q
|
|
!pip install pandas==1.5.2 -q
|
|
\end{lstlisting}
|
|
\end{minipage}
|
|
|
|
\leavevmode\vadjust pre{\hypertarget{8f41454c-0bf0-4e53-992b-34fb47bf7dd1}{}}%
|
|
Import the the classes necessary to run the couchbase connector.
|
|
|
|
\hypertarget{5c4c7d02-c06b-49c3-a6e6-9819e9eae44b}{}
|
|
\begin{minipage}{1.0\textwidth}
|
|
\begin{lstlisting}[language=iPython]
|
|
import pandas as pd
|
|
from couchbase.cluster import Cluster
|
|
from couchbase.auth import PasswordAuthenticator
|
|
|
|
from couchbase.options import ClusterOptions, QueryOptions
|
|
\end{lstlisting}
|
|
\end{minipage}
|
|
|
|
\leavevmode\vadjust pre{\hypertarget{2e5173f8-7c64-40f4-8fbe-e3bf28ad96a3}{}}%
|
|
Connect to the cluster select the sample bucket and connect to the
|
|
default collection.
|
|
|
|
\hypertarget{66ada60d-8a49-473b-bc38-a40999ba761a}{}
|
|
\begin{minipage}{1.0\textwidth}
|
|
\begin{lstlisting}[language=iPython]
|
|
cluster = Cluster.connect(
|
|
"couchbase://couchbase1",
|
|
ClusterOptions(
|
|
PasswordAuthenticator("Administrator", "some-pw-that-is-better-than-this!")
|
|
),
|
|
)
|
|
bucket = cluster.bucket("travel-sample")
|
|
|
|
# get a reference to the default collection
|
|
cb_coll = bucket.default_collection()
|
|
\end{lstlisting}
|
|
\end{minipage}
|
|
|
|
\leavevmode\vadjust pre{\hypertarget{3dbc2840-9793-4a92-802b-78d0b333e632}{}}%
|
|
Acces a document by key.
|
|
|
|
\hypertarget{5e5fd8c8-200f-4857-b63d-1172b842f11c}{}
|
|
\begin{minipage}{1.0\textwidth}
|
|
\begin{lstlisting}[language=iPython]
|
|
# get a document by key
|
|
result = cb_coll.get("airline_10")
|
|
result.value
|
|
\end{lstlisting}
|
|
\end{minipage}
|
|
|
|
\begin{lstlisting}
|
|
{'id': 10,
|
|
'type': 'airline',
|
|
'name': '40-Mile Air',
|
|
'iata': 'Q5',
|
|
'icao': 'MLA',
|
|
'callsign': 'MILE-AIR',
|
|
'country': 'United States'}
|
|
\end{lstlisting}
|
|
|
|
\leavevmode\vadjust pre{\hypertarget{e818c095-e2d9-40c7-9234-ceddcb27cddb}{}}%
|
|
The Querry language contains a If a hyphen is used please use a pair of
|
|
` to surround the name using a hyphen.
|
|
|
|
\hypertarget{8eb6d0be-9fc0-4c89-ad18-801c0349d5cc}{}
|
|
\begin{minipage}{1.0\textwidth}
|
|
\begin{lstlisting}[language=iPython]
|
|
result = cluster.query(
|
|
"""
|
|
SELECT airport.*
|
|
FROM `travel-sample`.inventory.airport airport
|
|
""",
|
|
QueryOptions(metrics=True),
|
|
)
|
|
df = pd.DataFrame(result)
|
|
df.info()
|
|
\end{lstlisting}
|
|
\end{minipage}
|
|
|
|
\begin{lstlisting}
|
|
<class 'pandas.core.frame.DataFrame'>
|
|
RangeIndex: 1968 entries, 0 to 1967
|
|
Data columns (total 9 columns):
|
|
# Column Non-Null Count Dtype
|
|
--- ------ -------------- -----
|
|
0 airportname 1968 non-null object
|
|
1 city 1968 non-null object
|
|
2 country 1968 non-null object
|
|
3 faa 1709 non-null object
|
|
4 geo 1968 non-null object
|
|
5 icao 1687 non-null object
|
|
6 id 1968 non-null int64
|
|
7 type 1968 non-null object
|
|
8 tz 1968 non-null object
|
|
dtypes: int64(1), object(8)
|
|
memory usage: 138.5+ KB
|
|
\end{lstlisting}
|
|
|
|
\hypertarget{c1237c23-2ab4-4fa0-9895-0691c20827b6}{}
|
|
\begin{minipage}{1.0\textwidth}
|
|
\begin{lstlisting}[language=iPython]
|
|
result = cluster.query(
|
|
"""
|
|
SELECT airport.airportname, airport.city, airport.country
|
|
FROM `travel-sample`.inventory.airport airport INNER JOIN `travel-sample`.inventory.landmark landmark on airport.city = landmark.city
|
|
""",
|
|
QueryOptions(metrics=True),
|
|
)
|
|
pd.DataFrame(result).sample(5)
|
|
\end{lstlisting}
|
|
\end{minipage}
|
|
|
|
\begin{tabular}{llll}
|
|
\toprule
|
|
{} & airportname & city & country \\
|
|
\midrule
|
|
\textbf{4877} & London Heliport & London & United Kingdom \\
|
|
\textbf{4289} & London St Pancras & London & United Kingdom \\
|
|
\textbf{3411} & Stansted & London & United Kingdom \\
|
|
\textbf{6457} & London - Kings Cross & London & United Kingdom \\
|
|
\textbf{8759} & Charles De Gaulle & Paris & France \\
|
|
\bottomrule
|
|
\end{tabular}
|
|
|
|
\hypertarget{a4cc7ef5-511e-4709-9d82-e1c20238f173}{}
|
|
\begin{minipage}{1.0\textwidth}
|
|
\begin{lstlisting}[language=iPython]
|
|
result = cluster.query(
|
|
"""
|
|
SELECT airport.airportname, airport.city, airport.country, landmark.name
|
|
FROM `travel-sample`.inventory.airport airport INNER JOIN `travel-sample`.inventory.landmark landmark on airport.city = landmark.city
|
|
""",
|
|
QueryOptions(metrics=True),
|
|
)
|
|
pd.DataFrame(result).sample(5).sort_index()
|
|
\end{lstlisting}
|
|
\end{minipage}
|
|
|
|
\begin{tabular}{lllll}
|
|
\toprule
|
|
{} & airportname & city & country & name \\
|
|
\midrule
|
|
\textbf{443 } & Edinburgh & Edinburgh & United Kingdom & Murrayfield Ice Rink \\
|
|
\textbf{1487} & Luton & London & United Kingdom & The Phoenix \\
|
|
\textbf{3620} & Paddington Station & London & United Kingdom & Kensal Green Cemetery \\
|
|
\textbf{5629} & London-Corbin Airport-MaGee Field & London & United States & Alexandra Palace \\
|
|
\textbf{8588} & Le Bourget & Paris & France & Aux Merveilleux de Fred \\
|
|
\bottomrule
|
|
\end{tabular} |