该程序创建与 cassandra 中的 sstableloader 一起使用的 sstables。但是,当我运行这个程序时,它给了我这个错误:
找不到 cassandra.yaml致命配置错误;无法启动服务器。 有关堆栈跟踪,请参阅日志。
我在 2.1.2 位 ubuntu 12.04 机器上运行 Apache Cassandra v 12.04。我在datastax网站上找到了一个合理的解决方案,链接在这里,但我无法遵循确切的解决方案
有人知道下一步该怎么做吗?
package com.cassandra;
import static org.apache.cassandra.utils.ByteBufferUtil.bytes;
import static org.apache.cassandra.utils.UUIDGen.decompose;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.apache.cassandra.db.marshal.AbstractType;
import org.apache.cassandra.db.marshal.AsciiType;
import org.apache.cassandra.db.marshal.CompositeType;
import org.apache.cassandra.db.marshal.CompositeType.Builder;
import org.apache.cassandra.db.marshal.UUIDType;
import org.apache.cassandra.dht.Murmur3Partitioner;
import org.apache.cassandra.io.sstable.SSTableSimpleUnsortedWriter;
public class SStableBuilder {
static String csvfilename = "records.csv";
public static void main(String[] args) {
try {
buildSStables();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void buildSStables() throws Exception {
String keyspace = "samplekey";
String table = "users";
File directory = new File(keyspace + "/" + table);
if (!directory.exists()) {
directory.mkdirs();
}
List<AbstractType<?>> compositeColumnValues = new ArrayList<AbstractType<?>>();
compositeColumnValues.add(AsciiType.instance);
compositeColumnValues.add(AsciiType.instance);
CompositeType compositeColumn = CompositeType.getInstance(compositeColumnValues);
System.out.println("yahahahaha");
SSTableSimpleUnsortedWriter bulkWriter = new SSTableSimpleUnsortedWriter(
directory, new Murmur3Partitioner(), keyspace, table,
compositeColumn, null, 64);
// Create a single timestamp for each insert
long timestamp = System.currentTimeMillis() * 1000;
BufferedReader reader = new BufferedReader(new FileReader(csvfilename));
String line;
int lineNumber = 1;
CsvEntry entry = new CsvEntry();
while ((line = reader.readLine()) != null) {
if (entry.parse(line, lineNumber)) {
ByteBuffer uuid = ByteBuffer.wrap(decompose(entry.key));
bulkWriter.newRow(uuid);
Builder builder = compositeColumn.builder();
builder.add(bytes(entry.firstname));
builder.add(bytes("firstname"));
bulkWriter.addColumn(builder.build(), bytes(entry.firstname), timestamp);
builder = compositeColumn.builder();
builder.add(bytes(entry.firstname));
builder.add(bytes("lastname"));
bulkWriter.addColumn(builder.build(), bytes(entry.lastname), timestamp);
builder = compositeColumn.builder();
builder.add(bytes(entry.firstname));
builder.add(bytes("password"));
bulkWriter.addColumn(builder.build(), bytes(entry.password), timestamp);
builder = compositeColumn.builder();
builder.add(bytes(entry.firstname));
builder.add(bytes("age"));
bulkWriter.addColumn(builder.build(), bytes(String.valueOf(entry.age)), timestamp);
builder = compositeColumn.builder();
builder.add(bytes(entry.firstname));
builder.add(bytes("email"));
bulkWriter.addColumn(builder.build(), bytes(entry.email), timestamp);
}
lineNumber++;
}
reader.close();
System.out.println("Success");
bulkWriter.close();
System.exit(0);
}
static class CsvEntry {
UUID key;
String firstname;
String lastname;
String password;
long age;
String email;
boolean parse(String line, int lineNumber) {
// Ghetto csv parsing
String[] columns = line.split(",");
if (columns.length != 6) {
System.out.println(String.format(
"Invalid input '%s' at line %d of %s", line,
lineNumber, csvfilename));
return false;
}
try {
key = UUID.fromString(columns[0].trim());
firstname = columns[1].trim();
lastname = columns[2].trim();
password = columns[3].trim();
age = Long.parseLong(columns[4].trim());
email = columns[5].trim();
return true;
} catch (NumberFormatException e) {
System.out.println(String.format(
"Invalid number in input '%s' at line %d of %s", line,
lineNumber, csvfilename));
return false;
}
}
}
}
该配置是从系统属性"cassandra.config"指定的URL加载的。您可以将其设置为指向您的文件。
如果未设置该值,它将查找缺省的"cassandra.yaml"作为类加载器资源。你可以通过确保你的CLASSPATH包含一个包含cassandra.yaml的目录的路径来使它成功。
https://github.com/apache/cassandra/blob/0ce9abd78c8712ad4fba28734214b18f617a2aac/src/java/org/apache/cassandra/config/YamlConfigurationLoader.java#L55-L68