0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
apache_log_query.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  * MA 02110-1301, USA.
20  */
21 #include "Common/Compat.h"
22 
23 #include <cstdio>
24 
25 #include "Common/Error.h"
26 #include "Common/System.h"
27 
28 #include "Hypertable/Lib/Client.h"
29 
30 using namespace Hypertable;
31 using namespace std;
32 
33 namespace {
34 
35  const char *usage =
36  "\n"
37  " usage: apache_log_query <row-prefix> [ <cf> ... ]\n"
38  "\n"
39  " Queries the table 'LogDb' for all rows that start\n"
40  " with <row-prefix>. If no column families are\n"
41  " specified, then all column families are returned.\n"
42  " Otherwise, just the column families specified by\n"
43  " the <cf> arguments are returned\n";
44 }
45 
52 int main(int argc, char **argv) {
53  ClientPtr client_ptr;
54  NamespacePtr namespace_ptr;
55  TablePtr table_ptr;
56  TableScannerPtr scanner_ptr;
57  ScanSpecBuilder scan_spec_builder;
58  Cell cell;
59  String end_row;
60 
61  if (argc <= 1) {
62  cout << usage << endl;
63  return 0;
64  }
65 
66  try {
67 
68  // Create Hypertable client object
69  client_ptr = make_shared<Client>( System::locate_install_dir(argv[0]) );
70 
71  // Open the root namespace
72  namespace_ptr = client_ptr->open_namespace("/");
73 
74  // Open the 'LogDb' table
75  table_ptr = namespace_ptr->open_table("LogDb");
76 
77  // setup row interval
78  end_row = (String)argv[1];
79  end_row.append(1, static_cast<char>(0xff)); // next minimum row
80  scan_spec_builder.add_row_interval(argv[1], true, end_row.c_str(), false);
81 
82  // setup scan_spec columns
83  for (int i=2; i<argc; i++)
84  scan_spec_builder.add_column(argv[i]);
85 
86  // Create a scanner on the 'LogDb' table
87  scanner_ptr.reset(table_ptr->create_scanner(scan_spec_builder.get()));
88 
89  }
90  catch (std::exception &e) {
91  cerr << "error: " << e.what() << endl;
92  return 1;
93  }
94 
95  // Iterate through the cells returned by the scanner
96  while (scanner_ptr->next(cell)) {
97  printf("%s\t%s", cell.row_key, cell.column_family);
98  if (*cell.column_qualifier)
99  printf(":%s", cell.column_qualifier);
100  printf("\t%s\n", std::string((const char *)cell.value,
101  cell.value_len).c_str());
102  }
103 
104  return 0;
105 }
Retrieves system information (hardware, installation directory, etc)
ScanSpec & get()
Returns the built ScanSpec object.
Definition: ScanSpec.h:566
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
int main(int argc, char **argv)
This program is designed to query the table 'LogDb' for the contents of the row that has the key equa...
const char * column_qualifier
Definition: Cell.h:68
STL namespace.
std::shared_ptr< TableScanner > TableScannerPtr
Smart pointer to TableScanner.
Definition: TableScanner.h:124
std::shared_ptr< Namespace > NamespacePtr
Shared smart pointer to Namespace.
Definition: Namespace.h:333
std::shared_ptr< Client > ClientPtr
Definition: Client.h:156
Compatibility Macros for C/C++.
const char * row_key
Definition: Cell.h:66
Helper class for building a ScanSpec.
Definition: ScanSpec.h:318
Hypertable definitions
static String locate_install_dir(const char *argv0)
Returns the installation directory.
Definition: System.cc:50
void add_column(const string &str)
Adds a column family to be returned by the scan.
Definition: ScanSpec.h:408
void add_row_interval(const string &start, bool start_inclusive, const string &end, bool end_inclusive)
Adds a row interval to be returned in the scan.
Definition: ScanSpec.h:455
const char * column_family
Definition: Cell.h:67
uint32_t value_len
Definition: Cell.h:72
Encapsulates decomposed key and value.
Definition: Cell.h:32
Error codes, Exception handling, error logging.
std::shared_ptr< Table > TablePtr
Definition: Table.h:53
const uint8_t * value
Definition: Cell.h:71