I am using kusto .net SDK to write to kusto database using c#.. I am able to create the table as I please, but I wish to be able to write some rows to the database and populate the columns "a" and "b".. couldn't find an easy way to do this online..
static void Main(string[] args)
{
var KustoClusterConnectionString = $"xxxxxxxxxxxxxx";
var KustoDbName = "userGroup";
var AppId = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";
var TenantId = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";
var clientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";
var kcsb = new KustoConnectionStringBuilder(KustoClusterConnectionString)
{
InitialCatalog = KustoDbName,
FederatedSecurity = true,
ApplicationClientId = AppId,
ApplicationKey = clientSecret,
Authority = TenantId
};
var tableName = "userGroup";
using (var kustoClient = KustoClientFactory.CreateCslAdminProvider(kcsb))
{
var command =
CslCommandGenerator.GenerateTableCreateCommand(
tableName,
new[]
{
//Tuple.Create("TimeStamp", "System.DateTime"),
Tuple.Create("a", "System.String"),
Tuple.Create("b", "System.String"),
});
kustoClient.ExecuteControlCommand(KustoDbName, command);
}
}
there are several code samples in this repo: https://github.com/Azure/azure-kusto-samples-dotnet
specifically - this project/file: https://github.com/Azure/azure-kusto-samples-dotnet/blob/master/client/HowToDataIngestion/Program.cs
Related
I am using CosmosDb Sdk with nuget Microsoft.Azure.DocumentDB.Core 2.1.0 with .NetStandard 2.0.
I am using this query to fetch documents
var client client = new DocumentClient(new Uri(config.CosmosDbEndPointUrl), config.CosmosDbPrimaryKey);
var partitionKey = "siteId"; // the partition key is defined as /siteId, I tried both, still not working
var queryOptions = new FeedOptions
{
MaxItemCount = 500,
EnableCrossPartitionQuery = true,
PartitionKey = new PartitionKey(partitionKey)
};
var auditQuery = client
.CreateDocumentQuery<AuditDTO>(
UriFactory.CreateDocumentCollectionUri(_databaseName, _collectionName), queryOptions)
.Where(f => f.Status == AuditStatus.Pending)
.AsDocumentQuery();
var results = new List<AuditDTO>();
while (auditQuery.HasMoreResults)
{
var audits = await auditQuery.ExecuteNextAsync<AuditDTO>();
results.AddRange(audits);
}
This query returns 0 document.
I based my query on this tutorial
I thought some parameters were incorrect so, I tried to use the Rest Api, code based on this example (see comment EXECUTE a query)
var client = new System.Net.Http.HttpClient();
string response = string.Empty;
string authHeader = string.Empty;
string verb = string.Empty;
string resourceType = string.Empty;
string resourceId = string.Empty;
string resourceLink = string.Empty;
client.DefaultRequestHeaders.Add("x-ms-date", utc_date);
client.DefaultRequestHeaders.Add("x-ms-version", "2017-02-22");
verb = "POST";
resourceType = "docs";
resourceLink = $"dbs/{databaseId}/colls/{collectionId}/docs";
resourceId = (idBased) ? $"dbs/{databaseId}/colls/{collectionId}" : collectionId.ToLowerInvariant();
authHeader = GenerateMasterKeyAuthorizationSignature(verb, resourceId, resourceType, masterKey, "master", "1.0");
client.DefaultRequestHeaders.Remove("authorization");
client.DefaultRequestHeaders.Add("authorization", authHeader);
client.DefaultRequestHeaders.Add("x-ms-documentdb-isquery", "True");
client.DefaultRequestHeaders.Add("x-ms-documentdb-query-enablecrosspartition", "true");
client.DefaultRequestHeaders.Add("x-ms-partition-key", "[\"siteId\"]");
var qry = new SqlQuerySpec { query = "SELECT * FROM root WHERE (root[\"status\"] = 0)" };
var r = await client.PostWithNoCharSetAsync(new Uri(baseUri, resourceLink), qry);
var s = await r.Content.ReadAsStringAsync();
Using the Rest Api returns 1 document as I am expecting.
I tried to deserialize the response to the class AuditDTO, it works, so, no problem with my model.
I don't understand why the Rest Api is working and not the .Net Sdk.
There is no authorization exception, no error message. I just get 0 document.
I am missing something?
var partitionKey = "siteId";
var queryOptions = new FeedOptions
{
MaxItemCount = 500,
EnableCrossPartitionQuery = true,
PartitionKey = new PartitionKey(partitionKey)
};
Your problem is on the partition key.
Partition key is expected to be the value of the partition key not the definition.
This means that the partitionKey value on FeedOptions should look like this 59c49da3-b398-4db7-aff4-d2129353e3a8 (assuming it's a guid) not this siteId.
The correct FeedOptions Object would look like this:
var partitionKey = "59c49da3-b398-4db7-aff4-d2129353e3a8";
var queryOptions = new FeedOptions
{
MaxItemCount = 500,
EnableCrossPartitionQuery = true,
PartitionKey = new PartitionKey(partitionKey)
};
The documentation is really misleading.
To enable CrossPartition queries to query all partitions, you just need to set EnableCrossPartitionQuery to true and that's it!
var queryOptions = new FeedOptions
{
MaxItemCount = 500,
EnableCrossPartitionQuery = true
};
As Nick Chapsas said, if you need to search a specific partition, set the PartitionKey property to the value of the partition you want to search against.
Here is a sample I found on Azure GitHub.
I am using ElastciSearch 2.3.0
I am trying to delete documents from the ElasticSearch using .net and NEST for specific index.
I only want to delete all documents and not the _mapping
DeleteByQueryRequest r = new DeleteByQueryRequest(new IndexName() { Name = indexName });
r.QueryOnQueryString = "*";
var response = client.DeleteByQuery(r);
I am try to do this by using above code but it is not working.
Please suggest on what's wrong with the above code or how this can be done.
Thanks for your help in advance.
Don't use delete by query it was made a plugin since elastic 2.0 for a good reason. You can get out of memory exceptions easy.
You should delete the whole index and recreate the mappings
static void Main(string[] args)
{
ElasticClient db = new ElasticClient(new Uri("http://localhost.fiddler:9200"));
db.IndexMany(Enumerable.Range(0, 100).Select(i => new Data { Id = i, Name = "Name" + i }), "test_index");
var mappings = db.GetMapping<Data>();
var delete = db.DeleteIndex(new DeleteIndexRequest("test_index"));
var indexMapping = mappings.IndexTypeMappings["test_index"].ToDictionary(k => k.Key, v => (ITypeMapping)v.Value);
db.CreateIndex(new CreateIndexRequest("test_index")
{
Mappings = new Mappings(indexMapping)
});
Console.ReadLine();
}
class Data
{
public int Id { get; set; }
public string Name { get; set; }
}
Raw copy of index
var res = db.LowLevel.IndicesGetMapping<JObject>("test_index");
var delete = db.DeleteIndex(new DeleteIndexRequest("test_index"));
var mappings = res.Body["test_index"].ToString();
var create = db.LowLevel.IndicesCreate<JObject>("test_index", mappings);
If you really need to install the plug-in
sudo bin/plugin install delete-by-query
It worked.
Thanks a lot.
var res = db.LowLevel.IndicesGetMapping<JObject>("test_index");
var delete = db.DeleteIndex(new DeleteIndexRequest("test_index"));
var mappings = res.Body["test_index"].ToString();
var create = db.LowLevel.IndicesCreate<JObject>("test_index", mappings);
var ec2Client = new AmazonEC2Client();
string amiID = "ami;
string keyPairName = "aaaa";
// List<string> groups = new List<string>() ;
var launchRequest = new RunInstancesRequest()
{
ImageId = amiID,
InstanceType = "tttt",
MinCount = 1,
MaxCount = 1,
KeyName = kkkk,
SubnetId = "bbbb",
};
How do I hard-code my access key and secret access key ?
var awsCreds = new BasicAWSCredentials("ACCESS_KEY", "SECRET_KEY");
var ec2Config = new AmazonEC2Config() {
// add configurations here,
// such as ServiceURL and RegionEndpoint
}
var ec2Client = new AmazonEC2Client(awsCreds, ec2Config);
Learn more:
AmazonEC2Client Class
AmazonEC2Config Class
Anyway, hard-coding access key and secret key is not recommended. Please be really cautious, especially when you are going to share your code to a public repository.
I am trying to read a row from my MongoDB with the following code
public static void MongoConnection()
{
var connectionString = "127.0.0.1";
var mongoClient = new MongoClient(connectionString);
var mongoServer = mongoClient.GetServer();
var databaseName = "PointToPoint";
var db = mongoServer.GetDatabase(databaseName);
var mongodb = db.GetCollection("OCS.MeterEntity");
BsonDocument documentRead = mongodb.FindOne(new QueryDocument {
{"_id", "4B414D000000011613CD" }
});
But the documentRead is always null and i know the specifik _id exists in the database. What am i doing wrong?
It does connect to the database and the right table.
Seems to be a calling convention:
new QueryDocument( "_id", new ObjectId("4B414D000000011613CD") )
but this should work as well:
new BSONDocument { { "_id", "4B414D000000011613CD" } }
I mannaged to figure it out :)
public static void MongoConnection()
{
var connectionString = "mongodb://localhost";
var mongoClient = new MongoClient(connectionString);
var mongoServer = mongoClient.GetServer();
var databaseName = "PointToPoint";
var db = mongoServer.GetDatabase(databaseName);
var mongodb = db.GetCollection<MongoDBModel>("OCS.MeterEntity");
var mongodbQuery = Query<MongoDBModel>.EQ(x => x._id, "4B414D000000011613CD");
var foundMongoDB = mongodb.FindOne(mongodbQuery);
}
Hi I am trying to make a generic method to return a single value from the database
I am not sure where to put the return result, value is returned as a BSON document.I need 1 value from it....
public static string SearchMongoSingleValue(string key, string value, string ducument, string datatable)
{
var connectionString = "mongodb://localhost";
var client = new MongoClient(connectionString);
var server = client.GetServer();
var database = server.GetDatabase(datatable);
var collection = database.GetCollection(ducument);
var query = Query.EQ(key, value);
var oneDocument = collection.FindOne(query);
return oneDocument[value];
Thanks
I think you need oneDocument[key] and not oneDocument[value]. Just tested this code:
using MongoDB.Driver;
using MongoDB.Bson;
using MongoDB.Driver.Builders;
var client = new MongoClient("mongodb://localhost");
var coll = client.GetServer().GetDatabase("local").GetCollection("test1");
var doc = new BsonDocument();
doc.Add("Name","John");
doc.Add("Color","Red");
coll.Insert(doc);
var query = Query.EQ("Name", "John");
var doc2 = coll.FindOne(query);
var value = doc2["Color"];
It returns "Red" allright