gpt4 book ai didi

java - 如何使用 freeze> 进行 datastax

转载 作者:行者123 更新时间:2023-11-30 06:13:05 25 4
gpt4 key购买 nike

我正在使用 datastax,并希望从 Cassandra 检索 map 。

尝试使用 java 中的 datastax 驱动程序从 Cassandra 检索卡住集列。但无法在 java 的 std 输出中打印该列或打印到 csv。我得到的只是一个空白输出。下面是我正在尝试执行的代码。它能够打印该表中除设置的列之外的所有列值。如果有人以前这样做过,请给我一个想法。

package com.cassandra.cassandrafetch1;
import java.util.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Iterator;

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.ColumnDefinitions.Definition;
import com.datastax.driver.core.DataType;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.SimpleStatement;
import com.datastax.driver.core.Statement;

import com.google.common.collect.Sets;

public class CassExport {

static int i = 0;

public static void main(String[] args) throws FileNotFoundException {

long startTime = System.currentTimeMillis();
PrintWriter pw = new PrintWriter(new File("test.csv"));
String keyspace = "xxxxxxx";
String table = "xxxxxxxxxxx";
String username = "xxxxx";
String password = "xxxxxx";
String host = "xxxxxx";
double count = 0;
Cluster.Builder clusterBuilder = Cluster.builder()
.addContactPoints(host)
.withCredentials(username, password);
Cluster cluster = clusterBuilder.build();
Session session = cluster.connect(keyspace);
Statement stmt = new SimpleStatement("SELECT names FROM " + table );
stmt.setFetchSize(2000);
ResultSet rs = session.execute(stmt);
Iterator<Row> iter = rs.iterator();
while ( !rs.isFullyFetched()) {
if (rs.getAvailableWithoutFetching() == 120 )
rs.fetchMoreResults();
Row row = iter.next();
if ( rs != null )
{
StringBuilder line = new StringBuilder();
for (Definition key : row.getColumnDefinitions().asList())
{
String val = myGetValue(key, row);
line.append("\"");
line.append(val);
line.append("\"");
line.append(',');
}
line.deleteCharAt(line.length()-1);
line.append('\n');
pw.write(line.toString());


System.out.println(line.toString());
++count;
}
}
pw.close();
session.close();
cluster.close();
System.out.println(count + "\t rows copied into csv");
long endTime = System.currentTimeMillis();
System.out.println("Took "+(endTime - startTime) + " ms");
}

public static String myGetValue(Definition key, Row row)
{
String str = "";
if (key != null)
{
String col = key.getName();
try
{
if (key.getType() == DataType.cdouble())
{
str = new Double(row.getDouble(col)).toString();
}
else if (key.getType() == DataType.cint())
{
str = new Integer(row.getInt(col)).toString();
}
else if (key.getType() == DataType.uuid())
{
str = row.getUUID(col).toString();
}
else if (key.getType() == DataType.cfloat())
{
str = new Float(row.getFloat(col)).toString();
}
else if (key.getType() == DataType.timestamp())
{
str = row.getDate(col).toString();
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ");
str = fmt.format(row.getDate(col));
}
else if (key.getType().equals(DataType.frozenSet(DataType.varchar())))
{
try {
for(int i = 0; i < ; i++) {
Set<String> st = row.getSet(i, String.class );

System.out.println(st);
str = st;

}
}
else
{
str = row.getString(col); }
} catch (Exception e)
{
str = "";
}
}
return str;
}
}

架构:

CREATE TABLE xxx.xxxx (
dynamic uuid,
source text,
view int,
names frozen<set<text>>,
nameid tinyint,
groupid uuid,
texts int,
total int static,
notin text,
PRIMARY KEY ((dynamicid, source, view), names)

)

该集列的 cqlsh 输出:

            names
--------------------------------------------------------------------
---------------------------------------------------------------------------------
{').', '0/11', 'ndf', 'STOP', 'where', 'No', 'You', 'zxz', 'are', 'at', 'forward', 'looking', 'to'}
{').', '1/17', 'STOP', 'nowhere', 'Unsubscribe', 'We', 'ndt', 'are', 'Word', 'brett', 'ndf', 'hgf'}

最佳答案

您犯了一个错误 - 您试图通过调用 row.getSet(i, String.class) 来访问错误的元素 - 在这种情况下,您试图访问row 按索引,而不是按名称检索整个集合。

在您的情况下,处理卡住集的代码应如下:

if (key.getType().equals(DataType.frozenSet(DataType.varchar()))) {
Set<String> ts = row.getSet(col, String.class);
for (String string : ts) {
//... do something with set's content...
}
}

关于java - 如何使用 freeze<set<text>> 进行 datastax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49821589/

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