gpt4 book ai didi

java - 使用 SparkConf 连接到远程 Cassandra 集群时如何获得 pass "requires authentication"?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:41:43 26 4
gpt4 key购买 nike

我正在尝试使用 apache spark 和 cassandra 进行数据分析。所以我写了一个java代码来访问在远程机器上运行的cassandra。我使用了以下 java 代码。

public class JavaDemo implements Serializable {
private transient SparkConf conf;

private JavaDemo(SparkConf conf) {
this.conf = conf;
}

private void run() {
JavaSparkContext sc = new JavaSparkContext(conf);
generateData(sc);
compute(sc);
showResults(sc);
sc.stop();
}

private void generateData(JavaSparkContext sc) {
CassandraConnector connector = CassandraConnector.apply(sc.getConf());
Session session = connector.openSession();

// Prepare the schema

session.execute("DROP KEYSPACE IF EXISTS java_api");
session.execute("CREATE KEYSPACE java_api WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}");
session.execute("CREATE TABLE java_api.products (id INT PRIMARY KEY, name TEXT, parents LIST<INT>)");
session.execute("CREATE TABLE java_api.sales (id UUID PRIMARY KEY, product INT, price DECIMAL)");
session.execute("CREATE TABLE java_api.summaries (product INT PRIMARY KEY, summary DECIMAL)");

}

private void compute(JavaSparkContext sc) {
System.out.println("IN compute");
}

private void showResults(JavaSparkContext sc) {
System.out.println("IN showResults");
}

public static void main(String[] args) {


SparkConf conf = new SparkConf();
conf.setAppName("Java API demo");
conf.setMaster("local[1]");
System.out.println("---------------------------------");
conf.set("spark.cassandra.connection.host", "192.168.1.219");


JavaDemo app = new JavaDemo(conf);
app.run();
}

其中 192.168.1.219 是我运行 cassandra 的远程主机。默认端口为 9160。当我运行程序时出现以下错误。

    15/01/29 10:14:26 INFO ui.SparkUI: Started Spark Web UI at http://Justin:4040
15/01/29 10:14:27 WARN core.FrameCompressor: Cannot find LZ4 class, you should make sure the LZ4 library is in the classpath if you intend to use it. LZ4 compression will not be available for the protocol.
Exception in thread "main" com.datastax.driver.core.exceptions.AuthenticationException: Authentication error on host /192.168.1.219:9042: Host /192.168.1.219:9042 requires authentication, but no authenticator found in Cluster configuration
at com.datastax.driver.core.AuthProvider$1.newAuthenticator(AuthProvider.java:38)
at com.datastax.driver.core.Connection.initializeTransport(Connection.java:139)
at com.datastax.driver.core.Connection.<init>(Connection.java:111)
at com.datastax.driver.core.Connection$Factory.open(Connection.java:445)
at com.datastax.driver.core.ControlConnection.tryConnect(ControlConnection.java:216)
at com.datastax.driver.core.ControlConnection.reconnectInternal(ControlConnection.java:172)
at com.datastax.driver.core.ControlConnection.connect(ControlConnection.java:80)
at com.datastax.driver.core.Cluster$Manager.init(Cluster.java:1145)
at com.datastax.driver.core.Cluster.getMetadata(Cluster.java:313)
at com.datastax.spark.connector.cql.CassandraConnector$.com$datastax$spark$connector$cql$CassandraConnector$$createSession(CassandraConnector.scala:166)
at com.datastax.spark.connector.cql.CassandraConnector$$anonfun$4.apply(CassandraConnector.scala:151)
at com.datastax.spark.connector.cql.CassandraConnector$$anonfun$4.apply(CassandraConnector.scala:151)
at com.datastax.spark.connector.cql.RefCountedCache.createNewValueAndKeys(RefCountedCache.scala:36)
at com.datastax.spark.connector.cql.RefCountedCache.acquire(RefCountedCache.scala:61)
at com.datastax.spark.connector.cql.CassandraConnector.openSession(CassandraConnector.scala:72)
at com.datastax.spark.demo.JavaDemo.generateData(JavaDemo.java:42)
at com.datastax.spark.demo.JavaDemo.run(JavaDemo.java:34)
at com.datastax.spark.demo.JavaDemo.main(JavaDemo.java:73)

有什么我想念的吗?它直接连接到端口 9042。我该如何连接它?

最佳答案

您的 cassandra 集群似乎配置了身份验证。由于您不提供凭据,因此不允许您连接。您可以使用 spark.cassandra.auth.usernamespark.cassandra.auth.password 属性传递身份验证凭据,如所述 here .

所以你可以这样做:

conf.set("spark.cassandra.auth.username", "cassandra");            
conf.set("spark.cassandra.auth.password", "cassandra");

在您的代码中使其工作。

如果你有authentication启用并且您还没有创建/更改任何用户,您可以使用“cassandra”作为用户名和密码。但在生产中,您应该创建一个单独的帐户并使用它,并更改 cassandra 用户密码,因为它可以访问所有内容。

关于java - 使用 SparkConf 连接到远程 Cassandra 集群时如何获得 pass "requires authentication"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28207195/

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