gpt4 book ai didi

hadoop - 如何修复 Jython 和 HBase 中的 "ImportError: No module name apache"错误

转载 作者:可可西里 更新时间:2023-11-01 16:35:32 26 4
gpt4 key购买 nike

我正在尝试运行以下 Jython 代码来扫描 hbase 表:

    import java.lang
from org.apache.hadoop.hbase import TableName, HBaseConfiguration
from org.apache.hadoop.hbase.client import Connection, ConnectionFactory,
Result, ResultScanner, Table, Admin
from org.apache.hadoop.conf import Configuration
conf = HBaseConfiguration.create()
connection = ConnectionFactory.createConnection(conf)
admin = connection.getAdmin()
tableName = TableName.valueOf('station')
table = connection.getTable(tableName)
cf = "stn"
attr = "name"
scanner = table.getScanner(cf)
while 1:
result = scanner.next()
if not result:
break
print java.lang.String(result.row),
java.lang.String(result.getValue(cf, attr))

我正在使用以下命令来尝试运行它:

jython test1.py

但是我得到以下错误:

Traceback (most recent call last):
File "test1.py", line 2, in <module>
from org.apache.hadoop.hbase import TableName, HBaseConfiguration
ImportError: No module named apache

我该如何解决这个问题?我已经在独立模式下安装了 hbase,它工作正常(使用 hbase shell 创建了一些示例表)。

更新:正如评论中所建议的,我尝试了以下命令来运行它:

jython -Dpython.path=./hbase-1.4.8/lib/hbase-common-1.4.8.jar test1.py

我现在收到以下错误消息:

Traceback (most recent call last): File "test1.py", line 2, in from org.apache.hadoop.hbase import TableName, HBaseConfiguration java.lang.NoClassDefFoundError: org/apache/hadoop/io/RawComparator at org.apache.hadoop.hbase.NamespaceDescriptor.(NamespaceDescriptor.java:45) at org.apache.hadoop.hbase.TableName.(TableName.java:82) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:348) at org.python.core.Py.loadAndInitClass(Py.java:991) at org.python.core.Py.findClassInternal(Py.java:926) at org.python.core.Py.findClassEx(Py.java:977) at org.python.core.packagecache.SysPackageManager.findClass(SysPackageManager.java:133) at org.python.core.packagecache.PackageManager.findClass(PackageManager.java:33) at org.python.core.packagecache.SysPackageManager.findClass(SysPackageManager.java:122) at org.python.core.PyJavaPackage.findattr_ex(PyJavaPackage.java:134) at org.python.core.PyObject.findattr(PyObject.java:946) at org.python.core.imp.importFromAs(imp.java:1160) at org.python.core.imp.importFrom(imp.java:1132) at org.python.pycode._pyx0.f$0(test1.py:13) at org.python.pycode._pyx0.call_function(test1.py) at org.python.core.PyTableCode.call(PyTableCode.java:167) at org.python.core.PyCode.call(PyCode.java:18) at org.python.core.Py.runCode(Py.java:1386) at org.python.util.PythonInterpreter.execfile(PythonInterpreter.java:296) at org.python.util.jython.run(jython.java:362) at org.python.util.jython.main(jython.java:142) Caused by: java.lang.ClassNotFoundException: org.apache.hadoop.io.RawComparator at org.python.core.SyspathJavaLoader.findClass(SyspathJavaLoader.java:131) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 22 more

java.lang.NoClassDefFoundError: java.lang.NoClassDefFoundError: org/apache/hadoop/io/RawComparator

我该如何解决?

最佳答案

类路径中似乎缺少该库。默认情况下,jython.jar 仅包含 lib 目录中的默认 python 模块。需要在类路径中添加任何第三方模块。

您需要在库模块中添加 hbase jar 的引用,如下所示:

jython -Dpython.path=/<location of jar>/hbase.jar test1.py

另一种选择是:

java -cp /<location of jar>/hbase.jar;jython.jar org.python.util.jython test1.py

Jython 类路径应包含 HBASE 类路径以包含所有依赖项。

更多详情请引用: https://hbase.apache.org/book.html#jython

关于hadoop - 如何修复 Jython 和 HBase 中的 "ImportError: No module name apache"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53879922/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com