【问题标题】:Insert data into Hbase using Stargate Rest使用 Stargate Rest 将数据插入 Hbase
【发布时间】:2012-04-17 21:15:13
【问题描述】:

我正在使用 curl 通过 REST 访问 Hbase。我在将数据插入 Hbase 时遇到问题。我遵循了 Stargate 文档,但是当我遵循相同的语法时,它给了我 400/405 错误请求错误和不允许方法错误。我已经粘贴了下面的命令。请告诉我哪里出错了。

星际之门文档说

POST /<table>/<row>/<column> (:qualifier)?/<timestamp>
curl -H "Content-Type: text/xml" --data '[...]' http://localhost:8000/test/testrow/test:testcolumn

我的curl命令如下:

curl -H "Content-Type: text/xml" --data '[<CellSet><Row key="111"><Cell column="f1">xyz</Cell></Row></CellSet>]' http://localhost:8080/mytable/row/fam

这样做的正确方法是什么?因为这给了我 Bad request 错误。

另外,我在 Python 客户端中尝试了同样的操作。它给了我 ColumnFamilyNotFoundException。我正在从文件中读取要传递给星际之门服务器的 Xml 数据。代码如下。

url = 'http://localhost:8080/mytable/row/fam' f = open('example.xml', 'r') xmlData = f.read() r = requests.post(url, data=xmlData, headers=headers)

example.xml 有以下内容:

<CellSet>
     <Row key="111">
   <Cell column="fam:column1">
             xyz
         </Cell>
     </Row>
 </CellSet>

【问题讨论】:

    标签: python rest insert hbase stargate


    【解决方案1】:

    这是一个非常简单的错误。 Hbase 期望 base64 编码中的每个值。 Tha key 以及 columnfamily:column 在输入 xml 之前必须经过 base64 编码。

    【讨论】:

      【解决方案2】:

      使用starbase很容易插入。

      $ pip install starbase

      创建一个名为table1 的表,其中包含col1col2

      from starbase import Connection
      connection = Connection()
      table = connection.table('table1')
      table.create('col1', 'col2')
      

      table1 中插入一行。行键是row1

      table.insert(
          'row1', 
          {
              'col1': {'key1': 'val1', 'key2': 'val2'}, 
              'col2': {'key3': 'val3', 'key4': 'val4'}
          }
      )
      

      您也可以批量插入。

      不重复代码,假设我们的数据存储在data变量(dict)中。

      data = {
          'col1': {'key1': 'val1', 'key2': 'val2'}, 
          'col2': {'key3': 'val3', 'key4': 'val4'}
      }
      
      batch = table.batch()
      for i in range(100, 5000):
          batch.insert('row_%s' % i, data)
      batch.commit(finalize=True)
      

      使用update 方法完成更新,工作方式与insert 相同。

      要获取一行,请使用fetch 方法。

      获取整行:

      table.fetch('row1')
      

      仅获取col1 数据:

      table.fetch('row1', 'col1')
      

      仅获取 col1col2 数据:

      table.fetch('row1', ['col1', 'col2'])
      

      仅获取 col1:key1col2:key4 数据:

      table.fetch('row1', {'col1': ['key1'], 'col2': ['key4']})
      

      更改表架构:

      添加列col3col4

      table.add_columns('col3', 'col4')
      

      删除列

      table.drop_columns('col1', 'col4')
      

      显示表格列

      table.columns()
      

      显示所有表格

      connection.tables()
      

      【讨论】:

        猜你喜欢
        • 2012-06-10
        • 2015-10-11
        • 2011-01-09
        • 2014-01-23
        • 2013-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-07
        相关资源
        最近更新 更多