0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
freebase_load.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; version 3 of the
9  * License.
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, MA
19  * 02110-1301, USA.
20  */
21 
22 #include <Common/Compat.h>
23 
24 #include "freebase_parser.h"
25 
26 #include <Hypertable/Lib/Client.h>
27 #include <Hypertable/Lib/KeySpec.h>
28 
29 #include <Common/Error.h>
30 #include <Common/System.h>
31 
32 #include <cstdio>
33 #include <cstring>
34 #include <iostream>
35 
36 using namespace Hypertable;
37 using namespace std;
38 
39 namespace {
40 
41  const char *usage =
42  "\n"
43  " usage: freebase_load <file>\n"
44  "\n"
45  " Loads a freebase .tsv file into a table called\n"
46  " freebase with the following schema:\n"
47  "\n"
48  "create table freebase (\n"
49  " name,\n"
50  " category,\n"
51  " property\n"
52  ");"
53  "\n"
54  " This program assumes that the first line in the\n"
55  " .tsv file contains the column names and that the\n"
56  " first two columns are 'name' followed by 'id'.\n"
57  " The 'id' column is used as the row key and the\n"
58  " name of the file (minus the .tsv extension) is\n"
59  " taken as the category.\n";
60 
61 }
62 
63 
64 
68 int main(int argc, char **argv) {
69  freebase_parser parser;
70  ClientPtr client_ptr;
71  NamespacePtr namespace_ptr;
72  TablePtr table_ptr;
73  TableMutatorPtr mutator_ptr;
74  KeySpec key;
75  const char *inputfile;
76  String row;
77  InsertRec *recs;
78  int count;
79 
80  if (argc == 2)
81  inputfile = argv[1];
82  else {
83  cout << usage << endl;
84  return 0;
85  }
86 
87  parser.load(inputfile);
88 
89  try {
90 
91  // Create Hypertable client object
92  client_ptr = make_shared<Client>( System::locate_install_dir(argv[0]) );
93 
94  // Open the root namespace
95  namespace_ptr = client_ptr->open_namespace("/");
96 
97  // Open the 'free' table
98  table_ptr = namespace_ptr->open_table("freebase");
99 
100  // Create a mutator object on the
101  // 'LogDb' table
102  mutator_ptr.reset(table_ptr->create_mutator());
103 
104  }
105  catch (Exception &e) {
106  HT_ERROR_OUT << e << HT_END;
107  return 1;
108  }
109 
110  while ((recs = parser.next(&count)) != 0) {
111  for (int i=0; i<count; i++) {
112  try {
113  mutator_ptr->set(recs[i].key, recs[i].value, recs[i].value_len);
114  }
115  catch (Exception &e) {
116  HT_ERROR_OUT << e << HT_END;
117  quick_exit(EXIT_FAILURE);
118  }
119  }
120  }
121 
122  // Flush pending updates
123  try {
124  mutator_ptr->flush();
125  }
126  catch (Exception &e) {
127  cerr << "Exception caught: " << Error::get_text(e.code()) << endl;
128  quick_exit(EXIT_FAILURE);
129  }
130 
131 }
Retrieves system information (hardware, installation directory, etc)
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
STL namespace.
bool load(const std::string fname)
std::shared_ptr< Namespace > NamespacePtr
Shared smart pointer to Namespace.
Definition: Namespace.h:333
std::shared_ptr< Client > ClientPtr
Definition: Client.h:156
std::shared_ptr< TableMutator > TableMutatorPtr
Smart pointer to TableMutator.
Definition: TableMutator.h:257
const char * get_text(int error)
Returns a descriptive error message.
Definition: Error.cc:330
int main(int argc, char **argv)
Compatibility Macros for C/C++.
#define HT_END
Definition: Logger.h:220
#define HT_ERROR_OUT
Definition: Logger.h:301
Hypertable definitions
static String locate_install_dir(const char *argv0)
Returns the installation directory.
Definition: System.cc:50
This is a generic exception class for Hypertable.
Definition: Error.h:314
InsertRec * next(int *countp)
Error codes, Exception handling, error logging.
std::shared_ptr< Table > TablePtr
Definition: Table.h:53
int code() const
Returns the error code.
Definition: Error.h:391